Mic*_*hel 5 linux x86 assembly nasm strlen
我在汇编中实现了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
Run Code Online (Sandbox Code Playgroud)
和主要例程:
#include <stdio.h>
extern int stringlen(char *);
int main(void)
{
printf("%d", stringlen("h"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
您访问的不是字节(字符),而是双字。因此,您的代码不是在寻找单个终止零,而是在寻找 4 个连续的零。请注意,并不总是返回正确的值+4,这取决于字符串后面的内存包含的内容。
要修复此问题,您应该使用字节访问,例如更改edx为dl.
感谢您的回答。在此处为与我有相同问题的人提供工作代码。
section .text
[GLOBAL stringlen:]
stringlen:
push ebp
mov ebp, esp
mov edx, [ebp+8] ; the string
xor eax, eax ; loop counter
jmp if
then:
inc eax
if:
mov cl, [edx+eax]
cmp cl, 0x0
jne then
end:
pop ebp
ret
Run Code Online (Sandbox Code Playgroud)