【发布时间】:2017-02-21 01:53:56
【问题描述】:
我有一个需要 MIPS 帮助的 C 代码。 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”来抵消加载字和存储字。因为据我所知 load word 只使用静态值。
【问题讨论】:
-
您可以在每个循环中将 s0,s1,s2...增加一个数组项的大小。或者复制它们并增加它们,这样你就不会丢失原始起始地址。这适用于大多数指令集,包括这个。