简体   繁体   English

对于C中具有阵列的循环到具有偏移的MIPS

[英]For Loop With Array in C to MIPS With Offset

So I have an assignment problem which asks for a for loop in C to be converted into MIPS. 所以我有一个分配问题,要求将C中的for循环转换为MIPS。 My professor moves extremely fast so I couldn't catch half of what he said. 我的教授动作非常快,所以我听不懂他说的一半。 Here is the code: 这是代码:

for (i=0; i<10; i++){
    a[i] = b[i] + c[i];
}

This fragment is stored in memory starting from location 00000100 Hex. 此片段从位置00000100 Hex开始存储在内存中。 Convert this code to MIPS and provide numeric offsets for each branch or jump instruction used. 将此代码转换为MIPS,并为每个使用的分支或跳转指令提供数字偏移量。

I don't quite understand the use of offsets. 我不太了解偏移量的用法。 From the lecture slides given to us, it seems load word and store word commands are used for the offsets, which are also used for the arrays but I'm not sure how to go about it. 从给我们的讲座幻灯片中,似乎可以将加载字和存储字命令用于偏移量,这些偏移量也用于数组,但是我不确定该如何处理。 Below is something I put together based on other solutions I saw, but am of course open to changes. 以下是我根据看到的其他解决方案整理的内容,但是当然可以进行更改。 I'm hoping it's at least going in the right direction. 我希望至少朝着正确的方向发展。 Any help would be appreciated. 任何帮助,将不胜感激。

#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:

From what I can see, you are storing it in the same index each time. 据我所知,您每次都将其存储在同一索引中。 In MIPS, every 4 bytes is a word, so you must store it in 0, 4, 8, etc. Also, you need to allocate memory for your array before you start using one. 在MIPS中,每4个字节是一个字,因此您必须将其存储在0、4、8等中。另外,在开始使用一个数组之前,需要为数组分配内存。 Here's an example. 这是一个例子。

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

Check out this tutorial and see if it helps at all. 查看本教程 ,看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM