
_TEXT           SEGMENT BYTE PUBLIC 'CODE'

        ASSUME  CS:_TEXT

                PUBLIC  DIR_MEM_TECLA

DIR_MEM_TECLA   DD      0

                PUBLIC  VECTOR_TECLADO

VECTOR_TECLADO  DD      0

                PUBLIC  GUARDA_VECTORES

GUARDA_VECTORES         PROC    NEAR
                PUSH    AX
                PUSH    BX
                PUSH    ES
                MOV     AH,35h          ;Guarda el vector del teclado.
                MOV     AL,09h
                INT     21h
                MOV     WORD PTR CS:[VECTOR_TECLADO],BX
                MOV     WORD PTR CS:[VECTOR_TECLADO+2],ES
                POP     ES
                POP     BX
                POP     AX
                RET
GUARDA_VECTORES         ENDP

                PUBLIC  _SET_VECTORES

_SET_VECTORES   PROC    NEAR
                CALL    GUARDA_VECTORES
                PUSH    BP
                MOV     BP,SP
                PUSH    AX
                PUSH    DS
                PUSH    DX
                PUSH    SI

                LDS     SI,[BP+4]
                MOV     WORD PTR CS:[DIR_MEM_TECLA],SI
                MOV     WORD PTR CS:[DIR_MEM_TECLA+2],DS

                MOV     DX,OFFSET CS:TECLADO
                MOV     AX,_TEXT
                MOV     DS,AX
                MOV     AL,09h
                MOV     AH,25h
                INT     21h             ;Cambia el vector del teclado.

                POP     SI
                POP     DX
                POP     DS
                POP     AX
                POP     BP
                RET
_SET_VECTORES   ENDP

                PUBLIC  _UNSET_VECTORES

_UNSET_VECTORES PROC    NEAR
                PUSH    BP
                MOV     BP,SP
                PUSH    AX
                PUSH    DS
                PUSH    DX

                MOV     DX,WORD PTR CS:[VECTOR_TECLADO]
                MOV     AX,WORD PTR CS:[VECTOR_TECLADO+2]
                MOV     DS,AX
                MOV     AL,09h
                MOV     AH,25h
                INT     21h             ;Repone el vector del teclado.

                POP     DX
                POP     DS
                POP     AX
                POP     BP
                RET
_UNSET_VECTORES ENDP

                PUBLIC  TECLADO

TECLADO         PROC    FAR
                PUSH    AX
                PUSH    BX
                PUSH    DS
                PUSH    SI
                LDS     SI,CS:[DIR_MEM_TECLA]
                IN      AL,60h
                MOV     AH,AL
                IN      AL,61h
                OR      AL,10000000b
                OUT     61h,AL
                AND     AL,01111111b
                OUT     61h,AL
                MOV     AL,AH
                MOV     BL,AL
                MOV     BH,00h
                AND     BX,007Fh
                CMP     AL,00h
                JE      TECLEADO
                CMP     AL,0E0h
                JE      TECLEADO
                CMP     AL,0F0h
                JE      TECLEADO
                CMP     BYTE PTR [SI],0E0h      ;£ltima tecla
                JNE     NO_DOBLE
                ADD     BX,128
NO_DOBLE:       TEST    AL,80h
                JZ      NO_SOLT
                MOV     BYTE PTR [BX+SI],0
                JMP     TECLEADO
NO_SOLT:        MOV     BYTE PTR [BX+SI],1
TECLEADO:       MOV     [SI],AL                 ;£ltima tecla
                LDS     SI,CS:[DIR_MEM_TECLA]
                MOV     BYTE PTR [SI+84],0
                MOV     BYTE PTR [SI+85],0
                MOV     AL,[SI+42]
                OR      AL,[SI+54]
                CMP     AL,1
                JNE     NO_IMPR_PANT
                CMP     BYTE PTR [SI+55],1
                JNE     NO_IMPR_PANT
                MOV     BYTE PTR [SI+84],1
NO_IMPR_PANT:   MOV     AL,[SI+29]
                OR      AL,[SI+157]
                CMP     AL,1
                JNE     NO_PAUSA
                CMP     BYTE PTR [SI+69],1
                JNE     NO_PAUSA
                MOV     BYTE PTR [SI+85],1
NO_PAUSA:       MOV     AL,20h
                OUT     20h,AL
                POP     SI
                POP     DS
                POP     BX
                POP     AX
                IRET
TECLADO         ENDP

_TEXT ends

      end