2014-09-22 57 views
0

我正在尝试使用一个循环来将数字“2”存储到数组中。所以我想我会使用循环2次,并且每次它在Array1 + cngAdd中存储一个数字,我将在开始时将cngAdd初始化为0,并在循环结束时将其增加4。所以在循环的第二次迭代地址由4如何在MIPS中的阵列地址中添加+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我得到这个说法错误:西南$ V0,数组1 + cngAdd

请问我应该用什么来代替cngAdd(这样我用+4改变地址,使用一些变量,或者甚至使用任何通用的整数寄存器,不用硬编码“4”)?

注:我的数据是:

.data 
cngAdd .word 0 
Array1 .space 2 

我的数组1是2点的整数这里,但我想这样做的“N”。然而对于这个问题2我认为是足够的。

谢谢。

回答

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论据.space字节据我所知。所以对于2个字你需要保留8个字节的空间。