home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCACHSRC.ZIP / OVLPARNT.ASM < prev    next >
Assembly Source File  |  1990-07-12  |  4KB  |  116 lines

  1. ;OVLPARNT.COM                       B.Kauler
  2. ;This program demonstrates overlays.
  3. ;The parent and child are both .COM files in this case.
  4. ;The child is named DEMO.OVL.
  5. ;
  6. comseg segment
  7.         assume ds:comseg, cs:comseg, ss:comseg, es:comseg
  8.         org 100h
  9. main1   proc    far
  10.         jmp     code_starts
  11. ;...................................................................
  12. ;data...
  13. child_filespec db "demo.ovl",0  ;put path if reqd.
  14. child_offset   dw   0   ;address of start of child program.
  15. child_segment  dw   0,0,0,0,0,0,0,0   ;       /
  16. errflag        db   0
  17. message1       db "start of parent process",0Ah,0Dh,"$"
  18. message2       db "back in parent process....$"
  19. errmsg         db "A memory deallocation or file access ERROR occurred$"
  20. ;...................................................................
  21. code_starts:
  22. ;the parent program doesn't do anything, except display a
  23. ;message, load the child, transfer control to it, then
  24. ;another message upon return, then back to DOS...
  25.         mov     dx,offset message1
  26.         mov     ah,9
  27.         int     21h
  28. ;The stack pointer is way out at the end of the 64K segment, so move it in...
  29.         mov bx,offset end_prog+126
  30.         mov sp,bx
  31. ;...since the overlay is to load after it....
  32. ;END_PROG is a label defined at the very end of the parent
  33. ;program.  ES must point to start of PSP.
  34. ;The extra 128 bytes is for the stack-- .COM format automatically
  35. ;puts SP at the end of the segment, and we are here defining
  36. ;only as much as we think we'll need, and deallocating the
  37. ;rest of the segment.
  38. ;Function4Ah will define free memory to start from END_PROG+128...
  39.         mov     bx,offset end_prog+144 ;use 144 so will be 1 para beyond SP.
  40.         mov     cl,4            ;convert block-size to paragraphs.
  41.         shr     bx,cl           ;       /
  42.         mov     ah,4Ah          ;SET_BLOCK. (ES:BX(para)-->)
  43.         int     21h             ;       /
  44.         jnc     nodeallocerr
  45.         mov     errflag,1
  46.         jmp     short errcond
  47. nodeallocerr:
  48. ;Some shuffling, to calculate the start of the new block...
  49.         mov     ax,es           ;note ES points to start of PSP.
  50.         add     ax,bx
  51.         inc     ax              ;to be sure new block beyond current program.
  52.         mov     child_offset,0
  53.         mov     child_segment,ax
  54. ;now the really interesting bit....
  55.         call    load_ovl        ;load DEMO.OVL
  56. ;DEMO.OVL is now loaded, with CHILD_ADDRESS containing
  57. ;the starting address of the child program....
  58. ;however before we can jump to it, think about DS...
  59. ;probably the child will want to access its own data area, so...
  60.         mov     ax,child_segment
  61.         mov     ds,ax
  62. ;another thought... if the child is written with a RETF at the end,
  63. ;we can go to it using a far CALL, which simplifies the return...
  64.         call    dword ptr cs:child_offset
  65. ;that's it, back in the parent program....
  66. ;restore original DS (=CS in .COM program)....
  67.         push    cs
  68.         pop     ds
  69. ;a message to acknowledge the fact.....
  70.         mov     dx,offset message2
  71.         mov     ah,9
  72.         int     21h
  73. ;do we need to close the child-file? better do it...
  74.         call unloadovl
  75.         cmp     errflag,0
  76.         je      backtodos
  77.         jmp     short backtodos
  78. errcond: mov    dx,offset errmsg
  79.         mov     ah,9
  80.         int     21h
  81.         mov     errflag,1   ;flag not used in this simple prog.
  82. backtodos:
  83.         mov     al,0
  84.         mov     ah,4Ch
  85.         int     21h
  86. main1   endp
  87. ;..............................................................
  88. load_ovl  proc near
  89. ;now we can load the child program....
  90.         mov     bx,offset child_segment
  91.         mov     dx,offset child_filespec
  92.         mov     al,3            ;method code.
  93.         mov     ah,4Bh
  94.         int     21h             ;EXEC
  95.         jnc     short commonexit
  96. error2: mov     errflag,2
  97. commonexit:
  98.         ret
  99. load_ovl endp
  100. ;.........
  101. unloadovl proc near
  102. ;wise to deallocate the memory that the overlay was occupying...
  103.         mov     es,child_segment
  104.         mov     ah,49h
  105.         int     21h
  106.         jnc     deallocokay
  107.         mov     errflag,3
  108. deallocokay: nop
  109.         ret
  110. unloadovl endp
  111. ;.........
  112. end_prog:
  113. comseg  ends
  114.  end     main1
  115.  
  116.