validation - 输入验证 MIPS
问题描述
如何使用 MARS 在 MIPS 指令集中验证用户输入?在我的以下程序中,我想知道如何向用户输入添加验证,例如在询问用户是否要解密或加密他们需要输入“E”或“D”的消息时(由于 ascii 值区分大小写)。以及来自 (1-9) 的输入,它是加密或解密密钥。如果他们输入除“E”、“D”和 1-9 的数字以外的任何内容,我如何要求用户使用子程序为每个提示输入有效输入?
代码:
.data
encrypt_decrypt: .asciiz "Type(E)ncrypt/(D)ecrypt:"
key: .asciiz "\nEnter encryption key (1-9): "
message_prompt: .asciiz "\nEnter message: "
output: .asciiz ""
re_enter: .asciiz "\nWrong input"
buffer: .space 50
.text
main:
# Prints the prompt asking the user if they want to encrypt or decrypt
li $v0, 4
la $a0, encrypt_decrypt # Loads address from the user prompt
syscall
# Takes in E OR D based on user choice
li $v0, 12
syscall
add $t6, $v0, $0
bne $t6, 69, ELSE # Branch to else if inputed value is not E
# Prints the prompt asking the user for a message to either decrypt or
encrypt based on the users previous choice
li $v0, 4
la $a0, message_prompt # Loads address from the users message
syscall
# Takes the users message
li $v0, 8
la $a0, buffer
lb $t0, 0($a0)
li $a1, 48
syscall
la $s2, buffer
# Asks the user for a key from 1-9
li $v0, 4
la $a0, key
syscall
# Takes the users key value from 1-9
li $v0, 5
syscall
add $s0, $v0, $0
la $t0, ($s0) # Loads key
li $t1, 0 # Lower order bit to be toggled
la $t2, ($s2) # Load user message
beq $t6, 69, encrypt # 69 Is the printable ASCII value of "E"
beq $t6, 68, decrypt # 68 is the printable ASCII value of "D"
ELSE:
bne $t6, 68, E_D_VALIDATION # Branch to E_D_VALIDATION if user input is not D
E_D_VALIDATION:
li $v0, 4
la $a0, re_enter
syscall
j main
encrypt:
encrypt_loop:
lb $t3, 0($t2) # Loads character bytes
beq $t3, 10, encrypted # Branches to "encrypted" if we reach the end of a character
add $t3, $t3, $t0
addi $t4, $0, 1
sllv $t4, $t4, $t1
xor $t3, $t3, $t4 # Lower order bit toggle
sb $t3, 0($t2) # Stored encrypted character
addi $t2, $t2, 1
j encrypt_loop # Loops through the list of encrypted characters
encrypted:
li $v0, 4 # Print out encrypted message
la $a0, output
syscall
la $a0, ($s2)
syscall
j exit # Exits the program
decrypt:
decrypted_loop:
lb $t3, 0($t2) # Loads character bytes
beq $t3, 10, decrypted # Branches to "decrypted" if we reach the end of a character
addi $t4, $0, 1
sllv $t4, $t4, $t1
xor $t3, $t3, $t4
sub $t3, $t3, $t0
sb $t3, 0($t2) # Stored decrypted character
addi $t2, $t2, 1
j decrypted_loop # Loops through the list of decrypted characters
decrypted:
li $v0, 4 # Print out decrypted message
la $a0, output
syscall
la $a0, ($s2)
syscall
j exit # Exit the program
exit:
解决方案
您所缺少的只是错误处理:
beq $t6, 69, encrypt # 69 Is the printable ASCII value of "E"
beq $t6, 68, decrypt # 68 is the printable ASCII value of "D"
<handle error here>
encrypt:
如何处理错误:打印一条消息并退出,或打印一条消息并转到代码中较早的某个位置,main
或者您为此引入的其他一些新标签。
但是,在到达这里之前检查错误的输入可能会很好,如果你这样做了,你以后不必检查太多:你可以只检查 E 或只检查 D 而不是同时检查两者,因为此时你知道这只是其中之一,没有别的。
所以你想在输入应该是 E 或 D 后立即输入的伪代码:
if ( input != E && input != D ) { print input error message; goto reEnter; }
reEnter
您选择返回以获取输入的主要位置或其他位置可能在哪里。
我将使用以下一系列转换将其翻译成汇编语言。
首先,if-goto-label 样式,当条件为 false 时跳过 if 语句的 then 部分:
if ( ! ( input != E && input != D ) ) goto inputOk;
print input error message
goto reEnter;
inputOk:
; continue with rest of program knowing good input (exactly E or D)
下一步:根据De Morgan分配否定:
if ( ! ( input != E ) || ! ( input != D ) ) goto inputOk;
print input error message
goto reEnter;
inputOk:
; continue with rest of program knowing good input (exactly E or D)
下一步:否定关系运算符:
if ( input == E || input == D ) goto inputOk;
print input error message
goto reEnter;
inputOk:
; continue with rest of program knowing good input (exactly E or D)
下一步:拆分析取 ( ||
)
if ( input == E ) goto inputOk;
if ( input == D ) goto inputOk;
print input error message
goto reEnter;
inputOk:
; continue with rest of program knowing good input (exactly E or D)
现在,这很容易用汇编语言编写。
推荐阅读
- reactjs - React 构建每次都失败并出现 uglify 错误
- php - 从地址访问时会话不起作用
- twilio - Studio MAKE HTTP REQUEST return sid
- git - Git 修订图不正确
- wordpress - WooCommerce:仅适用于一个优惠券代码的有条件手续费
- sql-server - 每天删除 /var/opt/mssql/log 中的所有文件?
- powershell - PowerShell 如何在一月份处理 ((Get-Date).Month)-1?
- python - python生成器yield语句不yield
- python - 为什么我插入 AWS Postgres 数据库(使用 psycopg2)的任何行都会立即变成死元组?
- cocoa - 保存子 NSManagedObjectContext 时在 NSOrderedSet 上调用额外不正确的 KVO 插入