【发布时间】:2016-05-04 04:25:43
【问题描述】:
我正在尝试在 x86-64 程序集中编写一个简单的过程,它只返回整数数组的长度。数组中的最后一个元素是 0,不应计算在内。数组作为 int * 从 C 代码传入。
我的汇编代码如下:
f1:
movq $0, %rax # zero out %rax
jmp test # jump to test
body:
incq %rax # increment %rax, which is counter and array index
test:
cmpq $0, (%rdi,%rax,4) # compare (rdi + (rax * 4)) to 0
jne body # jump if zero flag is not set
ret
当它运行时,我得到一个不正确的结果,但也不是完全不正确,所以不是 11(传递的数组大小减去结尾 0)我得到 38。我认为正在发生的是我的比较语句是不正确。我的想法是,由于 cmpq 在不更改寄存器的情况下执行 (dest - src),如果数组索引为 0,则 0-0 将产生零,因此将设置零标志,但这似乎没有发生。
我可以任意将数组的任意元素加载到%rax中,返回正确的值:
movq (%rdi,%rax,4), %rax # %rax initially 0, so first element loaded into %rax
任何帮助将不胜感激!
【问题讨论】:
-
做得很好,让你的代码非常简短,只是没有做你认为应该做的部分。为了解释你认为应该发生的事情,所以很容易解释那个具体的事情。
标签: arrays assembly x86-64 att