Marco
Marco

Reputation: 49

Why I have the error "store address not aligned on word boundary"

I have a problem with my MIPS code... I would check the occurrences in a string passed by keyboard without a character to compare. I have the string in the stack (stack -255 position) and an array in the .data section to store the occurrences. The basic idea is that I load a letter from the stack one-by-one with a loop (lb $t1, ($a0) t1= ascii code of the letter - a0= stack passed at the function), subtract the letters read from the stack with 97 (97=a) and obtain the index of the array and with another loop, count the occurrences and save this under the index calculated before. Obviously, the array has 104 position (26 letters * 4 because I would save the number of occurrences). The problem is that when I find the index of my array position and I would save inside the position the occurrences with sw $--, myArray($--) Mars give me this error: Runtime exception at 0x0040007c: store address not aligned on word boundary 0x10010087 I have tried to add .align in the .data section but I haven't find solution, maybe I wrong some things... Any help?

This is my code:

analizza_stringa: (function)

while_string:

# load a byte from the stack
lb $t0, ($a0)

#check end string
beq $t0, $zero, end 

#check a line feed
beq $t0, 10, end

    #array index
    subi $t3, $t0, 97

    #Multiply by 4 (I would save number)
    mul $t4, $t3, 4

    while_occorrenza:
         #like before
         beq $t1, 10, continue

         #load a letter like before
         lb $t1,($a0) 

         #Check the occurrences
         bne $t1, $t0, continue2 

         #add 1 at the occurrences
         addi $t5, $t5, 1

         continue2: 

         #add 1 for the pointer at the string
         addi $a0, $a0, 1

         #Repeat
         j while_occorrenza

    continue:

    #Save the number under the index calculated before
    sw $t5, myArray($t4)

    #counter=0 for another loop
    li $t3, 0   

#next character
addi $a0, $a0, 1

# repeat the first loop
j while_string

end:
jr $ra

Upvotes: 1

Views: 2891

Answers (1)

Michael
Michael

Reputation: 58447

If you want to access a word in memory, the address must be word-aligned (a multiple of 4).

Assuming that $t4 contains a multiple of 4, which it appears to do, that means myArray isn't word-aligned. To fix this you would add .align 2 on the line before myArray in your data section.

Upvotes: 1

Related Questions