问题
I'm doing a simple program of car park that takes the input from the user, and branch to a label in accordance of the input.
The problem is, the program always ends abruptly if the user enter an input with different data type than integer (like "a" or any alphabets)
Below is my code (that relevant to the input part) :
li $v0,5 #system call to get input from user
syscall
li $t1,2
beq $v0,1,park # go to "park" section of codes if input is 1
beq $v0,2,exit # go to "exit" section of codes if input is 2
beq $v0,0,end # go to "end" section of codes if input is 0
la $a0,statement6 #load statement6 into $a0
li $v0,4 #call code for print statement2
syscall
j start
How do I handle the wrong data type in assembly language, so that the program will not end if the user enters an input with wrong data type (or other than int).
Any ideas?
回答1:
Use a system call that reads text, rather than a specific data type, and then check the text for the correct format and characters while converting it to the value.
来源:https://stackoverflow.com/questions/50198025/handling-non-digit-user-input-to-a-mars-read-integer-system-call