I want to read space separated integers in mips assembler. I currently do:
li $v0, 5
syscall
move $t4, $v0
In a loop but it requires to make input with endline. I want to just put many space separated integers and make it read them. How can I do it?
I had this problem and I solved it using the following function:
get_integer:
li $t1, 10 # t1 = newLine (in ascii)
li $t0, 32 # t0 = space (in ascii)
li $v1, 0 # v1 = 0
read_int_loop:
li $v0, 12 # Read next char
syscall
beq $v0, $t0, read_int_done # if new character == space, end loop
beq $v0, $t1, read_int_done # if new character == newLine, end loop
addi $v0, $v0, -48 # Convert ascii to int
sll $t2, $v1, 3 # t2 = v1 * 8
sll $v1, $v1, 1 # v1 *= 2
add $v1, $v1, $t2 # v1 += t2 -> original v1 *= 10
add $v1, $v1, $v0 # v1 += v0 -> output = original v1 * 10 + v0
j read_int_loop # loop
read_int_done:
jr $ra # return (v1)
What this function does is:
This process ends when either a white space or a new line is entered.
And of course, the function can be called with jal
to substitute the regular integer input way that OP has mentioned.