error: "store address not aligned on word boundary"

Favolas picture Favolas · Mar 22, 2012 · Viewed 25.8k times · Source

I'm using MARS MIPS simulator and using the Digital Lab Sim.

The purpose of my code is to show the numbers 0 to 15 in hexadecimal on the Digital Lab Sim.

I'm getting this error

Runtime exception at 0x00400024: store address not aligned on word boundary 0xffff0011

This is the code I'm using:

        .data
digitos:    .word 0x3F,0x6,0x5B,0x4F,0x66,0x6D,0x7D,0x7,0x7F,0x67,0x77,0x7F,0x39,0x3F,0x79,0x71
contador:   .word 16
        .text

main:

    la  $t0,0xFFFF0011  

    la  $a0,contador    
    lw  $t1,0($a0)

    li  $t9,0       


    la  $t2, digitos    



loop:
    lw  $t3,0($t2)
    sw  $t3, 0($t0)         
    addi    $t9,$t9,1   
    addi    $t2,$t2,4   

    blt     $t9,$t1,loop

The instruction la $t0,0xFFFF0011 is the one responsible by the error. 0xFFFF0011 controls the left led and 0xFFFF0010 controls the right led.

Here is the funny part. If I use 0xFFFF0010 the code works as expected but if I use the 0xFFFF0011 it does not work.

What am I doing wrong?

Answer

Jeff picture Jeff · Mar 22, 2012

Are you sure that the error is happening on that line? I think the error is actually happening here:

sw  $t3, 0($t0)

The problem is that you're trying to store a word (because you're using sw) to an address that's not word-aligned. 0xFFFF0011 is not word-aligned. The reason why 0xFFFF0010 works is because it is word-aligned.

A word is 4 bytes long, so the valid word-aligned addresses are 0xFFFF0010, 0xFFFF0014, 0xFFFF0018, etc. Anything in between isn't word-aligned.

You should be able to fix this by changing it from sw to sb:

sb  $t3, 0($t0)

This works because storing a byte does not require a word-aligned address.

Edit: To clarify, a word-aligned address is one that's divisible by 4.