【发布时间】:2016-02-25 06:07:50
【问题描述】:
所以我有一个分配问题,要求将 C 中的 for 循环转换为 MIPS。我的教授动作非常快,所以我听不懂他说的一半。代码如下:
for (i=0; i<10; i++){
a[i] = b[i] + c[i];
}
此片段存储在内存中,从位置 00000100 Hex 开始。 将此代码转换为 MIPS 并为使用的每个分支或跳转指令提供数字偏移量。
我不太了解偏移量的使用。从给我们的讲座幻灯片中,似乎加载字和存储字命令用于偏移量,它们也用于数组,但我不知道如何去做。以下是我根据我看到的其他解决方案汇总的内容,但我当然愿意接受更改。我希望它至少朝着正确的方向发展。任何帮助将不胜感激。
#t0 = i
#s0 = a
#s1 = b
#s2 = c
#t3, t4, t5, t6, t7 = free
loop:
bgt $t0,9,exit #exit before i reaches 10
addi $t3,$s1,$t0 #temp reg $t3 = address of b[i]
addi $t4,$s2,$t0 #temp reg $t4 = address of c[i]
lw $t5,0($t3) #temp reg $t5 = c[i]
lw $t6,0($t4) #temp reg $t6 = a[i]
add $t3,$t5,$t6 #temp reg $t10 = b[i] + c[i]
addi $t7,$s0,$t0 #temp reg $t7 = address of a[i]
sw $t3,0($t7) #store word a[i] = b[i] + c[i]
addi $t0,$t0,1 #increment i by 1
j loop #jump to start of loop
exit:
【问题讨论】:
-
您应该在问题中包括如何在您的汇编代码中声明
a、b和c。您还应该在 C 代码中指定a、b和c的类型,因为这很重要。
标签: c arrays loops for-loop mips