-1

I'm a first time MIPS MARS user and keep getting this error:

Error line 19: Runtime exception at 0x00400024: store address not aligned on word boundary 0x00000002

This is the code I'm using:

.data
 str: .ascii "abcdefgh"
 array: .space 20
 .text
 main:
 li $s0, 1 # a = 1
 li $s2, 1 # b = 1
 loop:
 la $t1, array
 slti $t0, $s0, 3 # t0<- 1 if a < 3
 beq $t0, $zero, exit
 sll $t0, $s0, 2 # t1<- 4*a
 add $t1, $t1, $t0 # new base addr
 add $t2, $s2, $s0 # t2<- a+b
 sw $t1, 0($t2) # D[a]=a+b
 addi $s0, $s0, 1 # a = a +1
 j loop # goes to loop: label
 exit:
 li $v0, 10 # v0<- (exit)
 syscall

Can someone explain why this keeps happening?

Updated Code:

.data
str: .ascii "abcdefgh"
 array: .space 20
 .text
 main:
 li $s0, 1 # a = 1
 li $s2, 1 # b = 1
 loop:
 la $t1, array
 slti $t0, $s0, 3 # t0<- 1 if a < 3
 beq $t0, $zero, exit
 sll $t0, $s0, 2 # t1<- 4*a
 add $t1, $t1, $t0 # new base addr
 add $t2, $s2, $s0 # t2<- a+b
 sw $t1, 2($t2) # D[a]=a+b
 addi $s0, $s0, 1 # a = a +1
 j loop # goes to loop: label
 exit:
 li $v0, 10 # v0<- (exit)
 syscall

but now I'm getting this error:

line 19: Runtime exception at 0x00400024: address out of range 0x00000004

How can I fix this?

  • It would help to know what instruction the error refers to. – Scott Hunter Sep 02 '16 at 19:08
  • line 19: Runtime exception at 0x00400024: address out of range 0x00000004 – Asian_Panda Sep 02 '16 at 19:16
  • That has already been established; which is line 19? – Scott Hunter Sep 02 '16 at 21:38
  • Line 19 is sw $t1, 2($t2) # D[a]=a+b – Asian_Panda Sep 03 '16 at 15:55
  • 1
    To ensure alignment of `array` by the assembler, place it _before_ `str`. `str` has 8 chars [making `array` word aligned by chance]. If `str` had (e.g.) 9 chars, `array` would be unaligned (i.e. `sw $zero,array` would be unaligned and crash). I usually place all data that needs alignment first in the `.data` segment, followed by the `.ascii/.asciiz`. Or, put `.align 4` just before `array:`. – Craig Estey Sep 03 '16 at 17:59
  • Does this answer your question? [Error: Runtime exception ... store address not aligned on word boundary](https://stackoverflow.com/questions/45174399/error-runtime-exception-store-address-not-aligned-on-word-boundary) – Peter Cordes Mar 27 '20 at 07:59

1 Answers1

2

Words have to be located at addresses which are multiples of 4; you are using one (0x00000002) which is not.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101