home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / mslang / as / break.asm < prev    next >
Assembly Source File  |  1985-08-28  |  6KB  |  172 lines

  1.         title   Control-Break handler for Lattice C programs
  2.         name    break
  3.         include dos.mac
  4.  
  5. ;
  6. ; Control-Break Interrupt Handler for Lattice C programs 
  7. ; running on IBM PCs (and ROM BIOS compatibles)
  8. ; Ray Duncan, May 1985
  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-Break) 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 Lattice C parameter passing
  20. ; conventions, and also relies on the Lattice file DOS.MAC
  21. ; for the definition of certain constants and macros.
  22. ;
  23. ; The Int 23H Control-Break handler is a function of MS-DOS
  24. ; and is present on all MS-DOS machines, however, the Int 1BH
  25. ; handler is a function of the IBM PC ROM BIOS and will not
  26. ; necessarily be present on other machines. 
  27.         if      lprog
  28. args    equ     6               ;offset of arguments, Large models
  29.         else
  30. args    equ     4               ;offset of arguments, Small models
  31.         endif
  32.  
  33. cr      equ     0dh             ;ASCII carriage return
  34. lf      equ     0ah             ;ASCII line feed
  35.  
  36.         pseg
  37.  
  38.         public  capture,release ;function names for C
  39.  
  40.  
  41. ;
  42. ; The function CAPTURE is called by the C program to
  43. ; take over the MS-DOS and keyboard driver Control-
  44. ; Break interrupts (1BH and 23H).  It is passed the
  45. ; address of a flag within the C program which is set
  46. ; to TRUE whenever a Control-Break or Control-C
  47. ; is detected.  The function is used in the form:
  48. ;               static int flag;
  49. ;               capture(&flag)
  50. ;
  51.  
  52. capture proc    near            ;take over Control-Break 
  53.  
  54.         push    bp              ;interrupt vectors
  55.         mov     bp,sp
  56.         push    ds
  57.  
  58.         mov     ax,word ptr [bp+args]
  59.         mov     cs:flag,ax      ;save address of integer
  60.         mov     cs:flag+2,ds    ;flag variable in C program
  61.  
  62.                                 ;pick up original vector contents
  63.         mov     ax,3523h        ;for interrupt 23H (MS-DOS
  64.         int     21h             ;Control-Break handler)
  65.         mov     cs:int23,bx
  66.         mov     cs:int23+2,es
  67.  
  68.         mov     ax,351bh        ;and interrupt 1BH 
  69.         int     21h             ;(IBM PC ROM BIOS keyboard driver
  70.         mov     cs:int1b,bx     ;Control-Break interrupt handler)
  71.         mov     cs:int1b+2,es
  72.  
  73.         push    cs              ;set address of new handler     
  74.         pop     ds
  75.         mov     dx,offset ctrlbrk
  76.         mov     ax,02523H       ;for interrupt 23H
  77.         int     21h
  78.         mov     ax,0251bH       ;and interrupt 1BH
  79.         int     21h
  80.  
  81.         pop     ds              ;restore registers and
  82.         pop     bp              ;return to C program
  83.         ret
  84.  
  85. capture endp
  86.  
  87.  
  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
  105.  
  106.         mov     dx,cs:int1b     ;set interrupt 1BH
  107.         mov     ds,cs:int1b+2   ;(MS-DOS Control-Break 
  108.         mov     ax,251bh        ;interrupt handler)     
  109.         int     21h
  110.  
  111.         mov     dx,cs:int23     ;set interrupt 23H
  112.         mov     ds,cs:int23+2   ;(IBM PC ROM BIOS keyboard driver 
  113.         mov     ax,2523h        ;Control-Break interrupt handler)
  114.         int     21h
  115.  
  116.         pop     ds              ;restore registers and
  117.         pop     bp              ;return to C program
  118.         ret
  119.  
  120. release endp
  121.  
  122.  
  123. ;
  124. ; This is the actual interrupt handler which is called by
  125. ; the ROM BIOS keyboard driver or by MS-DOS when a Control-C
  126. ; or Control-Break is detected.  Since the interrupt handler
  127. ; may be called asynchronously by the keyboard driver, it
  128. ; is severely restricted in what it may do without crashing
  129. ; the system (e.g. no calls on DOS allowed).  In this
  130. ; version, it simply sets a flag within the C program to
  131. ; TRUE to indicate that a Control-C or Control-Break has
  132. ; been detected; the address of this flag was passed
  133. ; by the C program during the call to the CAPTURE function.
  134. ;
  135.  
  136. ctrlbrk proc    far             ;Control-Break interrupt handler
  137.  
  138.         push    bx              ;save affected registers
  139.         push    ds
  140.  
  141.         mov     bx,cs:flag      ;set flag within C program
  142.         mov     ds,cs:flag+2    ;to "True"
  143.         mov     word ptr ds:[bx],-1
  144.         
  145.         pop     ds              ;restore registers and exit
  146.         pop     bx
  147.  
  148.         iret
  149.  
  150. ctrlbrk endp
  151.  
  152.  
  153. flag    dw      0,0             ;long address of C program's
  154.                                 ;Control-Break detected flag
  155.  
  156. int23   dw      0,0             ;original contents of MS-DOS
  157.                                 ;Control-Break Interrupt 23H
  158.                                 ;vector
  159.         
  160. int1b   dw      0,0             ;original contents of ROM BIOS
  161.                                 ;keyboard driver Control-Break
  162.                                 ;Interrupt 1BH vector
  163.  
  164.  
  165.         endps
  166.  
  167.         end
  168.