home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv4_4 / overlays / ovrmgr.asm < prev    next >
Encoding:
Assembly Source File  |  1989-01-30  |  6.5 KB  |  167 lines

  1.                 .model  LARGE
  2.                 extrn   $$INTNO:byte
  3.                 extrn   $$MPGSNBASE:word
  4.                 extrn   $$MPGSNOVL:byte
  5.                 extrn   $$MPOVLLFA:word
  6.                 extrn   $$OVLBASE:byte
  7.  
  8.                 extrn   $$MAIN:far
  9.                 extrn   _read_overlay_section:far
  10.                 extrn   _errputs:far
  11.  
  12.                 .data                        
  13. _pspaddr        dw      ?               ;for relocation with read_overlay_section
  14. old_ovint       dw      ?               ;old overlay interrupt offset
  15.                 dw      ?               ;and segment
  16. _executable_name        db      80 dup (0)      ;space for full pathname
  17.  
  18. cantmap         db      'Can''t map overlay!...exiting',13,10,0
  19. reentry         db      'Already mapping an overlay!...exiting',13,10,0
  20.  
  21.  
  22.                 .code
  23.  
  24.                 public  $$OVLINIT
  25.                 public  _executable_name
  26.                 public  _pspaddr
  27.                 public  ax_save,bx_save,cx_save,es_save,ret_ip,ret_cs
  28.                 public  req_ov,ov_ofs,ov_seg,in_ovly_mgr,ovly_int,return_from_ov
  29.  
  30. ax_save         dw      ?               ;save area for used registers
  31. bx_save         dw      ?               
  32. cx_save         dw      ?
  33. es_save         dw      ?
  34.  
  35. ret_ip          dw      ?               ;original return address 
  36. ret_cs          dw      ?               ;(from call to overlaid routine)
  37.  
  38. req_ov          db      ?               ;requested overlay number
  39. ov_ofs          dw      ?               ;address to call after mapping
  40. ov_seg          dw      ?
  41.  
  42. in_ovly_mgr     db      0               ;flag to indicate already mapping
  43.  
  44. $$OVLINIT       label   far
  45.                 push    ax
  46.                 push    bx
  47.                 push    dx
  48.                 push    es
  49.                 push    ds                      ;need to use ds:dx
  50.                 mov     ax,DGROUP
  51.                 mov     ds,ax
  52.  
  53.                 mov     ax,es                   ;initialize _pspaddr 
  54.                 mov     _pspaddr,ax
  55.  
  56.                 mov     al,[$$INTNO]            ;get interrupt number
  57.                 mov     ah,35h                  ;get overlay number interrupt vector
  58.                 int     21h     
  59.  
  60.                 mov     old_ovint,bx
  61.                 mov     old_ovint+2,es
  62.  
  63.                 mov     bx,cs
  64.                 mov     ds,bx
  65.                 mov     dx,offset ovly_int      ;new interrupt
  66.                 mov     ah,25h
  67.                 int     21h                     ;install new int handler
  68.                 pop     ds                       
  69.                 pop     es
  70.                 pop     dx
  71.                 pop     bx
  72.                 pop     ax
  73.         
  74.                 jmp     $$MAIN                  ;go to mainline code 
  75.  
  76.  
  77. ;**
  78. ;** INTERRUPT HANDLER
  79. ;**
  80.  
  81. ovly_int        proc    far
  82.                 cmp     cs:in_ovly_mgr,0        ;are we not here already?
  83.                 je      ok                      ;right
  84. ; Error if we're already in overlay manager!
  85.                 mov     ax,offset reentry       ;error message
  86.                 push    ds
  87.                 push    ax
  88.                 call    _errputs
  89.                 add     sp,4
  90.                 mov     ax,4c41h                ;exit with error code 41h
  91.                 int     21h
  92.  
  93. ok:             mov     cs:in_ovly_mgr,1        ;note that we're here
  94.                 mov     cs:ax_save,ax
  95.                 mov     cs:bx_save,bx
  96.                 mov     cs:cx_save,cx
  97.                 mov     ax,es
  98.                 mov     cs:es_save,ax
  99.  
  100.                 pop     bx                      ;bx = return ip
  101.                 push    bx
  102.                 add     bx,3                    ;adjust for extra crap
  103.                 mov     cs:[ret_ip],bx          ;save it
  104.                 pop     bx                      ;get original retadd back
  105.  
  106.                 pop     ax                      ;return cs
  107.                 mov     cs:[ret_cs],ax          ;save it
  108.                 mov     es,ax                   ;es:bx -> bytes after INT
  109.  
  110.                 pop     ax                      ;flags, discard
  111.  
  112.                 xor     ah,ah
  113.                 mov     al,byte ptr es:[bx]     ;get requested overlay number
  114.                 inc     bx                      ;move to next item
  115.                 mov     byte ptr cs:req_ov,al   ;save it
  116.                 mov     cx,word ptr es:[bx]     ;get offset in overlay segment
  117.                 mov     cs:[ov_ofs],cx          ;save it
  118.         
  119.                 push    ax                      ;p3 for C function
  120.                 mov     bx,ax
  121.                 shl     bx,1                    ;* 2
  122.                 add     bx,offset $$MPGSNBASE   ;add to base of segment table
  123.                 mov     bx,[bx]
  124.                 mov     ov_seg,bx
  125.                 push    bx                      ;push the segment for p2
  126.                 mov     ax,0                    ;p2, low word (0)
  127.                 push    ax                      
  128.                 call    _read_overlay_section   ;call the pup!
  129.                 add     sp,6                    ;get rid of parms
  130.                 cmp     ax,0                    ;error return?
  131.                 je      no_error                ;no
  132.  
  133. ; Error if can't map this overlay!
  134.                 mov     ax,offset cantmap       ;error message
  135.                 push    ds
  136.                 push    ax
  137.                 call    _errputs
  138.                 add     sp,4
  139.                 mov     ax,4c42h                ;exit with error code 42h
  140.                 int     21h
  141.  
  142. no_error:       push    cs
  143.                 mov     ax,offset return_from_ov;trick up return to our code
  144.                 push    ax
  145.                 mov     ax,seg $$OVLBASE        ;set up call to overlay code
  146.                 push    ax
  147.                 mov     ax,ov_ofs               
  148.                 push    ax
  149.                 retf                            ;jump to overlay code...
  150.  
  151. return_from_ov:                                 ;..and come back here
  152.                 mov     ax,ret_cs               ;restore original retadd
  153.                 push    ax
  154.                 mov     ax,ret_ip
  155.                 push    ax
  156.                 mov     ax,cs:es_save           ;and original reg values
  157.                 mov     es,ax
  158.                 mov     ax,cs:ax_save
  159.                 mov     bx,cs:bx_save
  160.                 mov     cx,cs:cx_save
  161.                 mov     cs:in_ovly_mgr,0        ;we're not here anymore
  162.  
  163.                 ret
  164. ovly_int        endp
  165.  
  166.                 end
  167.