home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TRAPFLAG.ASM < prev    next >
Assembly Source File  |  1997-07-05  |  4KB  |  136 lines

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