assembly - 比较两个字符时出现问题,其中一个字符是从带有间接寻址的字符串中得到的
问题描述
所以我是组装的新手,我需要一些内存地址的帮助。(我认为它们是内存地址,但我不确定)所以,我有一个字符串,我想找到字符 % 出现在哪里。
所以,我做了一个循环,将值 37 移动到一个寄存器中(37 是 % 的 ascii 值),首先我尝试将该寄存器与 mystring(%counter) 进行比较,其中 counter 是我用作索引的寄存器,每次循环结束时将其递增 1。然后,我运行调试工具,发现比较总是错误的。因此,我将值 mystring(%counter) 移动到另一个寄存器中,当我再次运行 gdb 时,我看到那里有一个非常大的数字。所以,我认为这是一个内存地址或其他东西,并试图将 mystring(%counter) 加载到寄存器中。这也没有奏效。
.text
mystring: .asciz "asdb%asd%af " //that's how I declared my string
loop1:
cmpq %r9 , %r14 //in r9 I have the length of the string, r14 is the
//index
jl if1
if1:
movw $37 , %bx
leaw mystring(%r14) , %ax
cmpw %ax , %bx
je something
incq %r14
jmp loop1
因此,即使 mystring(%r14) 指向一个 % ,如果相等的跳转也不会发生,并且当我运行调试器时,它在 ax 中显示了一个大数字。(我还尝试调整寄存器的大小,因为我希望这会以某种方式改变值,这就是我使用 w 后缀的原因。) PS 这是我在这里的第一篇文章,所以如果我不遵守某些规则或不要太苛刻某物。:)
解决方案
这段代码有几个问题。
- 该字符串由 8 位 ASCII 字符组成,因此代码应使用 8 位比较。
- 它永远不会从字符串中读取字符的值。
- 如果找不到 '%' 字符,它永远不会退出循环。
这是您修复了这些问题的代码。
.text
mystring: .asciz "asdb%asd%af " //that's how I declared my string
loop1:
cmpq %r9, %r14 //in r9 I have the length of the string, r14 is the
//index
jge endloop1
movb $37, %bl
movb mystring(%r14), %al
cmpb %bl, %al
je something
incq %r14
jmp loop1
endloop1:
我有一些额外的建议来改进此代码:
- 将循环检查放在循环的末尾。
用一条指令替换
movb
and指令。cmpb
cmpq %r9, %r14 // This check may not be necessary if the string jge skiploop1 // length is known to be greater than 0. loop1: cmpb $37, mystring(%r14) je something incq %r14 cmpq %r9, %r14 // r9 is the length of the string, r14 is the // index jl loop1 skiploop1: