	TITLE   test

	.386

_DATA	SEGMENT word public 'DATA'
	PUBLIC	kbd_pending
kbd_pending	dd	0
_DATA	ENDS

_BSS	SEGMENT word public 'BSS'
saveip	dd	?
savecs	dw	?
_BSS	ENDS

DGROUP	GROUP	_DATA,_BSS
CGROUP	GROUP	_TEXT

_TEXT	SEGMENT	dword public use32 'CODE'
	ASSUME	cs:CGROUP, ds:DGROUP

myds	dw	?

	PUBLIC	kb_init
kb_init PROC NEAR
	push	ds
	push	es
	push	ebx
	mov	ax,cs
	mov	es,ax
	assume	es:_TEXT
	mov	es:myds,ds
	mov	ax,03515h
	int	21h
	mov	saveip,ebx
	mov	savecs,es
	mov	edx,offset kb_int
	mov	ax,cs
	mov	ds,ax
	mov	ax,02515h
	int	21h
	pop	ebx
	pop	es
	pop	ds
	ret
kb_init	ENDP

kb_int	PROC FAR
	iretd
	push	eax
	push	eax
	push	ds
	push	eax
	mov	ds,cs:myds
	cmp	ah,4fh
	jne	ignore
	inc	kbd_pending
ignore:
	mov	eax,saveip
	mov	8[esp],eax
	movzx	eax,savecs
	mov	12[esp],eax
	pop	eax
	pop	ds
	ret
kb_int	ENDP

	PUBLIC	kb_exit
kb_exit	PROC NEAR
	push	ds
	mov	edx,saveip
	mov	ds,savecs
	mov	ax,02515h
	int	21h
	pop	ds
	ret
kb_exit	ENDP

_TEXT	ENDS
	END
