Reputation: 1062
I am trying to take 4 numbers from the keyboard and store it to an array. I have come up with the following code however when I try and print the first number in the array it prints 0. Why is it not printing the first value in the array?
main:
li $v0, 5 # read integer
syscall
add $s0, $v0, $zero #store input1 to s0
li $v0, 5 # read integer
syscall
add $s1, $v0, $zero #store input2 to s0
li $v0, 5 # read integer
syscall
add $s2, $v0, $zero #store input3 to s0
li $v0, 5 # read integer
syscall
add $s3, $v0, $zero #store input4 to s0
.data
list: $s0, $s1, $s2, $s3 #set array from keyboard values
.text
#get the array into a register
la $t3, list # put address of list into $t3
lw $t4, 0($t3) # get the value from the array cell
li $v0, 1 # print integer
move $a0, $t4 # what to print is stored at $s1
syscall # make system call
exit:
li $v0, 10 # exit system call
syscall
Idea taken from: http://www.cs.pitt.edu/~xujie/cs447/AccessingArray.htm But they are using hard-coded ints instead of values saved to registers from keyboard. Thanks for the help.
Upvotes: 0
Views: 14500
Reputation: 2692
This:
.data
list: $s0, $s1, $s2, $s3 #set array from keyboard values
.text
does not work this way. You need sw
to store the values of registers to memory.
So, at the start of the program, reserve some space to store the values:
.data
list: .space 16
.text
Then, after you read the values, store them with sw
:
sw $s0, list
sw $s1, list + 4
sw $s2, list + 8
sw $s3, list + 12
Upvotes: 2