以下是我的代码:
.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
它完全扩展到上面的那个