Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MoreTypes.h"
- static ubyte KeyTbl[255];
- //static ushort oldHandler[2];
- ushort oldHandlerAddr;
- ushort oldHandlerSeg;
- // http://www.intel-assembler.it/portale/5/keyboard-programming-in-asm/keyboard-programming-in-asm.asp
- __declspec(naked) void __interrupt KeyboardHandler() {
- _asm {
- "PUSH AX"
- "PUSH BX"
- "PUSH CX"
- "PUSH ES"
- "MOV BX, SEG KeyTbl" // Set ES to segment of key table
- "MOV ES, BX"
- "LEA BX, KeyTbl" // Set BX to address of key table
- "XOR AX, AX" // Clear AX to 0
- "IN AL, 60h" // read key pressed/unpressed into AL
- "ADD BX, AX" // adjust pointer based on key down
- "CMP AL, 0E0h" // Signifies an extended key code
- "JE Int_Exit" // Ignore extended keys
- "MOV CL, AL" // check if significant bit is set
- "AND AL, 128" // (0 = key down, 1 = key up)
- "JZ Key_Pressed"
- "Key_Released:"
- "AND [ES:BX], 0FF00h"
- "JMP Int_Exit"
- "Key_Pressed:"
- "OR [ES:BX], 00FFh"
- "Int_Exit:"
- // this block just echos the key to the screen
- /*"XOR BX, BX"
- "MOV AL, CL"
- "MOV AH, 0Eh"
- "INT 10h"*/
- "POP ES"
- "POP CX"
- "POP BX"
- "POP AX"
- // Call old keyboard handler now that we're done
- "PUSH [oldHandlerSeg]"
- "PUSH [oldHandlerAddr]"
- "RETF"
- }
- }
- extern void InitKeyboard();
- #pragma aux InitKeyboard = \
- "PUSH AX" \
- "PUSH BX"\
- "PUSH DX"\
- "PUSH ES"\
- "PUSH DS"\
- \
- "MOV AH, 35h" /* BIOS function to get interrupt handler */ \
- "MOV AL, 9h" /* Interrupt function INT 09h */ \
- "INT 21h" /* Returns pointer in ES:BX */ \
- "MOV [oldHandlerSeg], ES" /* Save old pointer to restore after we're done */\
- "MOV [oldHandlerAddr], BX"\
- \
- "MOV AH, 25h" /* Function to set interrupt vector to DS:DX */\
- "MOV DX, CS" /* Move CS into DS */\
- "MOV DS, DX"\
- "LEA DX, KeyboardHandler" /* Get address of handler code */\
- "INT 21h"\
- \
- "POP DS"\
- "POP ES"\
- "POP DX"\
- "POP BX"\
- "POP AX"\
- extern void CloseKeyboard();
- #pragma aux CloseKeyboard = \
- "PUSH AX"\
- "PUSH DX"\
- "PUSH DS"\
- \
- "MOV AH, 25h" /* Function to set interrupt vector for INT 09h */\
- "MOV AL, 09h"\
- "MOV DX, [oldHandlerAddr]"\
- "MOV DS, [oldHandlerSeg]" /* Function gets pointer from DS:DX */\
- "INT 21h"\
- \
- "POP DS"\
- "POP DX"\
- "POP AX"\
- void main() {
- int iX;
- InitKeyboard();
- for (iX = 0; iX <= 0xFF; iX++)
- KeyTbl[iX] = 0;
- while(!KeyTbl[0x1C]){
- }
- CloseKeyboard();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement