home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progbas / qbnws203.arj / INTRPT2.ZIP / INTRPT2.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-09-16  |  10.4 KB  |  299 lines

  1. PAGE 81,132
  2. ;INTRPT2.ASM
  3. ;-----------------------------------------------------------------------|
  4. ;    --------          ---1991 Cornel H Huth     -------------------    |
  5. ;-----------------------------------------------------------------------|
  6. ;     date: 21 Jul 91                                                   |
  7. ; function: 80xxx INTERRUPT dispatcher for QuickBASIC/BASIC PDS         |
  8. ;   caller: FAR call QuickBASIC convention, reference (INTERRUPT(X))    |
  9. ;           call INTERRUPT(intnum%,ireg AS RegType,oreg AS RegType)     |
  10. ;           call INTERRUPTX(intnum%,ireg AS RegTypeX,oreg AS RegTypeX)  |
  11. ;    stack: +06 offset of return register pack                          |
  12. ;            08 offset of initial register pack                         |
  13. ;            10 interrupt number                                        |
  14. ;   return:  interrupt return registers                                 |
  15. ;     NOTE:  This routine is a direct replacement for QB's routine.     |
  16. ;     NOTE:  INT24 handler is installed temporarily to handle fatal     |
  17. ;            DOS errors without the Abort, Retry, Fail or system crash. |
  18. ;     NOTE:  If intnum% is out of range intnum% is NOT returned as -1   |
  19. ;            as the MS version does (no one is that stoopid, hey,hey).  |
  20. ;     NOTE:  The interrupt is done without writing to the code segment. |
  21. ;     NOTE:  b$SaveBP is no longer needed with PDS version since fatal  |
  22. ;            errors are handled locally, not by the BASIC INT24 handler.|
  23. ;     NOTE:  For verbose source comments see INTRPT.ASM in QBNWS105.    |
  24. ;-----------------------------------------------------------------------|
  25. ; This source file is based on:                                         |
  26. ; INTERRUPT - BASCOM software interrupt calling routine                 |
  27. ;       Copyright <C> 1986, 1987 Microsoft Corporation                  |
  28. ;-----------------------------------------------------------------------|
  29. ;This source was assembled using MASM 5.1. Previous or other versions of|
  30. ;assemblers should handle this source without any difficulty except for |
  31. ;perhaps the retf and retn mnemonics. Easy enough to use a macro or db. |
  32. ;-----------------------------------------------------------------------|
  33. WPTR    EQU <WORD PTR>
  34. BPTR    EQU <BYTE PTR>
  35.  
  36. Aintnum  EQU <WPTR [BP+10]>
  37. Aireg    EQU <WPTR [BP+08]>
  38. Aoreg    EQU <WPTR [BP+06]>
  39.  
  40. OldSI    EQU <WPTR [BP-02]>
  41. OldDI    EQU <WPTR [BP-04]>
  42. OldDS    EQU <WPTR [BP-06]>
  43. OldFLAGS EQU <WPTR [BP-08]>
  44. RegCnt   EQU <WPTR [BP-10]>
  45. RegES    EQU <WPTR [BP-12]>
  46. RegDS    EQU <WPTR [BP-14]>
  47. RegFLAGS EQU <WPTR [BP-16]>
  48. RegDI    EQU <WPTR [BP-18]>
  49. RegSI    EQU <WPTR [BP-20]>
  50. RegBP    EQU <WPTR [BP-22]>
  51. RegDX    EQU <WPTR [BP-24]>
  52. RegCX    EQU <WPTR [BP-26]>
  53. RegBX    EQU <WPTR [BP-28]>
  54. RegAX    EQU <WPTR [BP-30]>
  55.  
  56. LocalSize = 30
  57.  
  58. PostBP   EQU <WPTR [BP-34]> ;returned bp pushed here (after INT call return)
  59.  
  60. _BSS            SEGMENT WORD PUBLIC 'BSS'
  61. INT24error      dw 1 dup(?)
  62. INT24seg        dw 1 dup(?)
  63. INT24off        dw 1 dup(?)
  64. _BSS            ENDS
  65.  
  66. _DATA           SEGMENT WORD PUBLIC 'DATA'
  67. _DATA           ENDS
  68.  
  69. DGROUP          GROUP _DATA,_BSS
  70.  
  71. INTRPT2_TEXT    SEGMENT WORD PUBLIC 'CODE'
  72.                 ASSUME cs:INTRPT2_TEXT,ds:DGROUP,es:DGROUP,ss:DGROUP
  73.  
  74.                 PUBLIC INTERRUPT
  75. INTERRUPT       PROC FAR
  76.  
  77.                 push    bp
  78.                 mov     bp,sp
  79.                 sub     sp,LocalSize
  80.                 mov     RegCnt,8        ;8 registers passed for INTERRUPT
  81.                 jmp     SHORT INTRPT1
  82.  
  83.                 PUBLIC INTERRUPTX
  84. INTERRUPTX      LABEL FAR
  85.  
  86.                 push    bp
  87.                 mov     bp,sp
  88.                 sub     sp,LocalSize
  89.                 mov     RegCnt,10       ;10 passed for INTERRUPTX
  90.  
  91.                 ;set up for interrupt call
  92.  
  93. INTRPT1:        mov     bx,Aintnum
  94.                 mov     bx,[bx]         ;bx=interrupt to execute
  95.                 or      bh,bh           ;>255?
  96.                 jz      INTRPT2         ;no
  97.                 jmp     INTRPTxit       ;yes,just exit
  98.  
  99. INTRPT2:        push    bx
  100.                 mov     bl,1            ;trap fatal errors
  101.                 call    INT24switch
  102.                 pop     bx
  103.  
  104.                 mov     OldSI,si        ;save what we need to
  105.                 mov     OldDI,di
  106.                 mov     OldDS,ds
  107.                 pushf                   ;in case RegFLAGS changes DF?
  108.                 pop     OldFLAGS
  109.  
  110.                 cld
  111.                 mov     si,Aireg        ;ds:si->ireg pack
  112.                 mov     ax,ss
  113.                 mov     es,ax
  114.                 lea     di,RegAX        ;es:di->local stack vars
  115.                 mov     cx,RegCnt
  116.                 rep     movsw
  117.  
  118.                 push    bp              ;save original frame base
  119.  
  120.                 cmp     bl,25h          ;INT25?
  121.                 je      INTRPT3         ; or
  122.                 cmp     bl,26h          ;INT26?
  123.                 jne     INTRPT4         ;no
  124.  
  125.                 ;INT25/26 specific stack program
  126.  
  127. INTRPT3:        mov     ax,10
  128.                 push    ax              ;retf 10
  129.                 mov     ax,0CA90h       ;nop
  130.                 push    ax              ;sti
  131.                 mov     ax,0FB44h       ;inc sp
  132.                 push    ax              ;inc sp
  133.                 mov     ax,44FAh        ;cli    -ensure sp stays even
  134.                 push    ax
  135.                 jmp     SHORT INTRPT5
  136.  
  137.                 ;non-INT25/26 specific stack program
  138.  
  139. INTRPT4:        sub     ax,ax
  140.                 push    ax              ;retf 6
  141.                 mov     ax,06CAh
  142.                 push    ax
  143.  
  144.                 ;for either, the actual INT?? instruction
  145.  
  146. INTRPT5:        mov     ah,bl
  147.                 mov     al,0CDh
  148.                 push    ax              ;INT??
  149.  
  150.                 ;set up the far return address for the stack program
  151.  
  152.                 push    cs
  153.                 mov     ax,OFFSET INTRPT8 ;cs:ax->return to after INT??
  154.                 push    ax
  155.  
  156.                 ;store the address of the stack program--on the stack
  157.  
  158.                 push    ss
  159.                 mov     ax,sp
  160.                 add     ax,6            ;ss:ax->start of stack program
  161.                 push    ax
  162.  
  163.                 ;set up the registers for the INT call
  164.  
  165.                 mov     ax,RegFLAGS
  166.                 and     ax,0000111111010101b  ;mask valid 8086 flags
  167.                 push    ax                    ;save flags for a sec
  168.  
  169.                 mov     ax,RegAX
  170.                 mov     bx,RegBX
  171.                 mov     cx,RegCX
  172.                 mov     dx,RegDX
  173.                 mov     si,RegSI
  174.                 mov     di,RegDI
  175.  
  176.                 cmp     RegCnt,8        ;call INTERRUPT?
  177.                      je        INTRPT7
  178.  
  179.                 cmp     RegDS,-1        ;no,INTERRUPTX,load ds segment?
  180.                 je      INTRPT6
  181.                 mov     ds,RegDS        ;ds<>ss!
  182.  
  183. INTRPT6:        cmp     RegES,-1        ;how about es segment?
  184.                 je      INTRPT7
  185.                 mov     es,RegES
  186.  
  187. INTRPT7:        mov     bp,RegBP
  188.                 popf                    ;get the flags back
  189.  
  190.                 retf                    ;execute the stack program
  191.  
  192.                 ;come here after the stack program has executed the INT call
  193.  
  194. INTRPT8:        push    bp              ;save to PostBP
  195.                 mov     bp,sp
  196.                 mov     bp,[bp+2]       ;get original frame base
  197.  
  198.                 pushf                   ;get the return flags NOW
  199.                 pop     RegFLAGS
  200.  
  201.                 ;unhook temporary INT24 handler and process any error
  202.  
  203.                 push    bx              ;we want
  204.                 sub     bl,bl
  205.                 call    INT24switch
  206.                 pop     bx              ;we need
  207.                 cmp     ss:INT24error,0 ;fatal DOS error?
  208.                 je      INTRPT9         ;no
  209.                 or      RegFLAGS,1      ;set carry
  210.                 mov     ah,30h          ;clean up DOS
  211.                 int 21h
  212.                 mov     ax,ss:INT24error ;return DOS error
  213.  
  214. INTRPT9:        push    OldFlags
  215.                 popf
  216.  
  217.                 ;save returned registers and send them back
  218.  
  219.                 mov     RegAX,ax
  220.                 mov     RegBX,bx
  221.                 mov     RegCX,cx
  222.                 mov     RegDX,dx
  223.  
  224.                 mov     ax,PostBP       ;get BP value returned from INT call
  225.                 mov     RegBP,ax
  226.  
  227.                 mov     RegSI,si
  228.                 mov     RegDI,di
  229.                 mov     RegDS,ds
  230.                 mov     RegES,es
  231.  
  232.                 mov     ds,OldDS        ;ds=ss
  233.  
  234.                 lea     si,RegAX        ;ds:si->stack vars (ds=ss)
  235.                 mov     ax,ds
  236.                 mov     es,ax
  237.                 mov     di,Aoreg        ;es:di->oreg pack
  238.                 mov     cx,RegCnt
  239.                 rep     movsw
  240.  
  241.                 mov     si,OldSI
  242.                 mov     di,OldDI
  243.  
  244. INTRPTxit:      mov     sp,bp           ;deallocate locals
  245.                 pop     bp
  246.                 retf    3*2             ;we're done
  247.  
  248.                 ;temporary INT24 switch and handler
  249.  
  250. INT24switch:    push    es
  251.                 push    ds
  252.                 push    dx
  253.                 push    ax
  254.  
  255.                 or      bl,bl
  256.                 jz      INT24s2
  257.  
  258.                 mov     ss:INT24error,0
  259.                 mov     ax,3524h
  260.                 int 21h
  261.                 mov     ss:INT24seg,es
  262.                 mov     ss:INT24off,bx
  263.                 mov     ax,2524h
  264.                 mov     dx,OFFSET INT24handler
  265.                 push    cs
  266.                 pop     ds
  267. INT24s1:        int 21h
  268.                 pop     ax
  269.                 pop     dx
  270.                 pop     ds
  271.                 pop     es
  272.                 retn
  273.  
  274. INT24s2:        mov     ax,2524h
  275.                 mov     dx,ss:INT24off
  276.                 mov     ds,ss:INT24seg
  277.                 jmp     INT24s1
  278.  
  279.                 ;store the error number and return
  280.  
  281. INT24handler:   sti
  282.                 add     sp,6
  283.                 add     di,13h
  284.                 mov     ss:INT24error,di
  285.                 pop     ax
  286.                 pop     bx
  287.                 pop     cx
  288.                 pop     dx
  289.                 pop     si
  290.                 pop     di
  291.                 pop     bp
  292.                 pop     ds
  293.                 pop     es
  294.                 iret
  295.  
  296. INTERRUPT       ENDP
  297. INTRPT2_TEXT    ENDS
  298.                 END
  299.