输入无回显 Irvine32.lib
Input without echo with Irvine32.lib
所以我需要完成的是验证输入的整数是否在可接受的值 (1-4) 范围内,而不是提示用户它不正确,而是等到输入有效为止。 (此程序不允许使用 If、Repeat 或 While 语句)我已通过使用:
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo: ;Validate the integer
call ReadInt
cmp al,1 ;Wait for valid entry if integer is less than 1
jl Redo
cmp al,4 ;Wait for valid entry if integer is greater than 4
jg Redo
ret
然而,我的教授提到输入应该预先检查而不是显示。这可能吗?由于您将输入输入内容,它们是如何消失的?我不知道该怎么做,甚至不知道从哪里开始。我花了很长时间才弄清楚如何让程序等待有效输入,现在发现我做错了。有人可以给我一些指示吗?还是朝着正确的方向轻推?
这里是锁模拟的完整代码。
INCLUDE Irvine32.inc
.data
prompt BYTE 'Please press a button (1,2,3, or 4):', 0
report1 BYTE 'The lock 2s count = 0', 0
report2 BYTE 'The lock 2s count = 1', 0
report3 BYTE 'The lock 2s count = 2', 0
report4 BYTE 'The lock 2s count = 3', 0
report5 BYTE 'The lock is open!', 0
.code
main PROC
; Main program control procedure.
; Calls: DisplayMessage, GetButtonPress, Crlf,
; WriteString
Start: ;Beginning of the lock simulation
call DisplayMessage ;Display the starting lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State1
je State1
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State2
je State2
State0: ;When the lock 2s count equals 0
mov al, 0 ;Make the AL register equal to 0 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State1
je State1
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State2
je State2
State1: ;When the lock 2s count equals 1
mov al,1 ;Make the AL register equal to 1 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State2
je State2
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State3
je State3
State2: ;When the lock 2s count equals 2
mov al,2 ;Make the AL register equal to 2 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State1
je State1
cmp al, 2 ;If user choice is 2 jump to State3
je State3
cmp al, 3 ;If user choice is 3 jump to Terminal
je Terminal
cmp al, 4 ;If user choice is 4 jump to State3
je State3
State3: ;When the lock 2s count equals 3
mov al,3 ;Make the AL register equal to 3 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State2
je State2
cmp al, 2 ;If user choice is 2 jump to State3
je State3
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State3
je State3
Terminal:
mov edx, OFFSET report5 ;Declare the lock as open
call WriteString
exit ;exit to operating system
main ENDP
;-------------------------------------------------------------
GetButtonPress PROC
;
; Prompts the user for button entry
; Receives: None
; Returns: An integer value to the AL register
; Calls: WriteString, ReadInt
;----------------------------------------------------------------
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo: ;Validate the integer
call ReadInt
cmp al,1 ;Wait for valid entry if integer is less than 1
jl Redo
cmp al,4 ;Wait for valid entry if integer is greater than 4
jg Redo
ret
GetButtonPress ENDP
;-----------------------------------------------------------------
DisplayMessage PROC
;
; Displays the appropriate message concerning the lock 2s count
; Receives: The integer value in the AL register
; Returns: Nothing
; Calls: WriteString
;------------------------------------------------------------------
cmp al,0 ;Compare 1-3 to the AL register to display the proper message
je Message1
cmp al, 1
je Message2
cmp al, 2
je Message3
cmp al, 3
je Message4
Message1: ;If AL = 0 then the lock 2s count is 0
mov edx,OFFSET report1
call WriteString
jmp Quit
Message2: ;If AL = 1 then the lock 2s count is 1
mov edx,OFFSET report2
call WriteString
jmp Quit
Message3: ;If AL = 2 then the lock 2s count is 2
mov edx,OFFSET report3
call WriteString
jmp Quit
Message4: ;If AL = 3 then the lock 2s count is 3
mov edx,OFFSET report4
call WriteString
Quit: ;Return to main PROC
ret
DisplayMessage ENDP
END main
我想通了 - 我实际上需要将数字作为字符读取并改用作者的 ReadChar 过程,因为它不会回显到屏幕上。
我将此部分更改为:
;-------------------------------------------------------------
GetButtonPress PROC
;
; Prompts the user for button entry
; Receives: None
; Returns: A character value to the AL register
; Calls: WriteString, ReadChar
;----------------------------------------------------------------
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo:
call ReadChar
cmp al,'1'
jl Redo
cmp al,'4'
jg Redo
call WriteChar
ret
GetButtonPress ENDP
所以我需要完成的是验证输入的整数是否在可接受的值 (1-4) 范围内,而不是提示用户它不正确,而是等到输入有效为止。 (此程序不允许使用 If、Repeat 或 While 语句)我已通过使用:
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo: ;Validate the integer
call ReadInt
cmp al,1 ;Wait for valid entry if integer is less than 1
jl Redo
cmp al,4 ;Wait for valid entry if integer is greater than 4
jg Redo
ret
然而,我的教授提到输入应该预先检查而不是显示。这可能吗?由于您将输入输入内容,它们是如何消失的?我不知道该怎么做,甚至不知道从哪里开始。我花了很长时间才弄清楚如何让程序等待有效输入,现在发现我做错了。有人可以给我一些指示吗?还是朝着正确的方向轻推?
这里是锁模拟的完整代码。
INCLUDE Irvine32.inc
.data
prompt BYTE 'Please press a button (1,2,3, or 4):', 0
report1 BYTE 'The lock 2s count = 0', 0
report2 BYTE 'The lock 2s count = 1', 0
report3 BYTE 'The lock 2s count = 2', 0
report4 BYTE 'The lock 2s count = 3', 0
report5 BYTE 'The lock is open!', 0
.code
main PROC
; Main program control procedure.
; Calls: DisplayMessage, GetButtonPress, Crlf,
; WriteString
Start: ;Beginning of the lock simulation
call DisplayMessage ;Display the starting lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State1
je State1
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State2
je State2
State0: ;When the lock 2s count equals 0
mov al, 0 ;Make the AL register equal to 0 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State1
je State1
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State2
je State2
State1: ;When the lock 2s count equals 1
mov al,1 ;Make the AL register equal to 1 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State0
je State0
cmp al, 2 ;If user choice is 2 jump to State2
je State2
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State3
je State3
State2: ;When the lock 2s count equals 2
mov al,2 ;Make the AL register equal to 2 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State1
je State1
cmp al, 2 ;If user choice is 2 jump to State3
je State3
cmp al, 3 ;If user choice is 3 jump to Terminal
je Terminal
cmp al, 4 ;If user choice is 4 jump to State3
je State3
State3: ;When the lock 2s count equals 3
mov al,3 ;Make the AL register equal to 3 for message display
call DisplayMessage ;Display lock 2s count
call Crlf ;New line
call GetButtonPress ;Get user button choice
cmp al, 1 ;If user choice is 1 jump to State2
je State2
cmp al, 2 ;If user choice is 2 jump to State3
je State3
cmp al, 3 ;If user choice is 3 jump to State0
je State0
cmp al, 4 ;If user choice is 4 jump to State3
je State3
Terminal:
mov edx, OFFSET report5 ;Declare the lock as open
call WriteString
exit ;exit to operating system
main ENDP
;-------------------------------------------------------------
GetButtonPress PROC
;
; Prompts the user for button entry
; Receives: None
; Returns: An integer value to the AL register
; Calls: WriteString, ReadInt
;----------------------------------------------------------------
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo: ;Validate the integer
call ReadInt
cmp al,1 ;Wait for valid entry if integer is less than 1
jl Redo
cmp al,4 ;Wait for valid entry if integer is greater than 4
jg Redo
ret
GetButtonPress ENDP
;-----------------------------------------------------------------
DisplayMessage PROC
;
; Displays the appropriate message concerning the lock 2s count
; Receives: The integer value in the AL register
; Returns: Nothing
; Calls: WriteString
;------------------------------------------------------------------
cmp al,0 ;Compare 1-3 to the AL register to display the proper message
je Message1
cmp al, 1
je Message2
cmp al, 2
je Message3
cmp al, 3
je Message4
Message1: ;If AL = 0 then the lock 2s count is 0
mov edx,OFFSET report1
call WriteString
jmp Quit
Message2: ;If AL = 1 then the lock 2s count is 1
mov edx,OFFSET report2
call WriteString
jmp Quit
Message3: ;If AL = 2 then the lock 2s count is 2
mov edx,OFFSET report3
call WriteString
jmp Quit
Message4: ;If AL = 3 then the lock 2s count is 3
mov edx,OFFSET report4
call WriteString
Quit: ;Return to main PROC
ret
DisplayMessage ENDP
END main
我想通了 - 我实际上需要将数字作为字符读取并改用作者的 ReadChar 过程,因为它不会回显到屏幕上。
我将此部分更改为:
;-------------------------------------------------------------
GetButtonPress PROC
;
; Prompts the user for button entry
; Receives: None
; Returns: A character value to the AL register
; Calls: WriteString, ReadChar
;----------------------------------------------------------------
mov edx,OFFSET prompt ;Prompt user for integer entry
call WriteString
Redo:
call ReadChar
cmp al,'1'
jl Redo
cmp al,'4'
jg Redo
call WriteChar
ret
GetButtonPress ENDP