2017-03-20 180 views
0

我想獲取用戶輸入(輸入= $ v0),然後將其與10(10 = $ t1)進行比較。 如果輸入少於十,我想打印'<'。如果輸入大於十,我希望它打印'>'。我嘗試了一些不同的東西,但由於某種原因它最終打印了'<'和'>'。以及讀取「程序運行完畢(下降)」的錯誤「誰能告訴我,我做錯了什麼?MIPS打印問題

#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 
+0

標籤只是程序中位置的名稱,它們不是導致CPU停止執行的障礙。如果你想終止你的程序,你需要明確地這樣做,例如通過使用系統調用10。 – Michael

回答

0

你需要以某種方式完成你的程序。例如,如果代碼跳轉到less:標籤,因爲您沒有任何退貨或跳轉到完成該程序的例程,則執行直接沿着great:部分繼續。

所以你應該在你退出程序的地方製作另一個標籤end:。在執行less:great:中的系統調用之後,您應該跳到end:

#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 

我不知道有關退出的常規確切的語法,但我敢肯定,你不會有任何問題搞清楚了這一點:)

希望這有助於!