Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS - Storing ints In Array From User Input

Tags:

assembly

mips

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
like image 785
Philip Rego Avatar asked Apr 14 '13 05:04

Philip Rego


3 Answers

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

like image 184
anon Avatar answered Sep 21 '22 00:09

anon


  • The store should be

    sw $v0, array($t0)

  • Replace la $t0, 0 by li $t0, 0

  • Set the array above mess

Furthermore, when you reach 10 items, you restart the reading and overwrite the previous values.

like image 33
Patrik Avatar answered Sep 20 '22 00:09

Patrik


Proper Array Input Code

.data
    myarray:.space 40    
    st:.asciiz "Enter the 10 Elements"

.text    
    li $v0,4
    la $a0,st
    syscall
    jal fun
    li $v0,10
    syscall

fun:        
    li $v0,5
    syscall
    beq $t0,40,exit
    sw $v0,myarray($t0)
    add $t0,$t0,4
    j fun

exit:
    jr $ra
like image 29
Programming By AB Avatar answered Sep 20 '22 00:09

Programming By AB