0

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?

3
  • Does the documentation for the read-integer syscall say anything about what it returns on non-digit input? Does it just return 0 with no way to distinguish that from the user typing a 0? Commented May 6, 2018 at 13:06
  • 1
    It pops out this : "Carrpark.asm line 21: Runtime exception at 0x00400028: invalid integer input (syscall 5) Go: execution terminated with errors." Commented May 6, 2018 at 17:33
  • Ok. So MARS/SPIM integer input system calls are designed for toy programs where the user cooperates, not for writing robust software. Definitely reading input as ASCII bytes will work, like @prl suggests. It would be nice if there was another syscall that would read integers with some kind of error-reporting mechanism, or if there was a scanf. But writing your own string -> int function isn't hard. Commented May 6, 2018 at 18:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.