(ASM program)
This assembly language program displays keyboard status such as modifier keys are down or up, lock keys are on or off, and the scan code and ascii code of the key pressed recently.
;###########################################################
;# Scan code and ASCII code #
;# tested in Turbo Assembler , version 3.0 #
;#---------------------------------------------------------#
;# Note: Parameter passing through stack (C style). #
;##########################6/5/2008#########################
.8086
.MODEL SMALL
NEWLINE EQU 13 , 10 ;
LINEFEED EQU 10 ;
X_AXIS EQU 18 ;
PROLOGUE MACRO ;;;;;;; subroutine prologue.
push bp ;; save the old base pointer value.
mov bp , sp ;; set the new base pointer value.
ENDM
EPILOGUE MACRO ;;;;;;; subroutine epilogue.
mov sp , bp ;; deallocate local variable.
pop bp ;; restore the caller's base pointer value.
ret
ENDM
.STACK
.DATA
kbStatus DW ?
left DB LINEFEED
DB "____KEY BORD____" , NEWLINE
DB LINEFEED
DB "Caps Lock :" , NEWLINE
DB "Num Lock :" , NEWLINE
DB "Scroll Lock :" , NEWLINE
DB "Insert Lock :" , NEWLINE
DB LINEFEED
DB "Caps lock key :" , NEWLINE
DB "Num Lock key :" , NEWLINE
DB "Scroll Lock key:" , NEWLINE
DB LINEFEED
DB "Left Alt :" , NEWLINE
DB "Right Alt :" , NEWLINE
DB "Left Ctrl :" , NEWLINE
DB "Right Ctrl :" , NEWLINE
DB "Left Shift :" , NEWLINE
DB "Right Shift :" , NEWLINE
DB '$'
center0 DB "__Last key__$"
center1 DB "Ascii value :$"
center2 DB "scan code :$"
down0 DB "Click mouse to exit.$"
down1 DB "http://programs.on.googlepages.com$"
loopVar DB ?
y_axis DB 3,4,5,6,8,9,10,12,13,14,15,16,17
bitPosi DB 6,5,4,7,14,13,12,9,11,8,10,1,0
on DB "on $"
off DB "off $"
up DB "up $"
down DB "down$"
.CODE
START:
;***********************************************************
; main procedure
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
main PROC
mov ax , 0003h ;
int 10h ; set 25 X 80 text mode.
mov dx, @DATA ;
mov ds , dx ; set data segment.
mov ax , 00h ;
int 33h ; initilize mouse driver.
mov ah , 12h ;
int 16h ;
xor ax , 7fffh ;
mov kbStatus , ax ; for initial printing of keyboard status.
mov dx , offset left ;
mov ah , 09h ;
int 21h ; set the information on the left of the screen.
mov ah , 11 ;
mov al , 41 ;
push ax ;
call PutCursor ;
mov dx, offset center0 ;
mov ah , 09h ;
int 21h ;
;
mov ah , 13 ;
mov al , 41 ;
push ax ;
call PutCursor ;
mov dx , offset center1 ;
mov ah , 09h ;
int 21h ;
;
mov ah , 14 ;
mov al , 41 ;
push ax ;
call PutCursor ;
mov dx , offset center2 ;
mov ah , 09h ;
int 21h ; set the information on the middle of the screen.
mov ah , 22 ;
mov al , 30 ;
push ax ;
call PutCursor ;
mov dx , offset down0 ;
mov ah , 09h ;
int 21h ;
;
mov ah , 24 ;
mov al , 22 ;
push ax ;
call PutCursor ;
mov dx , offset down1 ;
mov ah , 09h ;
int 21h ; set the information on the bottom of the screen.
add sp , 5*2 ; restore the stack
mainLoop:
mov ax , 03h ;
int 33h ;
cmp bx , 00h ;
je noMouseKey ;
call Exit0 ; terminate the process when mouse is clicked.
noMouseKey:
mov ah , 12h ;
int 16h ;
cmp ax , kbStatus ;
je noStatusChange ;
push ax ;
push kbStatus ;
mov kbStatus , ax ;
call PutStatus ;
add sp , 4 ; new keyboard status will be printed
call HideCursor ; if keyboard status changed.
noStatusChange:
mov ah , 01h ;
int 16h ;
jz mainLoop ;
call PutKey ;
call HideCursor ;
jmp mainLoop ; key codes will be printed if there is a key.
main ENDP
;-----------------------------------------------------------
;***********************************************************
; display the status of the keyboard
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PutStatus PROC
PROLOGUE
push si ;
mov loopVar , 0 ; initialize loop variable.
LoopContinue:
mov bx , offset bitPosi ;
mov cl , loopVar;
xor ch , ch ;
mov si , cx ;
push [bx+si] ;
push [bp+6] ;
push [bp+4] ;
call IsSameBit ;
add sp , 6 ;
cmp al , 0 ;
je incrementing; check the bit is changed.
push ax ; save the return value of IsSameBit.
mov al , X_AXIS ;
mov bx , offset y_axis ;
mov cl , loopVar;
xor ch , ch ;
mov si , cx ;
mov ah , [bx+si];
push ax ;
call PutCursor ;
add sp , 2 ; set the cursor position.
pop ax ; restore the return value of IsSameBit.
cmp ah , 0 ;
jne keyOnOrDown ; decide the key is on/up or off/down.
cmp loopVar , 3 ;
jle lockKeyOff ; decide the key is lock-key or not.
lea dx , up ;
jmp printThestr ; load "up" string to be printed.
lockKeyOff:
lea dx , off ;
jmp printTheStr ; load "off" string to be printed
keyOnOrDown:
cmp loopVar, 3 ;
jle lockKeyOn ; decide the key is lock-key or not.
lea dx , down ;
jmp printTheStr ; load "down" string to be printed
lockKeyOn: ;
lea dx , on ; load "on" string to be printed
printTheStr: ;
mov ah , 09h ;
int 21h ; print the loaded string.
incrementing: ;
inc loopVar ;
cmp loopVar , 12;
jle LoopContinue; increment and conditional part of the loop.
stopLooping:
pop si ; restore the registers.
EPILOGUE
PutStatus ENDP
;-----------------------------------------------------------
;***********************************************************
; print ascii code scan code
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PutKey PROC
PROLOGUE
sub sp , 2 ; make room for one local variable [bp-2]
mov ah , 00h ;
int 16h ;
mov [bp-2] , ax ; store key code in local variable.
mov ah , 13 ;
mov al , 57 ;
push ax ;
call PutCursor ;
add sp , 2 ; set the cursor position for ascii printing.
mov al , [bp-2] ;
cmp al , 0 ;
je noAsciiValue; Is there ascii value?
push [bp-2] ;
call PutHex ;
add sp , 2 ;
jmp scanCode ; prints the ascii value.
noAsciiValue: ;
mov cx , 3 ;
loopPutKey: ;
mov dl , ' ' ;
mov ah , 02h ;
int 21h ; clear the previous ascii value
loop loopPutKey ; since there is no ascii value.
scanCode: ;
mov ah , 14 ;
mov al , 57 ;
push ax ;
call PutCursor ;
add sp , 2 ;
mov ax , [bp-2] ;
mov al , ah ;
push ax ;
call PutHex ;
add sp , 2 ; prints the scan code.
EPILOGUE
PutKey ENDP
;-----------------------------------------------------------
;***********************************************************
; recieve one byte as parameter and print in Hexadecimal
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PutHex PROC
PROLOGUE
; display the first hex digit.
;
mov bx , [bp+4] ; [bp+4] is first parameter.
xor bh , bh ; reset the register.
shl bx , 4 ; shift the first hex digit to bh .
add bh , '0' ; convert the first digit to number.
cmp bh , '9' ; Is the first digit A,B,C,D,E or F.
jle print1stHex ; if not, print1stHex.
add bh , 7 ; convert the first digit to A,B,C,D,E or F.
print1stHex: ;
mov dl , bh ;
mov ah , 02h ;
int 21h ; write the first hex digit to stdout.
; display the second hex digit.
;
xor bh , bh ;
shr bx , 4 ;
add bl , '0' ;
cmp bl , '9' ;
jle print2ndHex ;
add bl , 7 ; get second hex digit.
print2ndHex: ;
mov dl , bl ;
int 21h ; write the second hex digit to stdout.
mov dl , 'h' ;
int 21h ; write the hex digit sign to stdout.
EPILOGUE
PutHex ENDP
;-----------------------------------------------------------
;***********************************************************
; receives one word as parameter and puts the cursor.
; low byte of parameter = row of the new cursor position.
; high byte of parameter = column of the new cursor position.
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PutCursor PROC
PROLOGUE
mov dx , [bp+4] ;
mov ah , 2h ;
xor bh , bh ;
int 10h ; set the curser
EPILOGUE
PutCursor ENDP
;-----------------------------------------------------------
;***********************************************************
; check whether the particular bit of two status are same.
; If so, set AL = nonzero; otherwise AL = zero and
; AH = status of that bit at new status
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IsSameBit PROC
PROLOGUE
mov ax , [bp+4] ; parameter1 - old status.
mov bx , [bp+6] ; parameter2 - new status.
mov cx , [bp+8] ; parameter3 - postion of the bit.
shr ax , cl ; the particular bit of old status become the LSb
and ax , 1h ; and all other bit become reset.
shr bx , cl ; the particular bit of new status become the LSb
and bx , 1h ; and all other bit become reset.
xor ax , bx ;
mov ah , bl ; make return value
EPILOGUE
IsSameBit ENDP
;-----------------------------------------------------------
;***********************************************************
; hide the cursor blinking
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
HideCursor PROC
mov ah , 1h ;
mov ch , 20h ;
int 10h ; hide cursor
ret
HideCursor ENDP
;-----------------------------------------------------------
;***********************************************************
; remove all mouse keys and return to DOS.
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exit0 PROC
mouseKey: ;
mov ax , 3h ;
int 33h ;
cmp bl , 0 ;
jne mouseKey ; eats all mouse keys.
mov ax , 4c00h ;
int 21h ; return to DOS.
Exit0 ENDP
;-----------------------------------------------------------
END START
;***********************************************************
;* More programs: http://programs.on.googlepages.com *
;* Please report bugs to: programs.on@gmail.com *
;***********************************************440*lines***
Click here to download the executable file of this program.
Desclaimer
I am not responsible for this exe file if it causes any damages.