home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_06 / 1n06015a < prev    next >
Text File  |  1990-08-13  |  4KB  |  129 lines

  1.         PAGE ,132
  2.  
  3. ;  Figure 1
  4. ;  Install a custom Interrupt 23 (Ctrl-C exception) handler
  5.  
  6.  
  7. %       .MODEL  memodel,lang            ;Add model and language support via
  8.                                         ;command line macros, e.g.
  9.                                         ;MASM /Dmemodel=LARGE /Dlang=C
  10.  
  11. kbstatseg       equ     40h
  12. kbstatofs       equ     17h
  13. ctl_on          equ     0100b
  14. alt_on          equ     1000b
  15. sk_c            equ     2eh
  16. EOI             equ     20h
  17. PIC             equ     20h
  18. kb_inp          equ     60h
  19. kb_outp         equ     61h
  20.  
  21.         .CODE
  22.  
  23. _oldvec09       dd      ?
  24. _oldvec1b       dd      ?
  25.  
  26.         public  ccrcvd
  27.  
  28. ccrcvd          dw      0
  29.  
  30. ;
  31. ;  This is our actual ISR
  32. ;
  33. myint09:
  34.         push    ax                      ;save AX...
  35.         pushf                           ;  ...& flags
  36.         in      al,kb_inp               ;get scan code
  37.         cmp     al,sk_c                 ;'C'?
  38.         jne     do_old                  ;no, forget it
  39.  
  40.         push    ax                      ;yes, save it...
  41.         push    es                      ;...& ES reg
  42.         mov     ax,kbstatseg            ;read keyboard status from 40:17
  43.         mov     es,ax
  44.         mov     al,es:kbstatofs
  45.         test    al,ctl_on               ;Ctrl pressed?
  46.         pop     es                      ;(restore AX, ES)
  47.         pop     ax
  48.         jz      do_old                  ;no, forget it
  49.  
  50.         in      al,kb_outp              ;yes, toggle keyboard acknowledge line
  51.         mov     ah,al
  52.         or      al,80h
  53.         out     kb_outp,al
  54.         xchg    al,ah
  55.         out     kb_outp,al
  56.         cli
  57.         mov     ax,EOI                  ;send end-of-interrupt code
  58.         out     PIC,al
  59.         mov     ax,1
  60.         mov     CS:ccrcvd,ax
  61.         pop     ax                      ;discard original flags...
  62.         pop     ax                      ; ...& AX
  63.         iret                            ;all done
  64. do_old:
  65.         popf                            ;restore flags...
  66.         pop     ax                      ; ...& AX
  67.         jmp     dword PTR CS:_oldvec09  ;call our handler
  68.  
  69. ;
  70. ;  To avoid keyboard confusion, trap Ctrl-Break separately
  71. ;
  72. myint1b:
  73.         push    ax
  74.         pushf
  75.         mov     ax,-1
  76.         mov     CS:ccrcvd,ax
  77.         popf
  78.         pop     ax
  79.         iret
  80.  
  81. ;
  82. ;  Call this to uninstall our ISR
  83. ;
  84. undo09  PROC    USES DX DS AX
  85.         mov     dx, word PTR CS:_oldvec09 ;restore original keyboard vector
  86.         mov     ds, word PTR CS:_oldvec09+2
  87.         mov     ax,2509h
  88.         int     21h
  89.  
  90.         mov     dx, word PTR CS:_oldvec1b ;restore original keyboard vector
  91.         mov     ds, word PTR CS:_oldvec1b+2
  92.         mov     ax,251bh
  93.         int     21h
  94.  
  95.         ret
  96. undo09  ENDP
  97.  
  98. ;
  99. ;  Call this to install  our ISR
  100. ;
  101. ins09   PROC    USES AX BX DS ES
  102.  
  103.         mov     ax,3509h                ;get old keyboard ISR vector...
  104.         int     21h
  105.         mov     word PTR CS:_oldvec09,bx
  106.         mov     word PTR CS:_oldvec09+2,es ;...and save it
  107.  
  108.         mov     ax,351bh                ;get old exit vector...
  109.         int     21h
  110.         mov     word PTR CS:_oldvec1b,bx
  111.         mov     word PTR CS:_oldvec1b+2,es ;...and save it
  112.  
  113.         push    cs                      ;get myint09 segment in DS
  114.         pop     ds
  115.         mov     dx, OFFSET myint09      ;install myint09 in int 09h
  116.         mov     ax,2509h
  117.         int     21h
  118.  
  119.         push    cs                      ;get myint1b segment in DS
  120.         pop     ds
  121.         mov     dx, OFFSET myint1b      ;install myint1b in int 1bh
  122.         mov     ax,251bh
  123.         int     21h
  124.  
  125.         ret
  126. ins09   ENDP
  127.  
  128.         end
  129.