How do I get $t5 to store into $t7

2

Below is my code:

.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)

When I run this in MIPS, I get the errors:

Exception occurred at PC=0x0040005c
Unaligned address in store: 0x00000006

Can anyone help by letting me know what I'm doing wrong?

Thanks

assembly
mips
asked on Stack Overflow Sep 19, 2013 by GirlWhoCodes • edited Sep 19, 2013 by Jens Björnhager

2 Answers

2

I'm not sure what you're trying to do, but sw $t7, 0($t5) reads like store the value of $t7 at the address $t5 + 0. Judging from your preceding code, $t5 is not a memory address, but a scalar value (the result of a sum).

If you want to store the result of the sum back into the memory location denoted by "sum", then you should do sw $t5, sum.

answered on Stack Overflow Sep 19, 2013 by Catalin P • edited Mar 12, 2019 by phuclv
1

MIPS, like most other architectures, doesn't allow unaligned access. In your case, after loading the sum address to $t5, you add it with 3, which cause the address to be misaligned (if it was a multiple of 4 before, or generally, any value that is different from 4n + 1). So storing a value to the address $t5 cause an exception

lw $t5, sum       # Load 0 into register t5
...
addi $t5, $t5, 3  # Adds 3 to register t5 ( Sum) 
...
sw $t7, 0($t5)

If you want to store the new calculated value to the address pointed by $t7, you should do

sw $t5, 0($t7)

If you want to store $t5 to $t7 like the title says, add it with $zero

add $t7, $t5, $zero

or use the macro

move $t7, $t5

which expands exactly to the one above

answered on Stack Overflow Sep 19, 2013 by phuclv

User contributions licensed under CC BY-SA 3.0