gpt4 book ai didi

memory - MIPS 说明 : sw

转载 作者:行者123 更新时间:2023-12-04 17:22:48 28 4
gpt4 key购买 nike

背景

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

作业

我的作业问题是这样的:假设你想迭代地(在一个循环中)计算前 20 个 Biggie 数字,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 工作原理的帮助或非常简单的解释都将非常感激。

最佳答案

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

sw $s7, 0($t1) # 因为 s7 保存了你想要存储的值,而 t1 保存了你想要存储它的内存地址

关于memory - MIPS 说明 : sw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12833386/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com