home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / DOS_HELP / ADVMSDOS.ZIP / BREAK.ASM < prev    next >
Encoding:
Assembly Source File  |  1986-06-20  |  5.8 KB  |  175 lines

  1.         page    55,132
  2.         title   Control-Break handler for Microsoft C programs
  3.         name    break
  4.  
  5. ;
  6. ; Control-Break Interrupt Handler for Microsoft C programs 
  7. ; running on IBM PCs (and ROM BIOS compatibles)
  8. ; Copyright (C) 1985 Ray Duncan
  9. ;
  10. ; This module allows C programs running on the IBM PC
  11. ; to retain control when the user enters a Control-Break
  12. ; or Control-C.  This is accomplished by taking over the
  13. ; Int 23H (MS-DOS Control-C) and Int 1BH (IBM PC 
  14. ; ROM BIOS Keyboard Driver Control-Break) interrupt 
  15. ; vectors.  The interrupt handler sets an internal
  16. ; flag (which must be declared STATIC INT) to TRUE within
  17. ; the C program; the C program can poll or ignore this 
  18. ; flag as it wishes.
  19. ; The module follows the Microsoft C parameter passing conventions.
  20. ;
  21. ; The Int 23H Control-C handler is a function of MS-DOS
  22. ; and is present on all MS-DOS machines, however, the Int 1BH
  23. ; handler is a function of the IBM PC ROM BIOS and will not
  24. ; necessarily be present on other machines. 
  25.  
  26. args    equ     4               ;offset of arguments, small model
  27.  
  28. cr      equ     0dh             ;ASCII carriage return
  29. lf      equ     0ah             ;ASCII line feed
  30.  
  31.  
  32. _TEXT   segment byte public 'CODE'
  33.  
  34.         assume cs:_TEXT
  35.  
  36.         public  _capture,_release       ;function names for C 
  37.  
  38.         page
  39. ;
  40. ; The function CAPTURE is called by the C program to
  41. ; take over the MS-DOS and keyboard driver Control-
  42. ; Break interrupts (1BH and 23H).  It is passed the
  43. ; address of a flag within the C program which is set
  44. ; to TRUE whenever a Control-Break or Control-C
  45. ; is detected.  The function is used in the form:
  46. ;               static int flag;
  47. ;               capture(&flag);
  48.  
  49. _capture proc   near            ;take over Control-Break 
  50.  
  51.         push    bp              ;interrupt vectors
  52.         mov     bp,sp
  53.         push    ds              ;save registers
  54.         push    di
  55.         push    si
  56.  
  57.         mov     ax,word ptr [bp+args]
  58.         mov     cs:flag,ax      ;save address of integer
  59.         mov     cs:flag+2,ds    ;flag variable in C program
  60.  
  61.                                 ;pick up original vector contents
  62.         mov     ax,3523h        ;for interrupt 23H (MS-DOS
  63.         int     21h             ;Control-C handler)
  64.         mov     cs:int23,bx
  65.         mov     cs:int23+2,es
  66.  
  67.         mov     ax,351bh        ;and interrupt 1BH 
  68.         int     21h             ;(IBM PC ROM BIOS keyboard driver
  69.         mov     cs:int1b,bx     ;Control-Break interrupt handler)
  70.         mov     cs:int1b+2,es
  71.  
  72.         push    cs              ;set address of new handler     
  73.         pop     ds
  74.         mov     dx,offset ctrlbrk
  75.         mov     ax,02523H       ;for interrupt 23H
  76.         int     21h
  77.         mov     ax,0251bH       ;and interrupt 1BH
  78.         int     21h
  79.  
  80.         pop     si
  81.         pop     di
  82.         pop     ds              ;restore registers and
  83.         pop     bp              ;return to C program
  84.         ret
  85.  
  86. _capture endp
  87.         page
  88. ;
  89. ; The function RELEASE is called by the C program to
  90. ; return the MS-DOS and keyboard driver Control-Break
  91. ; interrupt vectors to their original state.  Int 23h is
  92. ; also automatically restored by MS-DOS upon the termination
  93. ; of a process, however, calling RELEASE allows the C
  94. ; program to restore the default action of a Control-C
  95. ; without terminating.  The function is used in the form:
  96. ;
  97. ;               release();
  98. ;
  99.  
  100. _release proc   near            ;restore Control-Break interrupt
  101.                                 ;vectors to their original state        
  102.         push    bp
  103.         mov     bp,sp
  104.         push    ds              ;save registers
  105.         push    di
  106.         push    si
  107.  
  108.         mov     dx,cs:int1b     ;set interrupt 1BH
  109.         mov     ds,cs:int1b+2   ;(MS-DOS Control-C 
  110.         mov     ax,251bh        ;interrupt handler)     
  111.         int     21h
  112.  
  113.         mov     dx,cs:int23     ;set interrupt 23H
  114.         mov     ds,cs:int23+2   ;(IBM PC ROM BIOS keyboard driver 
  115.         mov     ax,2523h        ;Control-Break interrupt handler)
  116.         int     21h
  117.  
  118.         pop     si
  119.         pop     di
  120.         pop     ds              ;restore registers and
  121.         pop     bp              ;return to C program
  122.         ret
  123.  
  124. _release endp
  125.  
  126.         page
  127. ;
  128. ; This is the actual interrupt handler which is called by
  129. ; the ROM BIOS keyboard driver or by MS-DOS when a Control-C
  130. ; or Control-Break is detected.  Since the interrupt handler
  131. ; may be called asynchronously by the keyboard driver, it
  132. ; is severely restricted in what it may do without crashing
  133. ; the system (e.g. no calls on DOS allowed).  In this
  134. ; version, it simply sets a flag within the C program to
  135. ; TRUE to indicate that a Control-C or Control-Break has
  136. ; been detected; the address of this flag was passed
  137. ; by the C program during the call to the CAPTURE function.
  138. ;
  139.  
  140. ctrlbrk proc    far             ;Control-Break interrupt handler
  141.  
  142.         push    bx              ;save affected registers
  143.         push    ds
  144.  
  145.         mov     bx,cs:flag      ;set flag within C program
  146.         mov     ds,cs:flag+2    ;to "True"
  147.         mov     word ptr ds:[bx],-1
  148.         
  149.         pop     ds              ;restore registers and exit
  150.         pop     bx
  151.  
  152.         iret
  153.  
  154. ctrlbrk endp
  155.  
  156.  
  157. flag    dw      0,0             ;long address of C program's
  158.                                 ;Control-Break detected flag
  159.  
  160. int23   dw      0,0             ;original contents of MS-DOS
  161.                                 ;Control-C Interrupt 23H
  162.                                 ;vector
  163.         
  164. int1b   dw      0,0             ;original contents of ROM BIOS
  165.                                 ;keyboard driver Control-Break
  166.                                 ;Interrupt 1BH vector
  167.  
  168. _TEXT   ends
  169.  
  170.         end
  171.