我有一个名为string_length的函数,其汇编代码如下。
0x08048e90 <+0>: push %ebp
0x08048e91 <+1>: mov %esp,%ebp
0x08048e93 <+3>: mov 0x8(%ebp),%edx // assign whatever I declared into edx
0x08048e96 <+6>: mov $0x0,%eax // assign eax = 0
0x08048e9b <+11>: cmpb $0x0,(%edx) // compare edx to byte of 0 (null..?)
0x08048e9e <+14>: je 0x8048ea9 <string_length+25> // if equal, jump to +25
0x08048ea0 <+16>: add $0x1,%eax // else, add 1 to eax
0x08048ea3 <+19>: cmpb $0x0,(%edx,%eax,1) // compare byte 1*eax+edx with 0,
0x08048ea7 <+23>: jne 0x8048ea0 <string_length+16> // if not equal, back to +16
0x08048ea9 <+25>: pop %ebp // pop ebp
0x08048eaa <+26>: ret
因为函数名为string_length,所以我认为它将返回字符串中包含的字符数量。
我困惑的是
cmpb $0x0,(%edx)
这是在将edx指向的内容与0进行比较,而在ASCII码中0代表的是null。
还有:
cmpb $0x0,(%edx,%eax,1)
在比较时,按字节比较的是1 * eax + edx。如果edx是字符串,那么它是否意味着edx将首先转换为其ascii值,然后执行计算?