我是MIPS汇编语言的新手。刚开始学习。我有这个问题,我必须显示用户输入的测试分数的字母等级。到目前为止我已经做到了这一点,但我无法让它发挥作用。我总是一直无效或有时程序结束或给出D。
假设:
Score Range Letter Grade
90 to 100 A
80 to 89 B
70 to 79 C
60 to 69 D
0 to 59 F
请帮我弄清楚我做错了什么并纠正我的代码!
.data
prompt: .asciiz "Enter a test score [0 to 100] or -1 to stop: "
invalidPrompt: .asciiz "Invalid \n"
A: .asciiz "A \n"
B: .asciiz "B \n"
C: .asciiz "C \n"
D: .asciiz "D \n"
F: .asciiz "F \n"
.globl main
.text
main:
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
#move $a1, $v0
beq $v0, -1, end # -1 to stop
checkA:
bge $v0, 90, checkB
ble $v0, 100, checkB
li $v0, 4
la $a0, A
syscall
# j main
checkB:
bge $v0, 80, checkC
ble $v0, 89, checkC
li $v0, 4
la $a0, B
syscall
# j main
checkC:
bge $v0, 70, checkD
ble $v0, 79, checkD
li $v0, 4
la $a0, C
syscall
# j main
checkD:
bge $v0, 60, checkF
ble $v0, 69, checkF
li $v0, 4
la $a0, D
syscall
# j main
checkF:
#bge $v0, 0, invalid
ble $v0, 59, end
li $v0, 4
la $a0, D
syscall
# j main
invalid:
blt $v0, 0, end
bge $v0, 100, end
li $v0, 4
la $a0, invalidPrompt
syscall
# j main
j main
end:
#--- TERMINATE ---
li $v0, 10 # terminate program run and
syscall # return control to the OS.
问题的第二部分是我必须在用户输入-1后显示A,B等的数量。我怎么能这样做?我知道我必须在单独的寄存器中存储每个等级的计数,并保持递增但不确定如何完成。
编辑:我在Windows 7上使用MARS 4.1工具。
答案 0 :(得分:1)
第1部分:
checkA:
bge $v0, 90, checkB
ble $v0, 100, checkB
在这里测试$ v0是否介于90和100之间,然后跳转到checkB。您必须检查它是否介于90和100之间:
checkA:
ble $v0, 90, checkB
bge $v0, 100, checkB
第2部分:只需将$ t1初始化为$ t5为零,并在A时增加$ t1,在B时增加$ t2 ......
答案 1 :(得分:1)
你的逻辑倒退了。 GE =大于或等于 LE =少于或等于
如果你从最小的开始并按照自己的方式工作,你根本不需要逻辑AND运算符。
main: li $v0, 4 la $a0, prompt syscall li $v0, 5 syscall #move $a1, $v0 beq $v0, -1, end # -1 to stop ble $v0, 0, invalid checkF: bge $v0, 60, checkD li $v0, 4 la $a0, F syscall j main checkD: bge $v0, 70, checkC li $v0, 4 la $a0, D syscall j main checkC: bge $v0, 80, checkB li $v0, 4 la $a0, C syscall j main checkB: bge $v0, 90, checkA li $v0, 4 la $a0, B syscall j main checkA: bge $v0, 101, invalid li $v0, 4 la $a0, A syscall j main invalid: li $v0, 4 la $a0, invalidPrompt syscall j main end: li $v0, 10 # terminate program run and syscall # return control to the OS.