我有一个C代码,我在MIPS中需要帮助。 C代码如下。
for (i=0; i<100; i++){
a[i] = b[i] + c[i] - d[i];
}
我已将此转换为MIPS,但不知道将什么放入加载字的偏移量。
addi $t0, $zero, 0 #i = 0
for_loop:
bgt $t0, 100, for_loop_done #i <100
addi $t0, $t0, 1 #i++ or i = i+1
lw $t4, __($s0) # load a in t4
lw $t1, __($s1) # load b in t1
lw $t2, __($s2) # load c in t2
add $t4, $t2, $t1 # add b with c and store in a
lw $t3, __($s3) # load d in t3
sub $t4, $t3, $t4 # sub contents of a from d
sw $t4, __($s0) # store contents of t4 into a
j for_loop # go to start of loop
for_loop_done:
我们假设a,b,c,d分别在s0,s1,s2,s3,s4中。现在代码需要的是我们如何通过c代码中不断变化的“i”来偏移加载字和存储字。因为据我所知,加载词只使用静态值。
答案 0 :(得分:1)
数组在内存中以最低的基址排列,并以4字节的偏移量索引到下一个元素。[图片来自Harris D. M.,Harris S. L. - 数字设计与计算机体系结构,第2版 - 2012]
for_loop:
bgt $t0, 100, for_loop_done #i <100
addi $t0, $t0, 1 #i++ or i = i+1
lw $t4, 0($s0) # load a in t4
lw $t1, 4($s1) # load b in t1
lw $t2, 8($s2) # load c in t2
add $t4, $t2, $t1 # add b with c and store in a
lw $t3, 12($s3) # load d in t3
sub $t4, $t3, $t4 # sub contents of a from d
sw $t4, 0($s0) # store contents of t4 into a
j for_loop # go to start of loop
for_loop_done: