我有一个 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”来抵消加载字和存储字。因为据我所知加载词只使用静态值。
最佳答案
数组按内存中的最低基地址排列,并通过 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:
关于C 到 MIPS 的转换。数组加载字偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357326/