MIPS打印问题

时间:2017-03-20 13:24:09

标签: assembly mips

我正在尝试获取用户输入(input = $ v0),然后将其与10(10 = $ t1)进行比较。 如果输入小于10,我想打印'<'。如果输入大于10,我希望它打印'>'。我尝试了一些不同的东西,但由于某种原因它最终打印'<'和'>'。以及错误读取“程序已完成运行(从底部掉下来)”有人能告诉我我做错了什么吗?

    #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

1 个答案:

答案 0 :(得分:0)

你需要以某种方式完成你的程序。例如,如果代码跳转到less:标签,因为您没有任何返回或跳转到完成程序的例程,执行只是继续great:部分。

因此,您应该从程序中退出另一个标签end:。在end:less:执行系统调用后,您应该跳转到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

我不确定退出例程的确切语法,但我相信你可以毫无困难地解决这个问题:)

希望这有帮助!