home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advmsdos / chap05 / break.asm next >
Encoding:
Assembly Source File  |  1988-10-01  |  4.7 KB  |  164 lines

  1.         title   Ctrl-C & Ctrl-Break Handlers
  2.         page    55,132
  3.  
  4. ;
  5. ; Ctrl-C and Ctrl-Break handler for Microsoft C 
  6. ; programs running on IBM PC compatibles
  7. ; by Ray Duncan
  8. ;
  9. ; Assemble with:  C>MASM /Mx BREAK;
  10. ;
  11. ; This module allows C programs to retain control
  12. ; when the user enters a Ctrl-Break or Ctrl-C.  
  13. ; It uses Microsoft C parameter passing conventions
  14. ; and assumes the C small memory model.
  15. ;
  16. ; The procedure _capture is called to install
  17. ; a new handler for the Ctrl-C and Ctrl-Break
  18. ; interrupts (1BH and 23H).  _capture is passed
  19. ; the address of a static variable, which will be
  20. ; set to True by the handler whenever a Ctrl-C 
  21. ; or Ctrl-Break is detected.  The C syntax is:
  22. ;               static int flag;
  23. ;               capture(&flag);
  24. ;
  25. ; The procedure _release is called by the C program
  26. ; to restore the original Ctrl-Break and Ctrl-C
  27. ; handler.  The C syntax is:
  28. ;
  29. ;               release();
  30. ;
  31. ; The procedure ctrlbrk is the actual interrupt 
  32. ; handler.  It receives control when a software 
  33. ; Int 1BH is executed by the ROM BIOS or Int 23H 
  34. ; is executed by MS-DOS.  It simply sets the C 
  35. ; program's variable to True (1) and returns.
  36. ;
  37.  
  38. args    equ     4               ; stack offset of arguments, 
  39.                                 ; C small memory model
  40.  
  41. cr      equ     0dh             ; ASCII carriage return
  42. lf      equ     0ah             ; ASCII line feed
  43.  
  44.  
  45. _TEXT   segment word public 'CODE'
  46.  
  47.         assume cs:_TEXT
  48.  
  49.  
  50.         public  _capture
  51. _capture proc   near            ; take over Control-Break 
  52.                                 ; and Ctrl-C interrupt vectors
  53.  
  54.         push    bp              ; set up stack frame
  55.         mov     bp,sp
  56.  
  57.         push    ds              ; save affected registers
  58.         push    di
  59.         push    si
  60.  
  61.                                 ; save address of 
  62.                                 ; calling program's "flag"
  63.         mov     ax,word ptr [bp+args]
  64.         mov     word ptr cs:flag,ax
  65.         mov     word ptr cs:flag+2,ds
  66.  
  67.                                 ; save address of original
  68.         mov     ax,3523h        ; Int 23H handler
  69.         int     21h
  70.         mov     word ptr cs:int23,bx
  71.         mov     word ptr cs:int23+2,es
  72.  
  73.         mov     ax,351bh        ; save address of original
  74.         int     21h             ; Int 1BH handler
  75.         mov     word ptr cs:int1b,bx
  76.         mov     word ptr cs:int1b+2,es
  77.  
  78.         push    cs              ; set DS:DX = address
  79.         pop     ds              ; of new handler
  80.         mov     dx,offset _TEXT:ctrlbrk
  81.  
  82.         mov     ax,02523H       ; set Int 23H vector
  83.         int     21h
  84.  
  85.         mov     ax,0251bH       ; set Int 1BH vector
  86.         int     21h
  87.  
  88.         pop     si              ; restore registers     
  89.         pop     di
  90.         pop     ds
  91.  
  92.         pop     bp              ; discard stack frame
  93.         ret                     ; and return to caller
  94.  
  95. _capture endp
  96.  
  97.  
  98.         public  _release
  99. _release proc   near            ; restore original Ctrl-C
  100.                                 ; and Ctrl-Break handlers
  101.  
  102.         push    bp              ; save registers
  103.         push    ds
  104.         push    di
  105.         push    si
  106.  
  107.         lds     dx,cs:int1b     ; get address of previous
  108.                                 ; Int 1BH handler
  109.  
  110.         mov     ax,251bh        ; set Int 1BH vector
  111.         int     21h
  112.  
  113.         lds     dx,cs:int23     ; get address of previous
  114.                                 ; Int 23H handler
  115.  
  116.         mov     ax,2523h        ; set Int 23H vector
  117.         int     21h
  118.  
  119.         pop     si              ; restore registers     
  120.         pop     di              ; and return to caller
  121.         pop     ds
  122.         pop     bp
  123.         ret
  124.  
  125. _release endp
  126.  
  127.  
  128. ctrlbrk proc    far             ; Ctrl-C and Ctrl-Break
  129.                                 ; interrupt handler
  130.  
  131.         push    bx              ; save registers
  132.         push    ds
  133.  
  134.                                 
  135.         lds     bx,cs:flag      ; get address of C program's
  136.                                 ; "flag variable"
  137.  
  138.                                 ; and set the flag "true"
  139.         mov     word ptr ds:[bx],1
  140.         
  141.         pop     ds              ; restore registers
  142.         pop     bx
  143.  
  144.         iret                    ; return from handler
  145.  
  146. ctrlbrk endp
  147.  
  148.  
  149. flag    dd      0               ; far pointer to caller's
  150.                                 ; Ctrl-Break or Ctrl-C flag
  151.  
  152. int23   dd      0               ; address of original 
  153.                                 ; Ctrl-C handler
  154.         
  155. int1b   dd      0               ; address of original
  156.                                 ; Ctrl-Break handler
  157.  
  158. _TEXT   ends
  159.  
  160.         end
  161.  
  162.