将数组中的值存储到寄存器(Assembly)中

时间:2013-11-10 17:21:44

标签: arrays assembly x86-64 long-integer

我有一个longs数组(大小为4),我想将这4个值中的每一个存储到它自己的独立寄存器中。但我不知道该怎么做,因为我从未在汇编中使用过数组。例如,我想将array[0]存储到r9,将array[1]存储到r10等等。有人可以帮助我这样做吗?到目前为止,这是我的代码。 lhs是指向数组的指针。

define(lhs,%rdi)
define(rhs_d,%esi)
define(rhs,%rsi)
define(result,%rdx)

.text


.globl addBignumInt
    .type   addBignumInt, @function
addBignumInt:
.LFB18:
    .cfi_startproc
    # code goes here
    movslq  rhs_d, rhs

    .cfi_endproc
.LFE18:
    .size   addBignumInt, .-addBignumInt

所以,基本上我想做的是     movq lhs[0], %r9 但显然这是不正确的

1 个答案:

答案 0 :(得分:0)

您需要将指向数组的指针(其开头)偏移n次sizeof(type)。

所以你会做这样的事情:

mov rax, arrBegin ; rax points to the array of longs

然后以下内容将在rbx中存储第二个元素:

mov rbx, [rax + 4]

或者,如果你想访问第n个元素(可能在循环中,即使你需要手动扩展,如果你总是想要更改目标寄存器),那么:

psuedo-code(你可以这样做,或者使用乘法加法寻址模式或辅助寄存器):

mov dest, [rax + n * 4]  

其中n是辅助或缩放{2,4,8}(寻址模式的一部分)。