因此,我有一个赋值问题,它要求将C中的for循环转换为MIPS。我的教授动作太快了,所以我听不懂他说的一半。以下是代码:
for (i=0; i<10; i++){
a[i] = b[i] + c[i];
}这个片段存储在内存中,从位置00000100十六进制开始。将此代码转换为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:发布于 2016-02-25 06:39:03
据我所见,您每次都将它存储在相同的索引中。在MIPS中,每4个字节是一个单词,所以您必须将其存储在0、4、8等位置。此外,在开始使用数组之前,您需要为数组分配内存。下面是一个例子。
li $v0, 9 # create an array, start address in $v0
li $a0, 80 # allocate 80 bytes, or 20 words
syscall
move $t0, $v0 # move from $v0 (temp) to $t0查看一下本教程,看看它是否有帮助。
https://stackoverflow.com/questions/35619639
复制相似问题