strlen的汇编

编程入门 行业动态 更新时间:2024-10-26 20:30:37
本文介绍了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的汇编

本文发布于:2023-07-30 00:45:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:strlen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!