	.model small
	.stack 100
	.data

        
        data_add DW 0378H
        st_add   dw 0379h
        con_add  dw 037ah
        c_r      db 03h
        cl_cont  db 00
        t_c      db 03h
        al_cont  db 00
        key_1    DB 00
	key_2    DB 00
        sck      db 00
        tog      db 00
        st_p     db 00
        cw_1     db 02h
        cw_2     db 01h
        cw_3     db 0bh
        cw_4     db 03h
        c_e      db 1
        c_t      dw 0f4h
	c_f      dw 03f6h
	c_p      dw 0432h
        a_ress   dw 00
        text     db 0f4h dup (00)
	text_p   db "c:\identity.txt",00h
        face     db 03f6h dup (00)
	face_p   db "c:\face.gif",00h
        finger   db 0432h dup (00)
	finger_p db "c:\finger.gif",00h
	text_h   dw ?
	face_h   dw ?
	finger_h dw ?
        m_began  db "THIS IS IDENTITY CARD PROGRAM.",0AH,0DH
                 DB "IF YOU ARE AUTHURIZED, PLEASE PUT THE CARD",0AH,0DH
                 DB "IN THE CARD READER THEN INPUT THE PASSWORD",0AH,0DH
                 DB "BY THE KEYBOARD.",0AH,0DH
                 db "NOTE: YOU HAVE THREE ATTEMPTS ONLY",0AH,0DH,0AH,0DH
                 db "THE PASSWORD IS:   ",'$'
        m_e_1    db "YOU INPUT A WRONG PASSWORD. YOU CAN TRY AGAIN ("          
        eee      db 00
                 db ") TIMES ONLY (BE CAREFUL)."
                 db  0AH,0DH,"THE PASSWORD IS : ",'$'
        m_e_2    db "SORRY YOU ARE NOT AUTHORIZED TO ACCESS THIS IDENTITY CARD.",'$'
                
;==================================================
	.code
main 	proc far

	mov ax,@data
	mov ds,ax
	lea dx,m_began
	mov ah,09h
        int 21h                     ;display begening message
attempts:

       
        mov ah,1
        int 21h
        xor al,30h
        mov cl,4
        rol al,cl                  ;convert from char to digit
        
        mov key_1,al
        

        mov ah,1
        int 21h
        xor al,30h
        xor key_1,al                  ;get first digit of password

        mov ah,1
        int 21h
        xor al,30h
        mov cl,4
        rol al,cl                  ;convert from char to digit
        
        mov key_2,al
        

        mov ah,1
        int 21h
        xor al,30h
        xor key_2,al                  ;get second digit of password

   ;ccccccccccccccccccccccccccccccccccccc
    
      call dat_a                           ; receive data
      ;=====================
     
        xor al,key_2
        xor al,key_1                ;decrypt

        cmp al,074h                   ;test the authorazition
         je retrave
        cmp c_e,3
         je e_the_p                  ; test the number of attempts

        mov bl,033h
        sub bl,c_e
        mov eee,bl
        mov ax,@data
        mov ds,ax
        mov ah,09h
        lea dx,m_e_1
        int 21h                       ;out error message 
        inc c_e
        jmp attempts


retrave:
        mov cx,c_t

text_loop:

      ;=====================
      call dat_a                    ;receive data
      ;=====================

        xor al,key_2
        xor al,key_1                ;decrypt

        mov bx,0f4h
        sub bx,c_t
        mov text+[bx],al             ; writ into string
        dec c_t
        inc a_ress
       loop text_loop                     
        
        
        mov ah,3ch                ;creat identity.txt file
        mov cx,00
        lea dx,text_p
        int 21h
         jc error_p
        mov text_h,ax

        mov ah,40h                  ;write into file
        mov bx,text_h
        mov cx,0f4h
        lea dx,text
        int 21h
         jc error_p
        cmp ax, 0f4h
        jne error_p

        mov ah,03eh                  ; close identity.txt file
        mov bx,text_h
        int 21h
         jc error_p


        mov cx,c_f

face_loop:
      ;=====================
      call dat_a                          ;receive data
      ;=====================

        xor al,key_2
        xor al,key_1                ;decrypt
    
        mov bx,03f6h
        sub bx,c_f
        mov face+[bx],al             ; writ into string
        dec c_f
        inc a_ress
       loop face_loop                     
        
        
        mov ah,3ch                ;creat face.gif file
        mov cx,00
        lea dx,face_p
        int 21h
         jc error_p
        mov face_h,ax

        mov ah,40h                  ;write into face.gif file
        mov bx,face_h
        mov cx,03f6h
        lea dx,face
        int 21h
         jc error_p
        cmp ax, 03f6h
        jne error_p

        mov ah,03eh                  ; close face.gif file
        mov bx,face_h
        int 21h
         jc error_p


        mov cx,c_p

finger_loop:
      ;=====================
       call dat_a              ; receive address
      ;=====================

        xor al,key_2
        xor al,key_1                ;decrypt

        mov bx,0432h
        sub bx,c_p                   
        mov finger+[bx],al           ; writ into string
        dec c_p
        inc a_ress
       loop finger_loop                      
        
        
        mov ah,3ch                ;creat finger.gif file
        mov cx,00
        lea dx,finger_p
        int 21h
         jc error_p
        mov finger_h,ax

        mov ah,40h                  ;write into finger.gif file
        mov bx,finger_h
        mov cx,0432h
        lea dx,finger
        int 21h
         jc error_p
        cmp ax, 0432h
        jne error_p

        mov ah,03eh                  ; close finger.gif file
        mov bx,text_h
        int 21h
         jc error_p


        jmp error_p
e_the_p:
        mov ax,@data
        mov ds,ax
        lea dx,m_e_2         ;out error message 
        mov ah,9h
        int 21h

;===================================================
error_p:mov ah,4ch
        int 21h
main 	endp


addr_ss proc near              ;to output the address
        mov dx,con_add
        mov al,cw_1
        out dx,al                    ; out control word 1 to control reg
       ;cccccccccccccccccccccccccccccccccccccc
        mov dx,data_add
      
        xchg bx,a_ress
        mov al,bl
        out dx,al
                            ;out the fist part of the address
     ;cccccccccccccccccccccccccccccccccccc
        mov dx,con_add
        mov al,cw_2
        out dx,al                    ; out control word 2 to control reg
       ;ccccccccccccccccccccccccccccccccccccccc
        mov dx,data_add
        mov al,bh                     ; out the second part of the address
        out dx,al
        xchg a_ress,bx

        ret
addr_ss endp


dat_a proc near             ;to input the data
start11: 
         mov tog,00
start1:
        call addr_ss
        mov dx,con_add
        mov al,cw_3
        out dx,al                    ; out control word 3 to control reg
       ;ccccccccccccccccccccccccccc
        mov dx,st_add             ;get the first nibbil of the byte in  the eprom
         in al,dx
        mov sck,al

        call addr_ss
        mov dx,con_add
        mov al,cw_3
        out dx,al                    ; out control word 3 to control reg
       ;ccccccccccccccccccccccccccc
        mov dx,st_add             ;get the first nibbil of the byte in  the eprom
         in al,dx
       cmp sck,al
       jne start1
       ;ccccccccccccccccccc1ccccccccccccc
       xchg cl,c_r
        ror al,cl
       xchg cl,c_r
        and al,0fh
        mov st_p,al
;cccccccccccccccccccccccccc
start2:
         call addr_ss
    ;ccccccccccccccccccccccccccc
        mov dx,con_add              ; out control word 4 to control reg  
        mov al,cw_4
        out dx,al
       ;ccccccccccccccccccccccccccccccccc
        mov dx,st_add             ;get the second nibbil of the byte in  the eprom
         in al,dx 
        mov sck,al

        call addr_ss
    ;ccccccccccccccccccccccccccc
        mov dx,con_add              ; out control word 4 to control reg  
        mov al,cw_4
        out dx,al
       ;ccccccccccccccccccccccccccccccccc
        mov dx,st_add             ;get the second nibbil of the byte in  the eprom
         in al,dx 
        cmp sck,al
        jne start2

        rol al,01h
        and al,0f0h

        xor al,st_p
        cmp tog,00
        jne st_2
        mov sck,al
        inc tog
        jmp start1
st_2:
        cmp sck,al
        jne start11

        ret
dat_a endp
        ;eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
	end main	
