Remember that branch and jump instructions on MIPS have delay slots. That is, the instruction directly following a branch or jump instruction is always executed.
It's possible that your assembler (gas?) is taking care of this for you by inserting NOPs
or re-ordering instructions. You could find out by disassembling the binary with objdump. If the assembler doesn't fix this for you, the easiest way to fix it by yourself is to insert a NOP
after each branch/jump.
The other thing that stands out is that you always reload $a1
with the address of array
before you read a new number. This means that every number you read will be stored at array[0]
, including the terminating 0. So your print loop will find 0 as the first value in the array and exit immediately.
You could fix that by changing the code to something like this:
main:
la $a1, array
read_numbers:
# Rest of code omitted for brevity...
beqz $v0, sort
j read_numbers