我在汇编中实现了自己的 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;
}
谢谢
最佳答案
您不是在访问字节(字符),而是双字。所以您的代码不是在寻找单个终止零,而是在寻找 4 个连续的零。请注意,不会总是返回正确的值 +4,这取决于字符串包含的内存内容。
要修复,您应该使用字节访问,例如将 edx
更改为 dl
。
关于linux - 组装中的强项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041481/