【问题标题】:For Loop With Array in C to MIPS With Offset带有 C 中数组的 For 循环到带有偏移量的 MIPS
【发布时间】: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:

【问题讨论】:

  • 您应该在问题中包括如何在您的汇编代码中声明abc。您还应该在 C 代码中指定 abc 的类型,因为这很重要。

标签: c arrays loops for-loop mips


【解决方案1】:

据我所知,您每次都将它存储在同一个索引中。在 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

查看this tutorial 看看是否有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多