So what I need to accomplish is validate that the integer entered is within the accepted values (1-4) and not prompt the user that it is incorrect, rather wait until a valid entry is made. (If, Repeat or While statements are not allowed for this program )I have done so by using:
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
However, my professor has mentioned that the input should be pre-checked and not displayed. Is this possible? Since you will be typing in the input, how do they just disappear? I have no idea how to do this or where to even start. It took me forever just to figure out how I was going to get the program to wait for valid entry and now have found out that I'm doing it wrong anyways. Can someone give me some pointers? Or a nudge in the right direction?
Here is the full code for the lock simulation.
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
I figured it out - I actually needed to read the numbers as characters and use the author's ReadChar procedure instead as it does not echo to the screen.
I changed this section to:
;-------------------------------------------------------------
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