strlen的汇编 [英] strlen in assembly

查看:227
本文介绍了strlen的汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了我自己实现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的汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆