home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / n / n002 / 4.ddi / ECLSRCA.ZIP / ECLSUBX.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-05-13  |  18.6 KB  |  676 lines

  1.         PAGE    55,132
  2.         TITLE   ISRSUBX.ASM
  3.         SUBTTL  Copyright (C) 1990, South Mountain Software, Inc.
  4.  
  5. IFNDEF NUMBER
  6.         %OUT NUMBER MUST BE DEFINED, for example: /DNUMBER=1
  7.         %OUT ASSEMBLY ABORTED
  8.         END
  9. ENDIF
  10.  
  11. IFDEF LARGEMODEL
  12.                 %OUT ***** Assembling LARGE MODEL *****
  13. ELSE
  14.     IFDEF    MEDIUM
  15.                     %OUT ***** Assembling MEDIUM MODEL *****
  16.     ELSE
  17.                     %OUT ***** Assembling SMALL MODEL *****
  18.     ENDIF
  19. ENDIF
  20.  
  21.  
  22. DATAMAC MACRO numb
  23.         public  _isr&numb&_ax, _isr&numb&_ah, _isr&numb&_al, _isr&numb&_bx, _isr&numb&_bh, _isr&numb&_bl
  24.         public  _isr&numb&_cx, _isr&numb&_ch, _isr&numb&_cl, _isr&numb&_dx, _isr&numb&_dh, _isr&numb&_dl
  25.         public  _isr&numb&_si, _isr&numb&_di, _isr&numb&_bp, _isr&numb&_ds, _isr&numb&_es, _isr&numb&_flags
  26.         public  _Isr&numb&24Error, _Isr&numb&24Result
  27. IFNDEF VARS_IN_CS
  28. even
  29. ENDIF
  30. _isr&numb&_ax         label   word
  31. _isr&numb&_al         db      0
  32. _isr&numb&_ah         db      0
  33.  
  34. _isr&numb&_bx         label   word
  35. _isr&numb&_bl         db      0
  36. _isr&numb&_bh         db      0
  37.  
  38. _isr&numb&_cx         label   word
  39. _isr&numb&_cl         db      0
  40. _isr&numb&_ch         db      0
  41.  
  42. _isr&numb&_dx         label   word
  43. _isr&numb&_dl         db      0
  44. _isr&numb&_dh         db      0
  45.  
  46. _isr&numb&_bp         dw      0
  47. _isr&numb&_si         dw      0
  48. _isr&numb&_di         dw      0
  49. _isr&numb&_ds         dw      0
  50. _isr&numb&_es         dw      0
  51. _isr&numb&_flags      dw      0
  52.  
  53. _Isr&numb&24Error     db      0
  54. _Isr&numb&24Result    db      0
  55. ENDM
  56.  
  57. PUBLICMAC MACRO numb
  58. public  _initisr&numb,_freeisr&numb             ; so C can set up isr and free isr
  59. endm
  60.  
  61.  
  62. START_INIT MACRO numb
  63. ifdef LARGEMODEL
  64.         _initisr&numb        proc    far
  65. else
  66.     ifdef MEDIUM
  67.             _initisr&numb        proc    far
  68.     else
  69.             _initisr&numb        proc    near
  70.     endif
  71. endif
  72. ENDM
  73.  
  74. END_INIT MACRO numb
  75. _initisr&numb        endp
  76. ENDM
  77.  
  78. START_FREE MACRO numb
  79. ifdef LARGEMODEL
  80.         _freeisr&numb        proc    far
  81.         freeisr&numb         label   far
  82. else
  83.     ifdef MEDIUM
  84.             _freeisr&numb        proc    far
  85.             freeisr&numb         label   far
  86.     else
  87.             _freeisr&numb        proc    near
  88.             freeisr&numb         label   near
  89.     endif
  90. endif
  91.  
  92. ENDM
  93.  
  94. END_FREE MACRO numb
  95. _freeisr&numb        endp
  96. ENDM
  97.  
  98. IFDEF VARS_IN_CS
  99. %OUT *** Assembling for SWAPPING COMBO ***
  100. START_COPY MACRO numb
  101. public _copy_regs_isr&numb
  102. ifdef LARGEMODEL
  103.         _copy_regs_isr&numb        proc    far
  104.         copy_regs_isr&numb         label   far
  105. else
  106.     ifdef MEDIUM
  107.             _copy_regs_isr&numb        proc    far
  108.             copy_regs_isr&numb         label   far
  109.     else
  110.             _copy_regs_isr&numb        proc    near
  111.             copy_regs_isr&numb         label   near
  112.     endif
  113. endif
  114.  
  115. ENDM
  116.  
  117. END_COPY MACRO numb
  118. _copy_regs_isr&numb        endp
  119. ENDM
  120. ENDIF
  121.  
  122. MOVE_EM_IN MACRO numb
  123.  
  124.         mov     _isr&numb&_ds,ax
  125.  
  126.         pop     ax
  127.         mov     word ptr _isr&numb&_ax, ax                     ; initialize so C can access registers
  128.         mov     word ptr _isr&numb&_bx, bx
  129.         mov     word ptr _isr&numb&_cx, cx
  130.         mov     word ptr _isr&numb&_dx, dx
  131.         mov     _isr&numb&_si, si
  132.         mov     _isr&numb&_di, di
  133.         mov     _isr&numb&_bp, bp
  134.         mov     _isr&numb&_es,es
  135. ENDM
  136.  
  137. MOVE_EM_OUT MACRO numb
  138.         mov     ax, _isr&numb&_ax                             ; set registers so calling program can see result
  139.         mov     bx, _isr&numb&_bx
  140.         mov     cx, _isr&numb&_cx
  141.         mov     dx, _isr&numb&_dx
  142.         mov     si, _isr&numb&_si
  143.         mov     di, _isr&numb&_di
  144.         mov     bp, _isr&numb&_bp
  145.         mov     es,_isr&numb&_es
  146.         mov     ds,_isr&numb&_ds
  147. ENDM
  148.  
  149.  
  150.  
  151. MOVE_ERRORS MACRO numb
  152. IFNDEF VARS_IN_CS
  153.         mov     dgroup:_Isr&numb&24Error, 1           ; tell c a critical error occured
  154.         mov     ax, di
  155.         mov     dgroup:_Isr&numb&24Result, al         ; tell c which error it was
  156. ENDIF
  157. ENDM
  158.  
  159.  
  160. _text   segment  byte public 'CODE'             ; set up so C can access assembler
  161. _text   ends
  162. _data   segment  word public 'DATA'
  163. _data   ends
  164. _bss    segment  word public 'BSS'
  165. _bss    ends
  166.  
  167. dgroup  group  _bss, _data                ; group so c and assembly can share data
  168.         assume  cs:_text, ds:dgroup
  169.  
  170. _data   segment  word public 'DATA'
  171. IFNDEF VARS_IN_CS
  172. DATAMAC %NUMBER
  173.  
  174. IFNDEF STACKIS
  175.                 %OUT ***** Assembling with default 500 byte stack *****
  176. stack1a         db      100 dup ('STACK')
  177. ELSE
  178.                 %OUT ***** Assembling with STACKIS byte stack *****
  179. stack1a         db      STACKIS dup ('STACK')
  180. ENDIF
  181.  
  182. stack1          db      0
  183. ENDIF
  184.  
  185. _data   ends
  186.  
  187. _text   segment byte public 'CODE'              ; this is so C can call these routines
  188.         assume  cs:_text, ds:dgroup             ; make routines accessible to C
  189.  
  190. IFDEF VARS_IN_CS
  191. var_start label byte
  192. DATAMAC %NUMBER
  193. var_end label byte
  194. ENDIF
  195.  
  196. PUBLICMAC %NUMBER
  197.  
  198. IFDEF VARS_IN_CS
  199. IFNDEF STACKIS
  200.                 %OUT ***** Assembling with default 100 byte stack *****
  201. stack1a         db      100 dup ('STACK')
  202. ELSE
  203.                 %OUT ***** Assembling with STACKIS byte stack *****
  204. stack1a         db      STACKIS dup ('STACK')
  205. ENDIF
  206.  
  207. stack1          db      0
  208. ENDIF
  209.  
  210. oldint16        label   dword
  211. oldint16off     dw      0
  212. oldint16seg     dw      0
  213.  
  214. oldint1b        label   dword                   ; original bios control break handler
  215. oldint1boff     dw      0
  216. oldint1bseg     dw      0
  217.  
  218. oldint23        label   dword                   ; original control-c handler
  219. oldint23off     dw      0
  220. oldint23seg     dw      0
  221.  
  222. oldint24        label   dword                   ; original critical error interrupt handler
  223. oldint24off     dw      0
  224. oldint24seg     dw      0
  225.  
  226. oldint          label   dword
  227. oldintoff       dw      0
  228. oldintseg       dw      0
  229.  
  230. library         label   dword                   ; resident program that is called when hotkey is hit
  231. libraryoff      dw      0
  232. libraryseg      dw      0
  233.  
  234. isr_size        dw      0                       ; size of this isr in paragraphs
  235.  
  236. oldss           dw      0                       ; interrupted stack
  237. oldsp           dw      0
  238.  
  239. ourss           dw      0                       ; C's stack
  240. oursp           dw      0
  241.  
  242. ourfuncid       dw      0                       ; INT16h AX val to get MyId
  243. ourisrid        dw      0                       ; ID of this isr
  244.  
  245. ourpsp          dw      0                       ; C's psp
  246.  
  247. isrfunc         db      0
  248. oldbreak        db      0                       ; old break status
  249. dos3            db      0                       ; dos3 = 1 if running dos 3.x
  250.         
  251. inside_isr      db      0                       ; indicate in 16h isr already
  252.                                                 ; changed 11/9/87
  253.  
  254. dummy_iret:     iret                            ; dummy iret instruction
  255.  
  256. ;        initisr - Initialize C ISR
  257. ;        --------------------------
  258.  
  259. START_INIT %NUMBER
  260.         jmp     short past_flags
  261.  
  262.         ;*** put flags here so that they can be referenced later by C code
  263.         ;*** as an offset to the pointer for the function name _initisrX
  264. do_chain        db      1                       ; default to chain
  265.  
  266. do_skip         db      0                       ; default not to skip normal chaining logic
  267.  
  268.  
  269. past_flags:
  270.         push    bp
  271.         mov     bp, sp                          ; setup stack frame
  272.         push    ds
  273.         push    es
  274.         push    si
  275.         push    di
  276.         assume  ds:DGROUP
  277.         mov     ax, DGROUP                      ; address C's data segment
  278.         mov     ds, ax
  279. ifdef LARGEMODEL
  280.         mov     ax, [bp+6]
  281.         mov     isrfunc, al
  282.         mov     ax, [bp+12]
  283.         mov     do_chain, al
  284.         mov     ax, [bp+8]
  285.         mov     libraryoff, ax
  286.         mov     ax, [bp+10]
  287.         mov     libraryseg, ax
  288. else
  289.     ifdef MEDIUM
  290.             mov     ax, [bp+6]
  291.             mov     isrfunc, al
  292.             mov     ax, [bp+12]
  293.             mov     do_chain, al
  294.             mov     ax, [bp+8]
  295.             mov     libraryoff, ax
  296.             mov     ax, [bp+10]
  297.             mov     libraryseg, ax
  298.     else
  299.             mov     ax, [bp+4]
  300.             mov     isrfunc, al
  301.             mov     ax, [bp+8]
  302.             mov     do_chain, al
  303.             mov     ax, [bp+6]
  304.             mov     libraryoff, ax
  305.             mov     ax, cs
  306.             mov     libraryseg, ax
  307.     endif
  308. endif
  309.  
  310. ;       check version of dos
  311.  
  312.         mov     dos3, 0                         ; assume not dos 3.x
  313.         mov     ah, 30h                         ; to get DOS version number
  314.         int     21h                             ; version is AL.AH
  315.         cmp     al, 3                           ; see if dos 3.x
  316.         jb      notdos3
  317.         mov     dos3, 1                         ; well its dos 3.x
  318.  
  319. notdos3:
  320. IFDEF VARS_IN_CS
  321.         mov     ourss, cs
  322.         mov     ax, offset cs:stack1
  323. ELSE
  324.         mov     ourss, DGROUP
  325.         mov     ax, offset DGROUP:stack1
  326. ENDIF
  327.         mov     oursp, ax
  328.  
  329. ;       save old interrupts
  330.         cli
  331.         mov     al, isrfunc
  332.         mov     ah, 35h
  333.         int     21h
  334.         mov     oldintoff, bx
  335.         mov     oldintseg, es
  336.  
  337. ;       take over control - c to prevent break during context switch
  338.  
  339.         push    cs
  340.         pop     ds
  341.         mov     dx, offset dummy_iret           ; DS:DX=dummy vector to set
  342.         mov     ax, 2523h                       ; to set ^C handler through DOS
  343.         int     21h                             ; now, no break will occur
  344.  
  345. ;       replace with our isr
  346.  
  347.         mov     al, isrfunc
  348.         mov     ah, 25h
  349.         mov     dx, offset initlib
  350.         int     21h
  351.         sti
  352. exit_initisr:
  353.         pop     di                              ; restore regs there was a error
  354.         pop     si
  355.         pop     es
  356.         pop     ds
  357.         pop     bp
  358.         ret
  359.  
  360. END_INIT %NUMBER
  361.  
  362. ;        InitRes - Initialize And Run Resident Program And Also Do HouseKeeping
  363. ;        ----------------------------------------------------------------------
  364.  
  365. ifdef LARGEMODEL
  366.         initlib proc    far
  367. else
  368.     ifdef MEDIUM
  369.             initlib proc    far
  370.     else
  371.             initlib proc    near
  372.     endif
  373. endif
  374.     cli
  375.         pushf
  376.         cmp     inside_isr, 0                   ; *** prevent recursive entry
  377.         jz      okay_isr                        ; ***
  378.  
  379.         cmp     oldintoff, 0                    ; see if handler loaded
  380.         jnz     do_old_call                     ; yep so do it
  381.         cmp     cs:oldintseg, 0                 ; see if segment is nonzero
  382.         jnz     do_old_call
  383.         
  384.         popf
  385.         stc                                     ; set carry for error
  386.     sti
  387.         ret     2                               ; leave carry set
  388.  
  389. do_old_call:
  390.  
  391.         popf
  392.         jmp     oldint                          ; call previous handler
  393.  
  394. okay_isr:                                       ; ***
  395.         popf
  396.         mov     inside_isr,1                    ; tell everyone we are in int 16
  397.  
  398. ;        cmp     isrfunc,21h                     ; is this int 21h?
  399. ;        jne     were_not_21
  400.  
  401. were_not_21:
  402.         mov     oldss, ss                       ; save interrupted stack
  403.         mov     oldsp, sp
  404.         cli
  405.         mov     ss, ourss                       ; move C's stack in
  406.         mov     sp, oursp
  407.         sti
  408.  
  409.         push    ax
  410.         push    ds
  411.  
  412. IFNDEF VARS_IN_CS
  413.         mov     ax, dgroup
  414.         mov     ds, ax
  415.         assume  ds:dgroup
  416. ELSE
  417.         mov     ax, cs
  418.         mov     ds, ax
  419.         assume  ds:_text
  420. ENDIF
  421.         pop     ax
  422.  
  423.     MOVE_EM_IN %NUMBER
  424.  
  425.  
  426. ;       save important interrupts
  427.  
  428.         push    es
  429.         push    ds
  430.         assume  ds:nothing
  431.         xor     ax, ax
  432.         mov     ds, ax
  433.         cli                                             ; No Interrupts now
  434.         les     bx, ds:[24h * 4]                        ; Get Int 24 address
  435.         mov     oldint24off, bx                         ; save it
  436.         mov     oldint24seg, es
  437.  
  438.         les     bx, ds:[23h * 4]                        ; Get Int 23 address
  439.         mov     oldint23off, bx                         ; save it
  440.         mov     oldint23seg, es
  441.  
  442.         les     bx, ds:[1bh * 4]                        ; Get Int 1b address
  443.         mov     oldint1boff, bx                         ; save it
  444.         mov     oldint1bseg, es
  445.         sti                                             ; Interrupts back on
  446.  
  447. ;       load in our handlers
  448.  
  449.         xor     ax, ax
  450.         mov     ds, ax
  451.         cli                                             ; no interrupts now
  452.         mov     word ptr ds:[1bh * 4], offset dummy_iret
  453.         mov     word ptr ds:[1bh * 4 + 2], cs
  454.  
  455.         mov     word ptr ds:[23h * 4], offset dummy_iret
  456.         mov     word ptr ds:[23h * 4 + 2], cs
  457.  
  458.         mov     word ptr ds:[24h * 4], offset ourint24
  459.         mov     word ptr ds:[24h * 4 + 2], cs
  460.         sti                                             ; interrupts on
  461.         pop     ds
  462.         pop     es
  463.         assume  ds:dgroup
  464.  
  465.  
  466. ifdef LARGEMODEL
  467.         call    library                                 ; dword call to popup
  468. else
  469.     ifdef MEDIUM
  470.         call library
  471.     else
  472.             call    libraryoff                              ; near call to handler
  473.     endif
  474. endif
  475.  
  476. ;       see if normal return
  477.  
  478.  
  479.         cmp     ax, 0           ; if al != 0, set(ff) or clear zero flag
  480.                                 ; if ah != 0, set(ff) or clear carry flag
  481.         je      norm
  482.         inc     do_skip
  483.         cmp     ah, 0
  484.         je      set_zero
  485.         cmp     ah, 0ffh        ; if ah == ff, carry will be cleared here
  486.         cmc                     ; complement carry flag
  487.         jmp     short norm
  488. set_zero:
  489.         cmp     al, 0ffh        ; if al ne ff, zero will not be set
  490.         jne     norm
  491.         sub     ax, ax          ; this will set zero flag
  492. norm:
  493.         pushf
  494. ;       restore old vectors
  495.  
  496.         push    ds
  497.         push    es
  498.         xor     ax, ax
  499.         mov     es, ax
  500.  
  501.         cli                                             ; no interrupts now
  502.         lds     dx, oldint1b                            ; BIOS ctrl-break handler
  503.  
  504.         assume  ds:nothing
  505.  
  506.         mov     word ptr es:[1bh * 4], dx
  507.         mov     word ptr es:[1bh * 4 + 2], ds
  508.  
  509.         lds     dx, oldint23                            ; DOS ctrl-C
  510.         mov     word ptr es:[23h * 4], dx
  511.         mov     word ptr es:[23h * 4 + 2], ds
  512.  
  513.         lds     dx, oldint24                            ; DOS crit err handler
  514.         mov     word ptr es:[24h * 4], dx
  515.         mov     word ptr es:[24h * 4 + 2], ds
  516.         sti                                             ; interrupts back on
  517.         pop     es
  518. IFNDEF VARS_IN_CS
  519.         pop     ds                                      ; restore data seg DS
  520.         assume  ds:dgroup
  521. ELSE
  522.         pop     ax
  523.         push    CS
  524.         POP     DS
  525.         assume ds:_text
  526. ENDIF
  527.  
  528. ;       set return registers
  529.  
  530.     MOVE_EM_OUT %NUMBER
  531.  
  532.         assume  ds:nothing
  533.  
  534.         popf
  535.  
  536.         cli                                             ; restore interrupted stack
  537.         mov     ss, oldss
  538.         mov     sp, oldsp
  539.         sti
  540.  
  541.         pushf
  542.  
  543.         cmp     do_skip, 0
  544.         je      get_out_isr
  545.         dec     do_skip
  546.         jmp     short skip_call
  547. get_out_isr:                                            ;*** change 11/9/87
  548.         cmp     do_chain, 0
  549.         jz      skip_call
  550. try_old:
  551.         cmp     oldintoff, 0                            ; see if handler loaded
  552.         jnz     old_call                                ; yep so do it
  553.         cmp     oldintseg, 0                            ; see if segment is nonzero
  554.         jz      skip_call
  555.  
  556. old_call:
  557.         popf
  558.         mov     inside_isr, 0
  559.         jmp     oldint                                  ; call previous handler
  560. skip_call:
  561.         popf
  562.         push    ax
  563.         push    bp
  564.         mov     bp,sp
  565.         lahf
  566.         mov     al,ah
  567.         mov     [bp+8],al       ; replace flags byte on stack
  568.         pop     bp
  569.         pop     ax
  570.         mov     inside_isr, 0
  571.         iret                                            ; house cleaning is finished
  572. initlib endp
  573.  
  574. ;        Free ISR - Unload ISR From Memory
  575. ;        ---------------------------------
  576.  
  577.     START_FREE %NUMBER
  578.  
  579.         push    cx                              ; save regs just in case not successful
  580.         push    ds
  581.         push es
  582.         cli
  583.  
  584.         mov     ah, 25h                         ; restore previous user interrupt
  585.         mov     al, isrfunc
  586.         lds     dx, oldint
  587.         int     21h
  588.  
  589.         sti
  590.         xor     ax, ax                          ; tell C ISR is gone
  591.         pop     es
  592.         pop     ds
  593.         pop     cx
  594.         ret                                     ; return for final cleanup
  595.      END_FREE %NUMBER
  596.  
  597. IFDEF VARS_IN_CS
  598.     START_COPY %NUMBER
  599. IFDEF LARGEMODEL
  600. dir     equ     6          ; dir=0 for copy from code or =1 for copy to code
  601. ptr_parm     equ     8
  602. ELSE
  603. dir     equ     4
  604. ptr_parm     equ     6
  605. ENDIF
  606.         push    bp
  607.         mov     bp, sp
  608.         push    si
  609.         push    di
  610.         push    es
  611.         push    ds
  612.         push    cx
  613.         mov     cx, offset var_end - offset var_start  ;code_data variables
  614.         cmp     word ptr [bp+dir],0  ;is it copy from
  615.         jne     do_copy_to
  616. IFDEF LARGEMODEL
  617.         les     di,[bp+ptr_parm]
  618. ELSE
  619.         push    ds
  620.         pop     es
  621.         mov     di,[bp+ptr_parm]
  622. ENDIF
  623.         mov     si, offset cs:var_start
  624.         push    cs
  625.         pop     ds
  626.         jmp     short do_copy
  627. do_copy_to:
  628. IFDEF LARGEMODEL
  629.         lds     si,[bp+ptr_parm]
  630. ELSE
  631.         mov     si,[bp+ptr_parm]
  632. ENDIF
  633.         mov     di, offset var_start
  634.         push    cs
  635.         pop     es
  636. do_copy:
  637.         cld
  638.         rep     movsb
  639.         pop     cx
  640.         pop     ds
  641.         pop     es
  642.         pop     di
  643.         pop     si
  644.         pop     bp
  645.         ret
  646. END_COPY %NUMBER
  647. ENDIF
  648.  
  649.      
  650.      
  651. ;        Intercept of Interrupt 24h -- Critical Error Interrupt Handler
  652. ;        --------------------------------------------------------------
  653.  
  654. ourint24        proc    far
  655.         assume  ds:dgroup, es:nothing, ss:nothing
  656.         push    ds
  657.         push    ax
  658.         mov     ax, dgroup
  659.         mov     ds, ax
  660.     MOVE_ERRORS %NUMBER
  661.         pop     ax
  662.         pop     ds
  663.         mov     al, 3                           ; AL=3=fail system call
  664.         cmp     dos3, 1
  665.         jz      exit24
  666.         xor     al, al                          ; NO - have to ignore err then
  667.  
  668. Exit24:
  669.         iret                                    ; return to DOS
  670. ourint24        endp
  671.  
  672. _text   ends
  673.  
  674.         end
  675.  
  676.