我做了我自己实现strlen的汇编,但它不会返回正确的值。它返回字符串的长度+ 4,因此。我不明白为什么..我希望任何你... 大会来源:
I made my own implementation of strlen in assembly, but it doesn't return the correct value. It returns the string length + 4. Consequently. I don't see why.. and I hope any of you do... Assembly source:
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; }感谢
推荐答案您没有访问字节数(字),但双字。所以,你的code是不是找一个终止零,它正在寻找连续4零。请注意,不会总是返回正确的值+4,这取决于你的字符串后,存储器包含。
You are not accessing bytes (characters), but doublewords. So your code is not looking for a single terminating zero, it is looking for 4 consecutive zeroes. Note that won't always return correct value +4, it depends on what the memory after your string contains.
要解决,你应该使用字节访问,例如通过改变 EDX 到 DL 。
To fix, you should use byte accesses, for example by changing edx to dl.
更多推荐
strlen的汇编
发布评论