你實際上是相當接近。
main:
標籤位於錯誤的地方,所以在某些模擬器下,可能不會執行前幾條指令。
此外,該程序[有點]不完整,因爲在採用條件分支之後,程序「脫離世界的盡頭」。
但是,問題的核心是兩個分支需要不同的分支條件和需要改變在每個第二寄存器:
變化:
ble $t0, $t1, numberSmaller
分爲:
blt $t0, $t2, numberSmaller
更改:
bge $t0, $t2, numberLarger
分爲:
bgt $t0, $t3, numberLarger
所以,這裏的全部糾正的程序:
.data
prompt: .asciiz "Enter your number (3-30):"
message: .asciiz "\n Your number is "
message2: .asciiz "\n Your number is less than 3"
message3: .asciiz "\n Your number is more than 30"
.text
.globl main
main:
# Prompt the user to enter the number
li $v0,4
la $a0,prompt
syscall
# Get the number
li $v0,5 # get an integer from the user
syscall
# Store the result in t0
move $t0,$v0
addi $t1,$zero,0
addi $t2,$zero,3
addi $t3,$zero,30
blt $t0,$t2,numberSmaller
bgt $t0,$t3,numberLarger
# output message to show the number
li $v0,4
la $a0,message
syscall
# show the number
li $v0,1
move $a0,$t0
syscall
exit:
li $v0,10 # syscall to end the program
syscall
numberSmaller:
li $v0,4
la $a0,message2
syscall
j exit
numberLarger:
li $v0,4
la $a0,message3
syscall
j exit
這裏有一個稍微更嚴格的版本:
.data
prompt: .asciiz "Enter your number (3-30):"
message: .asciiz "\n Your number is "
message2: .asciiz "\n Your number is less than 3"
message3: .asciiz "\n Your number is more than 30"
.text
.globl main
main:
# Prompt the user to enter the number
li $v0,4
la $a0,prompt
syscall
# Get the number
li $v0,5 # get an integer from the user
syscall
# Store the result in t0
move $t0,$v0
li $t1,3
blt $t0,$t1,numberSmaller
li $t1,30
bgt $t0,$t1,numberLarger
# output message to show the number
li $v0,4
la $a0,message
syscall
# show the number
li $v0,1
move $a0,$t0
syscall
exit:
li $v0,10 # syscall to end the program
syscall
numberSmaller:
li $v0,4
la $a0,message2
syscall
j exit
numberLarger:
li $v0,4
la $a0,message3
syscall
j exit
我不知道MIPS要麼是$ ble $ t0,$ t1,numberSmaller bge $ t0,$ t2,numberLarger li $ v0,10':第三行不可到達,因爲有相反的條件分支到上面的其他地方。 'bge'和'ble'也會在平等上分支。你可能需要'bgt'和'blt' –
你還沒有解釋問題是什麼(例如,你目前的結果與預期的結果有什麼不同?)。 – Michael