输入验证 MIPS
Input 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, [=10=]
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, [=10=]
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, [=10=], 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, [=10=], 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 样式,当条件为假时跳过 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)
现在,用汇编写起来很容易。
如何使用 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, [=10=]
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, [=10=]
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, [=10=], 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, [=10=], 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 样式,当条件为假时跳过 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)
现在,用汇编写起来很容易。