【发布时间】:2015-05-28 03:00:56
【问题描述】:
所以我需要完成的是验证输入的整数是否在可接受的值 (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
【问题讨论】:
-
当您在旧操作系统上读取字符串时,它首先会进入您的缓冲区,而您是显示它的人。问题在于您正在测试什么,如果是自动显示的情况,是否有办法(在您的 API 中)禁用输出。
-
无论如何,看到
ReadInt的实现可能会有很大帮助。 -
ReadInt 过程是从 Irvine 提供的,因此我们不应该篡改该部分。我们正在通过 Microsoft Visual Studios 运行程序。