I'm trying to get the user input (input=$v0) and then compare it to 10 (10=$t1). If the input is less than ten, I want to print '<'. If the input is greater than ten, I want it to print '>'. I've tried a few different things but for some reason it ends up printing both '<' and '>'. As well as an error reading "program is finished running (dropped off bottom)" Could anyone tell me what it is that I'm doing wrong?
#where values are initialized
addi $t1, $zero, 10 #number for comparison
addi $t1, $zero, 60 #< less than
addi $t2, $zero, 62 #> greater than
#Where things happen
addi $v0, $zero, 5 # syscall 5 is to read integer syscall
syscall #get input from keyboard
blt $v0, $t1, less #go to less if less than 10
bgt $v0, $t1, great #go to great if greater than 10
less: #if input is less than 10
addi $v0, $zero, 11 #print
add $a0, $t1, $zero #copy $v0 to print
syscall #call for print
great: #if input is greater than 10
addi $v0, $zero, 11 #print
add $a0, $t2, $zero #copy $v1 to print
syscall #call for print
You need to finish your program somehow. If, for example, the code jumps to the less:
lable, since you don't have any returns or a jump to a routine that finishes the program, the execution just continues straight down the great:
part.
So you should make another lable end:
where you exit from the program. And you should jump to the end:
after executing the syscalls in less:
and great:
.
#where values are initialized
addi $t1, $zero, 10 #number for comparison
addi $t1, $zero, 60 #< less than
addi $t2, $zero, 62 #> greater than
#Where things happen
addi $v0, $zero, 5 # syscall 5 is to read integer syscall
syscall #get input from keyboard
blt $v0, $t1, less #go to less if less than 10
bgt $v0, $t1, great #go to great if greater than 10
#jump to end: (since there can also be an equal case)
less: #if input is less than 10
addi $v0, $zero, 11 #print
add $a0, $t1, $zero #copy $v0 to print
syscall #call for print
#jump to end:
great: #if input is greater than 10
addi $v0, $zero, 11 #print
add $a0, $t2, $zero #copy $v1 to print
syscall #call for print
#jump to end:
end:
#program exiting routine
I'm not sure about the exact syntax for the exiting routine, but I'm sure you'll have no problem figuring that out :)
Hope this helps!
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.