; List of interrupts:  http://www.gabrielececchetti.it/Teaching/
; CalcolatoriElettronici/Docs/i8086_and_DOS_interrupts.pdf

; Using a proc to check for a correct password in 'scode' variable
; It checks as long as the characters match. If matched, prints *
; and moves forward. If not matched jumps to the next attempt. 3
; attempts are allowed. Give appropriate messages.

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
mess		db "Enter password", 13, 10, '$'
messNot     db "Are you a hacker?", 13, 10, '$'
messYes		db "you are in, buddy!", 13, 10, '$'
errorInp	db "<<<Error! Try Again!$"
scode		db '12345'

; --------------------------
CODESEG
parm1 	    equ [word ptr bp+4]
lvar1       equ [word ptr bp-2]      ; local variable (attempts)  

proc		passwordCheck
	        push bp
	        mov bp, sp
	        sub sp, 2
	
	        push bx 
	        push dx
	        push ax
	        push cx
	
	        mov ax, 3					    ;Number of attempts
	        mov lvar1, ax
	
	        mov dx, offset mess				;print message input code
	        mov ah, 9h
	        int 21h
		
TryAgain:
	        mov cx, 5						;length of code
	        mov bx, parm1
inputcheck:
            ; wait for input
	        mov ah, 7h						;input a character - no echo
	        int 21h                         ;so we don't show it (1h is with)
	        push ax
	        mov ah, 2h
	        mov dl, '*'						;print * (ascii 42)
	        int 21h
	        pop ax
	
	        cmp al, [bx]
	        je nextchar
	
	        dec lvar1                       ;char is no match if here
	        cmp lvar1, 0
	        je noMoreAttempts
	
	        xor bx, bx
	        mov dx, offset errorInp			;print error message
	        mov ah, 9h
	        int 21h

	        mov ah, 2h
	        mov dl, 13						;Move cursor to the beginning
	        int 21h

	        mov ah, 86h						;wait about 5 seconds
	        mov dx, 00000                   ; cx:dx micro seconds 
	        mov cx, 50
	        int 15h
	
	        mov cx, 23						;clear error message one
clearchar:						            ;char at a time. writes		
            mov ah, 2h                      ;space (in a loop)
	        mov dl, ' '                     
	        int 21h
	        loop clearchar
	
	        mov ah, 2h								
	        mov dl, 13						;Move cursor to the beginning
	        int 21h                         ;of the line. Without this cursor
	        jmp TryAgain                    ;stays where last left.
	
nextchar:                                 
	        inc bx	                        ;point to next char of password
	        loop inputcheck
	        mov dx, offset messYes			;print message successful
	        mov ah, 9h
	        int 21h
	        jmp exit
noMoreAttempts:
	        mov dx, offset messNot			;print message not successful
	        mov ah, 9h
	        int 21h
exit:	
	        pop ax
	        pop dx
	        pop bx
	        pop cx
	        add sp, 2
	        pop bp
	        ret 2
endp		passwordCheck

start:
	        mov ax, @data
	        mov ds, ax

	        mov  bx, offset scode
	        push bx
	        call passwordCheck
	
	        mov ax, 4c00h
	        int 21h
END start


