我有一个程序,它需要一个整数作为来自用户的输入(使用系统调用5)。当用户输入特殊字符时! @#$%^ _ - ?)(而不是崩溃的整数,我怎么能解决方案,而不是显示崩溃它的错误消息Mips汇编语言错误检查
这是我写
la $a0, prompt # prompt the message ask for the answer n
li $v0, 4
syscall
li $v0, 5 # input the answer
syscall
add $t1, $zero, $v0
# store the answer in $t1
blt $t1, $s0, negative # check for number entered < $s0
bgt $t1, $s1, exceed # check for > $s1 number
la $a0, blank # blank line
li $v0, 4
syscall
代码样本输出。
This is a number guessing game between 0- 100. Let's begin.
Chances remains: 6
Enter your guess: 56
The secret number is higher than : 56
Chances remains: 5
Enter your guess: 67
The secret number is higher than : 67
Chances remains: 4
Enter your guess: 75
The secret number is lower than : 75
Chances remains: 3
Enter your guess: 74
The secret number is lower than : 74
Chances remains: 2
Enter your guess: 72
The secret number is lower than : 72
Chances remains: 1
Enter your guess: 70
The secret number is lower than : 70
Chances remains: 0
Sorry...You lost.
The secret number is: 69
Do you want to play again?
1.Yes
2.No
Choice: 2
Your average guess: 3
-- program is finished running --
也许有更好的方法去做你想做的事情。你能澄清吗? – blackcompe
上面的代码实际上是猜数游戏的一部分,程序选择一个随机数(使用系统调用42)并且用户输入他的猜测。用户有6次机会猜测正确的答案。 –
样本输出: –