www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/18/18:49:49

Xref: news2.mv.net comp.os.msdos.djgpp:2855
From: Jean-Pierre Cuvelliez <jpcuve AT innet DOT be>
Newsgroups: comp.os.msdos.djgpp
Subject: keyboard read
Date: Thu, 18 Apr 1996 22:48:31 +0200
Organization: INnet NV (post doesn't reflect views of INnet NV)
Lines: 186
Message-ID: <3176AA9F.64BC@innet.be>
NNTP-Posting-Host: pool02a-249.innet.be
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Here is the first piece of code I wrote with DJGPP. I want to read the keyboard. 128 
bits are used, bit set means key pressed, bit reset means key released.
This sometimes crashes when I run it under Windows 95, sometimes works fine, sometimes 
does not notice when I release a key.
I know I did not lock the stack, but how can I do that since its location is unknown? 
Do I need to lock all the data seg?

(assembly code)
				.data
kb_data:		.quad	0, 0			# 128 bits
kb_org_sel:		.word	0
kb_org_off:		.long	0
kb_data_end:

				.text

				.equ	kb_in, 0x60
				.equ	kb_ctl, 0x61

				.global	_kb_read		# (int), returns int
_kb_read:
				enterl	$0, $0
				movl	8(%ebp), %eax
				andl	$0x7F, %eax
				btl		%eax, kb_data
				jc		kb_read_99
				xorl	%eax, %eax
kb_read_99:
				leave
				ret

				.global	_kb_init		# (void)
_kb_init:
				enterl	$0, $0

				# clear buffer
				xorl	%eax, %eax
				movl	$kb_data, %edi
				cld
				stosl
				stosl

				# save original vector
				movb	$0x09, %bl
				movw 	$0x0204, %ax
				int		$0x31
				movw	%cx, kb_org_sel
				movl	%edx, kb_org_off

				# lock data
				movl	$kb_data, %eax
				pushl	%eax
				popw	%cx
				popw	%bx
				subl	$kb_data_end, %eax
				negl	%eax
				pushl	%eax
				popw	%di
				popw	%si
				movw	$0x0600, %ax
				int		$0x31
				jc		kb_init_99

				#lock code

				movl	$kb_int, %eax
				pushl	%eax
				popw	%cx
				popw	%bx
				subl	$kb_int_end, %eax
				negl	%eax
				pushl	%eax
				popw	%di
				popw	%si
				movw	$0x0600, %ax
				int		$0x31
				jc		kb_init_99

				# set new vector
				movb	$0x09, %bl
				movw	%cs, %ax
				movw	%ax, %cx
				movl	$kb_int, %edx
				movw	$0x0205, %ax
				int		$0x31
kb_init_99:
				leave
				ret

				.global _kb_done		# (void)
_kb_done:
				enterl $0,$0

				# restore original vector
				movb	$0x09, %bl
				movw	kb_org_sel, %cx
				movl	kb_org_off, %edx
				movw	$0x0205, %ax
				int		$0x31

				#unlock code
				movl	$kb_int, %eax
				pushl	%eax
				popw	%cx
				popw	%bx
				subl	$kb_int_end, %eax
				negl	%eax
				pushl	%eax
				popw	%di
				popw	%si
				movw	$0x0601, %ax
				int		$0x31
				jc		kb_done_99

				# unlock data
				movl	$kb_data, %eax
				pushl	%eax
				popw	%cx
				popw	%bx
				subl	$kb_data_end, %eax
				negl	%eax
				pushl	%eax
				popw	%di
				popw	%si
				movw	$0x0601, %ax
				int		$0x31
				jc		kb_done_99

kb_done_99:				
				leave
				ret

kb_int:
				cli
				pushf
				pushl	%eax

				# reset kb, scan code in al, bit7 = break 
				inb		$kb_in, %al
				pushw	%ax
				inb		$kb_ctl, %al
				movb	%al, %ah
				orb		$0x80, %al
				outb	%al, $kb_ctl
				xchgb	%al, %ah
				outb	%al, $kb_ctl
				popw	%ax

				# set/ reset bit
				testb	$0x80, %al
				jnz		kb_int_01
				andl	$0x7F, %eax
				btsl	%eax, kb_data
				jmp		kb_int_99
kb_int_01:
				andl	$0x7F, %eax
				btrl	%eax, kb_data
kb_int_99:
				movb	$0x20, %al
				outb 	%al, $0x20
				popl	%eax
				popf
				sti
				iret
kb_int_end:
				.end

(C code to test the stuff - not efficiently written but that's not the purpose)
#include <conio.h>

int main(int argc, char** argv)
{
	int i, j;
	kb_init();
	while(!kb_read(1))
	{
	printf("Read: ");
		for( j = 0; j < 128; j++)
		{
			if(kb_read(j)) printf("%U ", j);
		}
	printf("\n");
	}
	kb_done();
	exit(0);
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019