rendered paste bodyTITLE kb_handler
DOSSEG
.MODEL SMALL
.STACK 100h
.DATA
hoffset dw ? ;Default Keyboard Handler Offset
hsegment dw ? ;Default Keyboard Handler Segment
tmhoffset dw ? ;Default Time Handler Offset
tmhsegment dw ? ;Default Time Handler Segment
inflag db 0h ;Flag to stop the handler
cnt db 0h ;Counter
shiftsw db 00h ;Shift Key Switch
capssw db 00h ;Caps lock Switch
timecount db 0h ;Time Counter
last db 0h ;Last Scan Code
.CODE
EXTRN scan2a:PROC
start: mov ax, @DATA
mov ds, ax
sub ax, ax
;Saving Current Keyboard Handler
mov al, 09h
mov ah, 35h
int 21h
mov hoffset, bx
mov hsegment, es
;Replacing Keyboard Handler
push ds
mov ax, cs
mov ds, ax
mov dx, OFFSET KBHANDLER
mov al, 09h
mov ah, 25h
int 21h
pop ds
;Saving Current Time Handler
mov al, 08h
mov ah, 35h
int 21h
mov tmhoffset, bx
mov tmhsegment, es
;Replacing Keyboard Handler
push ds
mov ax, cs
mov ds, ax
mov dx, OFFSET TMHANDLER
mov al, 08h
mov ah, 25h
int 21h
pop ds
koko: mov cl,inflag
cmp cl, 1h
jne koko
;Restoring original Keyboard handler
push ds
mov dx,hoffset
mov ds,hsegment
mov al, 09h
mov ah, 25h
int 21h
pop ds
;Restoring original Time handler
push ds
mov dx,tmhoffset
mov ds,tmhsegment
mov al, 08h
mov ah, 25h
int 21h
pop ds
;terminating
mov ax, 4C00h
int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The Keyboard Handler proccess
CHECK proc near
;Input: al : scan code
;Output: ah 1h to continue or 0h not to
cmp al, last ;Compare Scan code and Last scan code
je stp2 ;If equal, check time pressed
jmp succ ;Else, pass
stp2: mov ah,timecount ;Check Time
cmp ah, 09h
jae succ ;If >9 pass
fail: mov ah, 00h ;Failure
retn ;Ending
succ: mov last, al ;Refreshing "last"
mov timecount,00h ;Refreshing timer
mov ah, 01h ;Success
retn ;Ending
CHECK endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The Keyboard Handler proccess
KBHANDLER proc far
;Saving Registers
push ds
push ax
push bx
push cx
push dx
mov ax, @DATA
mov ds, ax
sub ax, ax
sti ;We ll need interupts
jmp handle
;Special Functions
escape: mov cl, 1h
mov inflag, cl ;Flag to terminate handler
jmp skip
shiftp: mov shiftsw, 01h ;flag
jmp skip
shiftr: mov shiftsw, 00h ;flag
jmp skip
;Dokimasa me to caps kai otan to pataga mia fora kai to afina
;edine to scancode tou Press enw otan to ksanaekana tou Release
;Opote to egrapsa etsi, diaferei apo ton tropo pou zitate
;(to ekana opws legate stin arxi, alla ithele 2 fores na patithei-afithei
;gia na energopoiithei...)
capsp: mov capssw, 02h ;flag
jmp skip
capsr: mov capssw, 00h ;flag
jmp skip
enter: mov cnt, 00h
mov dl, 10h ;Changing line
mov ah, 02h
int 21h
jmp bk
;Reading scan code
handle: in al, 60h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;ERWTIMA 3 allagi
call CHECK
cmp ah,00h
je skip
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Comparing for Special Use
cmp al, 1h ;Escape
je escape
cmp al, 2Ah ;Shift press
je shiftp
cmp al, 10101010b;Shift release
je shiftr
cmp al, 3Ah ;Caps press
je capsp
cmp al, 10111010b;Caps release
je capsr
cmp al, 80h ;Code of Release key
jae skip ;skips showing
conv: add cnt,1h ;Counter
mov dl, al
push dx ;Saving the Scan Code
mov al, 00000000b ;parameters
add al, shiftsw ;Shift switch
add al, capssw ;Caps switch
call scan2a
;printing letter
mov dl, al
mov ah, 02h
int 21h
;Going to the corner
mov ah, 02h
mov bh, 0
mov dh, 0
mov dl, 78
int 10h
;Printing scan code
pop dx ;Restoring scan code
call Disphex ;Printing
;Checking if newline
mov cl, cnt
cmp cl, 80d
je enter
;Going back on line
bk: mov bh, 0
mov dh, 24
mov dl, cnt
int 10h
;Signal to stop
skip: mov al, 20h
out 20h, al
;Restoring Registers
pop dx
pop cx
pop bx
pop ax
pop ds
iret
KBHANDLER endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The Time Handler proccess
TMHANDLER proc far
;Saving Registers
push ds
push ax
mov ax, @DATA
mov ds, ax
sub ax, ax
inc timecount
;Signal to stop
mov al, 20h
out 20h, al
;Restoring Registers
pop ax
pop ds
iret
TMHANDLER endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The disphex function
DispHex PROC NEAR
; Displays two hex digits
; Input: Byte to be displayed in DL (An integer mn)
; Output: None
; Uses: AX, CL, DX
push ax
push cx
push dx
mov cl, 4
xor dh, dh ; DX = 00mn
ror dx, cl ; DX = n00m
ror dh, cl ; DX = 0n0m
add dx, 3030h ; Convert m and n to ASCII (ASCII of "0" is 30h)
cmp dl, "9" ; if m>9 ...
jbe disp1
add dl, 7 ; ...then add 7 (ASCII of "A" is 41h = 3Ah + 7)
disp1: mov ah, 2
int 21h ; Display m
mov dl, dh ; Do the same for n
cmp dl, "9"
jbe disp2
add dl, 7
disp2: int 21h
pop dx
pop cx
pop ax
ret
DispHex ENDP
end start