【问题标题】:MIPS Explanation: swMIPS 解释:sw
【发布时间】:2012-10-11 06:21:58
【问题描述】:

背景

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

作业

我的作业问题是这样的:假设您要迭代地(在循环中)计算前 20 个 Biggie 数字 B(i) = 2i + 17i 并将它们顺序存储在数组 B 中,该数组在 MIPS 内存中的基地址存储在寄存器 @ 987654323@。请编写 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 工作原理的任何帮助或非常简单的解释。

【问题讨论】:

    标签: memory assembly mips mips32


    【解决方案1】:

    在您的示例中,您将寄存器颠倒了。应该是:

    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

    【讨论】:

    • 哇,这么简单的转换?我得再试一次!感谢您的帮助。
    猜你喜欢
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2017-07-02
    • 2021-10-31
    • 2018-06-06
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多