All pastes #275730 Raw Edit

Something

public text v1 · immutable
·published 2006-12-11 19:34 UTC
rendered paste body
TITLE kb_handler
DOSSEG

.MODEL SMALL
.STACK 100h
.DATA
	hoffset dw ?
	hsegment dw ?
	inflag db 0h ;Flag to stop the handler
	cnt db 0h ;Counter
	shiftsw db 00h ;Shift Key Switch
	capssw db 00h ;Caps lock Switch

.CODE
		EXTRN scan2a:PROC

	start:	mov ax, @DATA
		mov ds, ax
		sub ax, ax

		;Saving Current Hander
		mov al, 09h
		mov ah, 35h
		int 21h
		mov hoffset, bx
		mov hsegment, es

		;Replacing Handler
		push ds
		mov ax, cs
		mov ds, ax
		mov dx, OFFSET KBHANDLER
		mov al, 09h
		mov ah, 25h
		int 21h
		pop ds

koko:		mov cl,inflag
		cmp cl, 1h
		jne koko

		;Restoring original handler
		push ds
		mov dx,hoffset
		mov ds,hsegment
		mov al, 09h
		mov ah, 25h
		int 21h
		pop ds

		;terminating	
		mov ax, 4C00h
		int 21h

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The handler proccess

	KBHANDLER proc far
		;Saving Registers
		push ax
		push bx
		push cx
		push dx
		sti ;We ll need interupts
		jmp handle	

		;Special Functions
	escape: mov cl, 1h
		mov inflag, cl ;Flag to terminate handler
		jmp skip
	enter:	mov cnt, 00h
		mov dl, 10h ;Changing line
		mov ah, 02h
		int 21h
		jmp bk
	shiftp:	mov shiftsw, 01h ;flag
		jmp skip
	shiftr:	mov shiftsw, 00h ;flag
		jmp skip

		;Reading scan code
	handle:	in al, 60h
		;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, 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, 00h ;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
		iret
	KBHANDLER 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