如何在MIPS中的数组地址中添加+4

时间:2014-09-22 06:50:50

标签: mips

我正在尝试使用循环将'2'整数存储到数组中。所以我想我会使用一个循环,持续2次,每次它在Array1 + cngAdd中存储一个数字,我将在开始时将cngAdd初始化为0,并在循环结束时将其增加4。所以在循环的第二次迭代中,地址加4。

所以要读取输入:

li $t7, 2
li $t6, 1

intReadStore:   

li  $v0, 5
syscall
sw $v0, Array1 + cngAdd

lw $t0, cngAdd
li $t0, $t0, 4
sw $t0, cngAdd

li $t6, $t6, 1
ble $t6, $t7, intReadStore

但在qtspim中,我在此声明中收到错误:sw $ v0,Array1 + cngAdd

请问你应该告诉我应该使用什么而不是cngAdd(这样我可以使用+ 4更改地址,使用某个变量,甚至使用任何通用整数寄存器,而无需硬编码“4”)?

注意:我的数据是:

.data
 cngAdd    .word    0
 Array1    .space   2

我的Array1在这里是2个整数,但我想为'n'做这个。但是对于这个问题,我认为2就足够了。

感谢。

1 个答案:

答案 0 :(得分:1)

将基址放在寄存器中,并在每次循环迭代时增加一次:

la $a3,Array1    # Put the address of Array1 in register $a3

intReadStore:   

li  $v0, 5
syscall
sw $v0, ($a3)    # Store the value at the currently pointed to element of Array1

addiu $a3,$a3,4  # Point to the next word in Array1

执行$a3时,您可能需要保存/恢复syscall,因为$a3的值可能无法保留。


顺便说一下,就我所知,size的{​​{1}}参数在 bytes 中。因此,对于2个字,您需要保留8个字节的空间。