2012-10-11 137 views
3

背景MIPS说明:SW

我目前工作的小MIPS计划,家庭作业,并在这个过程中学习一些语言。我对此非常陌生,因此即使是我正在执行的操作的最基本方面,我也很不确定自己。此外,我的讲师坚持在我们的作业中不使用伪代码,这导致了很难理解如何完成某些任务。

分配

我分配的问题是这样的:假设你想计算迭代(在一个循环中)第一个20个大不了的数字,B(i) = 2i + 17i和他们在一个数组B其基址在MIPS记忆顺序存储存储在寄存器$s0中。请编写MIPS代码(完整评论)来计算B(i), 1 < i < 20

我的解决方案

我目前有:

.globl main 
main: 

.data 
BiggieArray: 
    .space 80     #4 bytes per word * 20 biggie numbers = 80 bytes reserved 
.text 
addi $s1, $zero, 1   #$s1 tracks i, initialize i with value of 1 
addi $s2, $zero, 21  #$s2 holds 21, for use in comparing with the value of i 
addi $s3, $zero, 2   #$s3 holds the value for the first mult 
addi $s4, $zero, 17  #$s4 holds the value for the second mult 

STARTLOOP: 
beq  $s1, $s2, ENDLOOP  #compares i ($s1) with $s2. If they are equal, i > 20, so branch to the end of the loop. 

add  $t0, $s1, $s1  #find 2i 
add  $t0, $t0, $t0  #find 4i, use as offset for BiggieArray 
addi $t0, $t0, -4   #adjusts to start from index 0 instead of index 1 

mult $s1, $s3    #Calculates 2i 
mflo $s5    #$s5 holds 2i 
mult $s1, $s4    #Calculates 17i 
mflo $s6    #$s6 holds 17i 

add  $s7, $s5, $s6  #$s7 holds 2i+17i 

add  $t1, $s0, $t0  #$t1 holds the current address of BiggieArray[i] 

sw  $t1, 0($s7)   #stores the value 2i+17i into RAM ????? 

addi $s1, $s1, 1   #increment i 

j STARTLOOP 

ENDLOOP: 

我的问题

我意识到,我目前没有$s0初始化为任何事情,但是这不是什么给我问题。我感到困惑的是如何将2i+17i的值存回BiggieArray。任何帮助或非常简单的解释如何sw工程将大大赞赏。

回答

2

在你的例子中,你有寄存器倒过来。它应该是:

sw $s7, 0($t1) # since s7 holds the value you want to store, and t1 holds the mem address where you want to store it

+1

哇,那简单的切换?我必须再去一次!我很感激帮助。 –