MIPS "Unaligned address, Exception 5" error

Israel picture Israel · Jan 3, 2013 · Viewed 7.6k times · Source

I'm a noob using SPIM MIPS simulator.
I get the error in title X 26 times, when I try to initialize an array of 26 words to 0. I've isolated the problem to be the store word operation sw $t0, 0($s3), but have no clue what am I doing wrong.

The code:

.data  
theArray: .space 104  
theArraySz: .word 26  
.text  
.globl main  
main:  
move    $t0, $zero  
la      $s3, theArray  
lw      $s4, theArraySz      
add     $t2, $zero  
initLoop:  
beq     $t2, $s4, initEnd       
sw      $t0, 0($s3)    
addi    $s3, $s3, 4        
addi    $t2, $t2, 1        
j       initLoop   
initEnd:        
jr $ra

Answer

unwind picture unwind · Jan 3, 2013

Make sure the address of theArray is aligned to a 32-bit word boundary. You can inspect the address if you have the ability to single-step through the program, and check the value of $s3 after the first la instruction.

See this wiki for documentation about alignment, and the .align directive that can be used to force alignment.