处理非数字用户输入到 MARS 读整数系统调用? [英] Handling non-digit user input to a MARS read-integer system call?

查看:64
本文介绍了处理非数字用户输入到 MARS 读整数系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的停车场程序,它接受用户的输入,并根据输入分支到一个标签.

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.

问题是,如果用户输入的数据类型不同于整数(如a"或任何字母),程序总是会突然结束

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).

有什么想法吗?

推荐答案

使用读取文本而不是特定数据类型的系统调用,然后在将文本转换为值时检查文本的格式和字符是否正确.

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.

这篇关于处理非数字用户输入到 MARS 读整数系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆