了解汇编语言中的cmpb和循环 [英] understand cmpb and loops in assembly language
问题描述
我有一个具有以下汇编代码的函数string_length
I have a function string_length that has the following assembly code
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,所以我假设它将返回字符串中有多少个字符.
Since the function name is string_length, I am assuming it will return how many characters is in the string.
我感到困惑的是
cmpb $0x0,(%edx)
这是否将指向edx的内容与0字节进行比较,而ASCII中的0为空.?
is this comparing whatever is pointed to edx to the byte of 0, and 0 in ASCII is null..?
和
cmpb $0x0,(%edx,%eax,1)
以字节为单位比较1 * eax + edx.如果edx是字符串,这是否意味着edx将首先转换其ascii值,然后执行计算?
is comparing, in bytes, 1*eax+edx. If edx is a string, does that mean edx will first be converted its ascii value and then perform the calculation?
推荐答案
此:
cmpb $0x0,(%edx)
采用EDX指向的字节(即包含地址)并将其与零比较.这个:
takes a byte that EDX points to (i. e. contains the address of) and compares it to zero. This:
cmpb $0x0,(%edx,%eax,1)
接受EDX + EAX指向的字节并将其与零进行比较. EDX充当字符串基本指针,EAX充当索引.小数位数为1,因为我们正在处理字节.这样想整个循环:for(eax=0; edx[eax] != 0; eax++)
.
takes a byte that EDX+EAX points to and compares it to zero. EDX serves as the string base pointer, EAX is the index. Scale is 1 because we're working with bytes. Think of the whole loop this way: for(eax=0; edx[eax] != 0; eax++)
.
这篇关于了解汇编语言中的cmpb和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!