我自己用汇编语言实现了strlen函数,但是它返回的值不正确。它返回字符串长度+4。因此,我不知道为什么会这样,希望有人能够帮忙解决。
汇编源码:
section .text
[GLOBAL stringlen:] ; C function
stringlen:
push ebp
mov ebp, esp ; setup the stack frame
mov ecx, [ebp+8]
xor eax, eax ; loop counter
startLoop:
xor edx, edx
mov edx, [ecx+eax]
inc eax
cmp edx, 0x0 ; null byte
jne startLoop
end:
pop ebp
ret
主程序如下:
#include <stdio.h>
extern int stringlen(char *);
int main(void)
{
printf("%d", stringlen("h"));
return 0;
}
Thanks
mov edx, [ecx+eax]
会从内存中加载4个字节,覆盖掉edx
中的任何内容(在这种情况下是零)。 - Jester