user2908001
user2908001

Reputation:

MIPS: store address not aligned on word boundary

I'm trying to write a program which works out the the number of non space characters, characters and words in a sentence using MIPS assembly language. I have succeeded in doing this but I have little issue right at the end.

I have got the values of these things like number of spaces in random registers and am trying to save them into 3 registers next to one another ($s1, $s2, $s3) but when I run my program (using Mars) I get the following error: Runtime exception at 0x00400070: store address not aligned on word boundary 0x0000001e

I will post my code below. Thanks in advance for any any answers!

    .data 

char_count: .word 0 

space_count: .word 0 

word_count: .word 0 

text_input: .asciiz "I'm having an old friend for dinner." 

    .text  
    .globl main

main:
la $s0, char_count        #t1 storing char_count address         

la $t0, text_input # Loading the text
li $t1, 0 # Making the character counter 0
addi $t5, $t5, 0x20 # Loading up the hex value for space
addi $t8, $t8, 0 # Making the register which decides whether or not the characters     (spaces/letters) before the space were a word.
addi $t9, $t9, 1 # Used for comparing the $t8 register to
addi $s4, $s4, 0 # Used for camping the $t8 register to

count:  
lb $t2, 0($t0)  # Load the first byte from address in $t0  
beqz $t2, end   # If the byte is 0 then jump to the end of the program
beq $t2, $t5, space # If the byte is a space then jump to the part of the program which     adds one to the space counter
beq $t8, $t9, continue   # If the word deciding register equals 1 then skip to the         continue part (to stop $t8 getting bigger than 1).
addi $t8, $t8, 1 # Add 1 to the register to indicate that a letter was the last byte.
continue:
add $t1, $t1, 1 # Increment the counter
add $t0, $t0, 1      # Increment the address  
j count      # This then loops this part of the program  

space:
addi $t4, $t4, 1 # Add 1 to the space counter
beq $t8, $s4, continue # If word deciding register equals 0 then jump to continue
sub $t8, $t8, $t9 # Make word deciding register ($t8) go back to 0
addi $t3, $t3, 1 # Add 1 to word counter
jal continue    

end:
beq $t8, $s4, finish
addi $t3, $t3, 1 # If last byte in the string was a character than another word must be         added. This stops spaces at the end being counted as a word.

finish:

sub $t6, $t1,$t4 # This works out the number of non space characters by doing number of     characters minus spaces.

sw $s1, ($t6)           # Trying to store number of non spaces in s1
sw $s2, ($t4)            #  Trying to store number of spaces in s2
sw $s3, ($t3)            # trying to store number of words in s3

li $v0, 10
syscall # Terminates the program.

Upvotes: 1

Views: 5910

Answers (1)

Michael
Michael

Reputation: 58507

The way you're using sw doesn't match what you seem to be trying to do:

sw $s1, ($t6)           # Trying to store number of non spaces in s1

What that line actually does it store the word contained in $s1 at the location in memory pointed to by $t6.

If you just wanted to copy the contents of $t6 into $s1 you should use:

move $s1, $t6

And if you wanted to store $t6 in memory at the location pointed to by $s1 you should use:

sw $t6, ($s1)

Keep in mind that in this case you need to place a valid address in $s1 first; e.g. la $s1, char_count.

Upvotes: 1

Related Questions