Handling non-digit user input to a MARS read-integer system call?

独自空忆成欢 提交于 2021-02-05 12:00:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!