Advertisement
Ham62

DOS Multikey.c

May 29th, 2021
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include "MoreTypes.h"
  2.  
  3. static ubyte KeyTbl[255];
  4. //static ushort oldHandler[2];
  5. ushort oldHandlerAddr;
  6. ushort oldHandlerSeg;
  7.  
  8. // http://www.intel-assembler.it/portale/5/keyboard-programming-in-asm/keyboard-programming-in-asm.asp
  9.  
  10. __declspec(naked) void __interrupt KeyboardHandler() {
  11.     _asm {
  12.         "PUSH AX"
  13.         "PUSH BX"
  14.         "PUSH CX"
  15.         "PUSH ES"
  16.  
  17.         "MOV BX, SEG KeyTbl"  // Set ES to segment of key table
  18.         "MOV ES, BX"
  19.         "LEA BX, KeyTbl"      // Set BX to address of key table
  20.        
  21.         "XOR AX, AX"     // Clear AX to 0
  22.         "IN AL, 60h"     // read key pressed/unpressed into AL
  23.         "ADD BX, AX"     // adjust pointer based on key down
  24.  
  25.         "CMP AL, 0E0h"   // Signifies an extended key code
  26.         "JE Int_Exit"    // Ignore extended keys
  27.        
  28.         "MOV CL, AL"     // check if significant bit is set
  29.         "AND AL, 128"    // (0 = key down, 1 = key up)
  30.         "JZ Key_Pressed"        
  31.  
  32.         "Key_Released:"
  33.         "AND [ES:BX], 0FF00h"
  34.         "JMP Int_Exit"
  35.  
  36.         "Key_Pressed:"
  37.         "OR [ES:BX], 00FFh"
  38.  
  39.  
  40.         "Int_Exit:"
  41.  
  42.         // this block just echos the key to the screen
  43.         /*"XOR BX, BX"
  44.         "MOV AL, CL"
  45.         "MOV AH, 0Eh"
  46.         "INT 10h"*/
  47.  
  48.         "POP ES"
  49.         "POP CX"
  50.         "POP BX"
  51.         "POP AX"
  52.  
  53.         // Call old keyboard handler now that we're done
  54.         "PUSH [oldHandlerSeg]"
  55.         "PUSH [oldHandlerAddr]"
  56.         "RETF"
  57.     }
  58. }
  59.  
  60. extern void InitKeyboard();
  61. #pragma aux InitKeyboard = \
  62.     "PUSH AX" \
  63.     "PUSH BX"\
  64.     "PUSH DX"\
  65.     "PUSH ES"\
  66.     "PUSH DS"\
  67.     \
  68.     "MOV AH, 35h" /* BIOS function to get interrupt handler */ \
  69.     "MOV AL, 9h"  /* Interrupt function INT 09h */ \
  70.     "INT 21h"     /* Returns pointer in ES:BX */ \
  71.     "MOV [oldHandlerSeg], ES" /* Save old pointer to restore after we're done */\
  72.     "MOV [oldHandlerAddr], BX"\
  73.     \
  74.     "MOV AH, 25h" /* Function to set interrupt vector to DS:DX */\
  75.     "MOV DX, CS" /* Move CS into DS */\
  76.     "MOV DS, DX"\
  77.     "LEA DX, KeyboardHandler" /* Get address of handler code */\
  78.     "INT 21h"\
  79.     \
  80.     "POP DS"\
  81.     "POP ES"\
  82.     "POP DX"\
  83.     "POP BX"\
  84.     "POP AX"\
  85.  
  86. extern void CloseKeyboard();
  87. #pragma aux CloseKeyboard = \
  88.     "PUSH AX"\
  89.     "PUSH DX"\
  90.     "PUSH DS"\
  91.     \
  92.     "MOV AH, 25h" /* Function to set interrupt vector for INT 09h */\
  93.     "MOV AL, 09h"\
  94.     "MOV DX, [oldHandlerAddr]"\
  95.     "MOV DS, [oldHandlerSeg]" /* Function gets pointer from DS:DX */\
  96.     "INT 21h"\
  97.     \
  98.     "POP DS"\
  99.     "POP DX"\
  100.     "POP AX"\
  101.  
  102. void main() {
  103.     int iX;
  104.     InitKeyboard();
  105.  
  106.     for (iX = 0; iX <= 0xFF; iX++)
  107.         KeyTbl[iX] = 0;
  108.  
  109.     while(!KeyTbl[0x1C]){
  110.     }
  111.    
  112.     CloseKeyboard();
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement