Why does this x86_64 assembly code does not work?
section .text
global my_strlen
my_strlen:
xor rax, rax
.LOOP:
cmp BYTE[rdi+rax], 0
jne .LOOP
inc rax
ret
I execute it with:
#include <stddef.h>
#include <stdio.h>
extern size_t my_strlen(const char *str);
int main(int argc, char *argv[]) {
if(argc!=2) return 1;
开发者_如何学运维 printf("%lu\n", (unsigned long)my_strlen(argv[1]));
return 0;
}
But when I execute the program, it does not output anything and it does not close.
You need to increment rax inside the loop.
Increment rax
in the loop. Otherwise, you always test the same byte.
my_strlen:
xor rax, rax
dec rax
.LOOP:
inc rax
cmp BYTE[rdi+rax], 0
jne .LOOP
Increasing rax outside the loop
精彩评论