在寄存器中迭代通过字符串
问题描述:
我在ebx寄存器中有一个字符串,我想遍历它的字节。我是装配新手,想要这个概念的工作。在寄存器中迭代通过字符串
#string is in ebx
movl (%ebx), %edi #put the adresse of the string to %edi
jmp looper
looper:
cmpl $0, %edi # check if byte at edi is 0 (it's a c conform string so 0 is the terminator)
je exit
addl $8, %edi # load next value, increment the edi by 8 to move it to the next byte of the string
jmp looper # jump to loop beginning
答
如果字符串是物理上包含在EBX寄存器中,那么计算一个地址是不可能的。
如果字符串在EBX寄存器指向的值,则
movl (%ebx), %edi #put the adresse of the string to %edi
需求是movl %ebx,%edi
代码cmpl $0, %edi
不会在EDI检查字节而是测试,如果EDI本身零。更好地使用cmpb $0,(%edi)
为什么在检查字节时将8添加到EDI寄存器?
By _“我在ebx寄存器中有一个字符串”_,你的意思是说你是'ebx'中字符串的_address?如果是这样,这看起来不正确:'movl(%ebx),%edi#输入字符串的地址到%edi'。 – Michael 2014-11-08 10:40:38
我认为这是实际价值。该字符串来自命令行参数,并通过popl%ebx复制到%ebx。 – user2147674 2014-11-08 12:00:16
这似乎不是一个特别好的主意。 'ebx'寄存器只能容纳4个字节,即4个ASCII字符(如果你有一个空终止符,则为3个)。 – Michael 2014-11-08 13:22:45