I\'m learning x86 assembly on GNU/Linux, and I\'m trying to write a program that reads user input from stdin and prints it on stdout.
The following code does work, b
Here's one way of calculating the length of a string in x86 assembly:
lea esi,[string]
mov ecx,-1 ; Start with ecx = -1
xor eax,eax ; Clear eax
cld ; Make scasb scan forward
repne scasb ; while (ecx != 0) { ecx--; if (*esi++ == al) break; }
; ecx now contains -1 - (strlen(string) + 1) == -strlen(string) - 2
not ecx ; Inverting ecx gives us -(-strlen(string) - 2) - 1 == strlen(string) + 1
dec ecx ; Subtract 1 to get strlen(string)
This assumes that the string is NUL-terminated ('\0'
). If the string uses some other terminator you'll have to initialize al
to that value before repne scasb
.