MIPS - Storing ints In Array From User Input

Philip Rego picture Philip Rego · Apr 14, 2013 · Viewed 45.9k times · Source

I'm trying to store user input into an array, but when I use sw I get an error "store address not aligned on word bound". My goal is to read in 10 integers from the array, but after I input the first digit I get an error at the sw command. I don't know what I'm doing wrong I spent a couple hours trying to figure it out. Any help will be greatly appreciated and marked useful.

        .data 

mess: .asciiz " Enter 10 numbers to be stored in the array. "
array: .space 40    #10 element integer array
    .globl main
    .text 
main:
    jal read
    b done
read:
    la $t0, 0   #count variable
    b readLoop
    jr $ra

readLoop:
    beq $t0, 40, read   #branch if equal to 40, 10 items
    li $v0, 4       #Print string
    la $a0, mess        #load prompt
    syscall
    li $v0, 5       #read int
    syscall 
    sw $v0, array       #store input in array ERROR HERE
    addi  $t0, $t0, 4   #add by 4 to count
    b readLoop
print:

done:

This worked for me. I don't know why it doesn't work above

    .data 
list:  .space 16
.globl main
.text

main:

    li $v0, 5
    syscall
    sw $v0, list

    move $a0, $v0
    li $v0, 1
    syscall

Answer

anon picture anon · Oct 28, 2014

Try allocating space for your array before you allocate space for your string in the data segment:

  array: .space 40    #10 element integer array
  mess: .asciiz " Enter 10 numbers to be stored in the array. "

if you allocate the string first the array might start at an address that is not divisible by 4 and lead to a word alignment error