简体   繁体   中英

MIPS Assembly program not outputting the correct integers

So, my MIPS program has the user enter 3 integers and order them from least to greatest and print them out. My code is as follows:

.data
prompt: .asciiz "Please enter an integer: "

.text
main:
li $v0, 4
la $a0, prompt
syscall

li $v0, 5
syscall
move $s0, $v0


li $v0, 5
syscall
move $s1, $v0


li $v0, 5
syscall
move $s2, $v0


jal order3


li $v0, 1
move $a0, $s0
syscall

li $v0, 1
move $a1, $s1
syscall

li $v0, 1
move $a2, $s2
syscall

li $v0, 10
syscall

swap:
move $t0, $s0
move $s0, $s1
move $s1, $t0
jr $ra

swap1:
move $t0, $s1
move $s1, $s2
move $s2, $t0
jr $ra

order3:
bgt $s0, $s1, swap
bgt $s1, $s2, swap1
bgt $s0, $s1, swap
jr $ra

The problem is the program is only printing out the first number I enter. For example, if I enter 60,50,70 as my 3 inputs, it is outputting 60 three times. I do not know why this is doing this. Im guessing it has something to do with my order3 function or with the way i am outputting these values. Any help would be appreciated.

Thanks.

You should always use register $a0 to print an integer through syscall.

li $v0, 1
move $a0, $s0
syscall

li $v0, 1
move $a0, $s1
syscall

li $v0, 1
move $a0, $s2
syscall

And it seems that brunch (bgt) does not save the pointer to return. Therefore it is better to use this type of jumps.

order3:
blt $s0, $s1, next
move $t0, $s0
move $s0, $s1
move $s1, $t0

next:
blt $s1, $s2, next2
move $t0, $s1
move $s1, $s2
move $s2, $t0

next2:
blt $s0, $s1, fin
move $t0, $s0
move $s0, $s1
move $s1, $t0

fin:
jr $ra

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM