I am trying to print on the console the values of the array, which are already given as intA in .data. I.e., trying to print values of an array without user's prompt.
My code:
.data
prompt: .asciiz "The values in the array are:\n"
finished: .asciiz "\nNo more values to present"
space: .asciiz " "
intA: .word 11, 2, 3, 4, 5, 34, 0
.text
.globl main
main:
#Prints the prompt string
li $v0, 4
la $a0, prompt
syscall
# initialization of a0, a1, and t3 (i, counter)
la $a0, intA # loading starting address (base) of array in register a0
addi $a1, $zero, 6 # array size - 1
addi $t3, $zero, 0 # i initialized to 0
j loop
loop:
lw $t1, 0($a0) # loading integer (value of array) in the current address to register t1, I use lw because integer is a word (4 bytes)
# printing current value of array
li $v0, 4
la $a2, ($t1)
syscall
# spacing between values
li $v0, 4
la $a2, space
syscall
# checking that next address is not outside of the array
addi $t3, $t3, 1
slti $t2, $t3, 6
bne $t2, 1, done
# accessing next integer and jumping back to print it
addi $a0, $a0, 4
j loop
done:
# indicating program is done
li $v0, 4
la $a0, finished
syscall
The output that I am getting: output
Any idea why it doesn't print the values of the array, and also what are these squares that are printed instead?
Edit:
I changed
# printing current value of array
li $v0, 4
la $a2, ($t1)
syscall
to
# printing current value of array
li $v0, 1
lw $a2, ($t1)
syscall
Because, from what I understand, I print an integer so $v0 should be fed 1, and I should lw and not la (because it is an integer, i.e, a word)
However, now I get a runtime error in line 31: lw $a2, ($t1)
Telling me that
fetch address not aligned on word boundary 0x0000000b
Solution: Instead of lw $a0, ($t1)
for printing the value of $t1
, I need to do add $a0, $t1, $zero
, because I am trying to use the value and not to access an address.
User contributions licensed under CC BY-SA 3.0