I am writing a program that reads 4 integers to an array from the user in an ascending order. If the values are not ascending the program prints an error message ("bigger" label in .data) and reads another integer from the user. After reading the integers, I iterate and print them with comma after each integer.
When I call the comma print, I get a space + the value of the "bigger" label (the error message) instead of comma. Code:
.data
array: .byte 4
comma: .asciiz ",\n"
bigger: .asciiz "The array inputs should be in an ascending order!\n"
.text
main: # main program entry
la $a0, array # load array address
jal get_array # call get_array procedure with the array address
addi $t0, $zero, 0 # set loop counter to 0
la $t1, array # load array address
loop: beq $t0, 4, endLoop # for $t0 < 4
li $v0, 1 # v0 integer syscall
lb $a0, 0($t1) # load current byte of the array
syscall # print it
la $a0, comma # load address of comma string
li $v0, 4 # v0 string syscall
syscall # print comma
addi $t1, $t1, 1 # array address pointer++
addi $t0, $t0, 1 # loop counter++
j loop # loop
endLoop:
li $v0, 10 # exit program
syscall
get_array: # read asceding integer array from the user
addi $t0, $zero, 1 # inserts count
la $t1, ($a0) # array address pointer
addi $v0, $zero, 5 # read integer syscall
syscall # read integer from the user
sb $v0, 0($t1) # save integer in current array address pointer
addi $t1, $t1, 1 # array address pointer++
loop1: beq $t0, 4, endLoop1 # for $t0 < 4
addi $v0, $zero, 5 # read integer syscall
syscall # read integer from the user
lb $t2, -1($t1) # read the previous array value
slt $t3, $t2, $v0 # check if current value is bigger than previous value
beq $t3, 0, invalid # if not go to invalid
valid: sb $v0, 0($t1) # valid - save new value in current array address pointer
addi $t1, $t1, 1 # array address pointer++
addi $t0, $t0, 1 # inserts count++
j loop1 # loop
invalid: # invalid - new value doesnt keep array ascending
addi $v0, $zero, 4 # print string syscall
la $a0, bigger # print error message
syscall # print error message
j loop1 # loop
endLoop1:
jr $ra # return to caller
Example: for the input:
1
2
3
4
The print is:
1 The array inputs should be in an ascending order!
2 The array inputs should be in an ascending order!
3 The array inputs should be in an ascending order!
4 The array inputs should be in an ascending order!
Expected result:
1,
2,
3,
4