背景
我目前正在为家庭作业编写一个小的MIPS程序,并且在学习一些语言的过程中。我对此非常陌生,因此,当涉及到我正在执行的操作的最基本方面时,我对自己非常不确定。此外,我的老师坚持在作业中不使用伪码,这给我们理解如何完成某些任务带来了很大的困难。
的任务
我的赋值问题是这样的:假设您想迭代地(在一个循环中)计算前20个Biggie数,B(i) = 2i + 17i并将它们顺序存储在一个数组B中,该数组B中的基地址存储在寄存器$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如何工作将不胜感激。
发布于 2012-10-11 06:52:31
在您的例子中,您的寄存器被反转。它应该是:
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
https://stackoverflow.com/questions/12833386
复制相似问题