Read and print user input with x86 assembly (GNU/Linux)

前端 未结 2 559
一生所求
一生所求 2021-01-14 17:11

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

2条回答
  •  攒了一身酷
    2021-01-14 17:36

    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.

提交回复
热议问题