因此,使用此 MIPS 代码,我试图让用户输入他们想要的树大小,而输出就是成本。199 for > 8, 99 >= 6, 69 >= 3, 39 >= 0,只有一个短语表示正面,仅用于负面输入。
我的问题是,不管输入什么,程序总是吐69,怎么回事?
#data declarations: declare variable names used in program, storage allocated in RAM
.data
msg1: .asciiz "Enter the height of a tree to purchase: "
msg21: .asciiz "The cost: "
msg22: .asciiz " dollars\n"
msg3: .asciiz "\n"
msgNeg: .asciiz "Please enter a positive integer for the tree height.\n"
big: .word 8
med: .word 6
small: .word 3
negative: .word 0
#The program begins after .text
.text
main:
#Prints first line and receives input
la $a0, msg1
li $v0, 4
syscall
li $v0, 5
syscall
la $a0, msg3
li $v0, 4
syscall
#Stores user input in parameter variable
move $a0, $v0
#Calls cost function to determine cost
addi $sp, $sp, -4
sw $ra, 0($sp)
jal cost
lw $ra, 0($sp)
addi $sp, $sp, 4
#stores returned value to temporary register
move $t0, $v0
#Prints out the cost of the tree
la $a0, msg21
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
la $a0, msg22
li $v0, 4
syscall
#return
jr $ra
cost:
#finds the range of the user value
la $t6, small
lw $t7, 0($t6)
bge $a0, $t7, smTree
nop
la $t6, med
lw $t7, 0($t6)
bge $a0, $t7, medTree
nop
la $t6, big
lw $t7, 0($t6)
bgt $a0, $t7, bigTree
nop
la $t6, negative
lw $t7, 0($t6)
blt $a0, $t7, negTree
nop
#else statement, returns 39
li $v0, 39
jr $ra
bigTree:
#first branch, returns 199
li $v0, 199
jr $ra
medTree:
#second branch, returns 99
li $v0, 99
jr $ra
smTree:
#third branch, returns 69
li $v0, 69
jr $ra
negTree:
#fourth branch, returns a phrase
la $a0, msgNeg
li $v0, 4
syscall
jr $ra