我编写了下面的汇编函数,可以从C调用它来计算以null结尾的字符串的长度。但是由于某些原因,计数总是减少+1。我不知道为什么。有什么线索吗?
谢谢!
1 .text
2 .globl _len
3 _len:
4 pushl %ebp # set up stack frame
5 movl %esp, %ebp # save esp in ebp
6 movl 8(%ebp), %ecx # the beg of string
7 xor %eax, %eax # init length to 0
8
9 start:
10 xor %edx, %edx # char at this index
11 movb (%ecx), %dl #
12 inc %eax
13 inc %ecx
14
15 cmpb $0x0, %dl
16 jne start
17 end:
18
19 movl %ebp, %esp # restore esp
20 popl %ebp # restore ebp
21 ret
22 .end
23 发布于 2012-03-04 07:49:37
您正在计算终止的零字符。要么从-1开始,要么在比较后递增。
发布于 2012-03-04 06:17:52
在递增字符计数器(eax)之前,尝试将当前字符与零(cmpb $0x0, %dl)进行比较。例如,如果是一个空字符串,您的函数将为此字符串返回1,因为在确保有有效字符要计数之前,计数器会递增。
https://stackoverflow.com/questions/9550293
复制相似问题