-1

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
N.Thompson
  • 45
  • 7
  • Labels are just names for locations in your program, they are not barriers that cause the CPU to stop execution. If you want to terminate your program you need to do so explicitly, e.g. by using system call 10. – Michael Mar 20 '17 at 13:35

1 Answers1

0

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!

shadowfire
  • 106
  • 3