Assembly 如何将$t5存储到$t7中

Assembly 如何将$t5存储到$t7中,assembly,mips,Assembly,Mips,下面是我的代码: .data inputOne: .word 2 # Value 1 inputTwo: .word 3 # Value 2 counter: .word 0 # Adds the amount of times that we go through the loop sum: .word 0 # Where the result of the addition goes random: .word 0 .text main: lw $t2, input

下面是我的代码:

.data
inputOne: .word 2 # Value 1
inputTwo: .word 3 # Value 2
counter: .word 0  # Adds the amount of times that we go through the loop
sum: .word 0      # Where the result of the addition goes
random: .word 0


.text
main:

    lw $t2, inputOne  # Load 2 into register t2
    lw $t3, inputTwo  # Load 3 into register t3
    lw $t4, counter   # Load 0 into register t4
    lw $t5, sum       # Load 0 into register t5
    lw $t7, random
    topOfLoop:        # Start of the loop
    beq $t4, $t2, bottomOfLoop  # Until t4 is equal to t2, the loop will continue
    addi $t5, $t5, 3  # Adds 3 to register t5 ( Sum) 
    addi $t4, $t4, 1  # Adds 1 to register t5 (Counter)
    j topOfLoop       # Jumps to the top of the loop
    bottomOfLoop:     # End of the loop 
    sw $t7, 0($t5)
当我在MIPS中运行此操作时,会出现以下错误:

Exception occurred at PC=0x0040005c
Unaligned address in store: 0x00000006
谁能帮我一个忙,让我知道我做错了什么


谢谢

我不知道您想做什么,但是
sw$t7,0($t5)
读起来像是将
$t7
的值存储在地址
$t5+0
上。从前面的代码判断,
$t5
不是内存地址,而是标量值(求和的结果)


如果要将求和结果存储回由“sum”表示的内存位置,则应执行
sw$t5,sum

MIPS与大多数其他体系结构一样,不允许未对齐的访问。在您的情况下,在将
sum
地址加载到$t5后,将其与3相加,这会导致地址未对齐(如果之前是4的倍数,或者通常是与4n+1不同的任何值)。因此,将值存储到地址$t5会导致异常

lw $t5, sum       # Load 0 into register t5
...
addi $t5, $t5, 3  # Adds 3 to register t5 ( Sum) 
...
sw $t7, 0($t5)
如果要将新的计算值存储到$t7所指的地址,应该执行以下操作

sw $t5, 0($t7)
如果您想像标题所说的那样存储$t5到$t7,请将其添加为$0

add $t7, $t5, $zero
或者使用宏

move $t7, $t5

这正好扩展到上面的一个

感谢您的回复。我正在尝试将值从$t5移到内存中。我回答的第二部分是您当时正在寻找的内容?是的!我只是好奇,是否有办法将$t5中的值存储到另一个寄存器中,如$t7或$t8?@CPR:将其与$零相加,这是所有MIPS书籍中编写的基本值,它终于起作用了!谢谢你的帮助。。我很感激。@CPR:如果你觉得某个答案对你有用或对你有用,那就接受或投票吧