home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progbas / qbnws103.arj / UEVENT.ZIP / EVENTCHN.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-03-06  |  3.8 KB  |  105 lines

  1. ; *********** EVENTCHN.ASM *******************************************
  2.  
  3.         .model medium, basic
  4.         .data
  5.  
  6. IntNumber       dw      -1      ; the requested INT vector
  7.  
  8.         .code
  9.  
  10. PreviousVector  LABEL DWORD     ; placed in the code segment to...
  11. PVOffset        dw      -1      ; ...facilitate chaining
  12. PVSegment       dw      77      ; a signature to avoid multi-installs
  13.  
  14. EXTRN   SetUEvent:FAR           ; this is in the BC runtime
  15. EXTRN   B_OnExit:FAR            ;          ditto
  16.  
  17. InstallHandler  PROC intnum     ; intnum passed BYVAL
  18.  
  19. ; This takes care of wrapping our metaphorical tentacles around the
  20. ; PC.  We take over the interrupt vector, saving the previous contents
  21. ; so that we can put things back the way they were when we exit.
  22.  
  23.         cmp     PVSegment, 77   ; insure this is first time
  24.         jne     iherr
  25.         mov     ax, intnum      ; get the requested vector
  26.         cmp     ax, 255
  27.         ja      iherr           ; vector too high...
  28.         mov     dx, offset RemoveInt
  29.         push    cs              ; push far address of RemoveInt
  30.         push    dx              ; to register the exit routine
  31.         call    B_OnExit        ; so that we don't hang machine
  32.         or      ax, ax          ; registered OK?
  33.         jz      iherr           ; error: too many registered routines
  34.         mov     ax, intnum      ;  _onexit corrupted this
  35.         mov     IntNumber, ax   ; save for removal routine
  36.         mov     ah, 35H         ; use DOS to get current...
  37.         INT     21H             ; ...holder of the vector
  38.         mov     PVSegment, es   ; save for chaining, removal
  39.         mov     PVOffset, bx
  40.         push    ds              ; hold DS a moment
  41.         mov     bx, cs          ; transfer CS to DS
  42.         mov     ds, bx
  43.         mov     dx, offset IntHandler; get address of our MASM handler
  44.         mov     ah, 25H         ; tell DOS we're taking this vector
  45.         INT     21H             ; grab the vector, Victor
  46.         pop     ds              ; restore DS
  47.         xor     ax, ax          ; return zero: all OK
  48. @@:     ret
  49.  
  50. iherr:  mov     ax, -1          ; couldn't install: return TRUE
  51.         jmp     @b
  52.  
  53. InstallHandler  ENDP
  54.  
  55. RemoveInt       PROC uses ds
  56.  
  57. ; RemoveInt will undo the InstallHandler by restoring the PC to the 
  58. ; state it was in before we came along.  If we don't, the interrupt
  59. ; vector will point into volatile space when the program terminates.
  60. ;
  61. ; Because we "register" this routine with B_OnExit, restoring the 
  62. ; machine state becomes a part of BASIC's normal shutdown process...
  63. ; we don't need to call this routine explicitly.
  64.  
  65.         mov     ax, IntNumber           ; vector we're using
  66.         mov     ah, 25H                 ; SET VECTOR call
  67.         lds     dx, PreviousVector      ; saved original vector
  68.         INT     21H                     ; restore it
  69.         mov     PVSegment, 77           ; only needed for .QLB purposes
  70.         ret
  71.  
  72. RemoveInt       ENDP
  73.  
  74. IntHandler      PROC FAR  ;note that we don't just say "PROC" here
  75.  
  76. ; This routine notifies the BASIC runtime that an event has occurred.
  77. ; You MUST save and restore any registers you use.  In this example
  78. ; we're overly pessimistic, assuming that BC will destroy everything.
  79.  
  80.         assume  cs:@code, ds:nothing, es:nothing, ss:nothing
  81.  
  82.         push    ds              ; only save the registers you really need to
  83.         push    di
  84.         push    es
  85.         push    si
  86.         push    dx
  87.         push    cx
  88.         push    bx
  89.         push    ax
  90.         call    SetUEvent       ; notify BC/QB that the event occurred
  91.         pop     ax
  92.         pop     bx
  93.         pop     cx
  94.         pop     dx
  95.         pop     si
  96.         pop     es
  97.         pop     di
  98.         pop     ds
  99.         jmp     [PreviousVector]  
  100.  
  101. IntHandler      ENDP
  102.  
  103.         END
  104.  
  105.