Loading overlays with the EXEC function is much less complex than using EXEC to run another program. The overlay can be constructed as either a memory image (.COM) or relocatable (.EXE) file and need not be the same type as the program that loads it. The main program, called the root segment, must carry out the following steps to load and execute an overlay:
1.Make a memory block available to receive the overlay. The program that calls EXEC must own the memory block for the overlay.
2.Set up the overlay parameter block to be passed to the EXEC function. This block contains the segment address of the block that will receive the overlay, plus a segment relocation value to be applied to the contents of the overlay file (if it is a .EXE file). These are normally the same value.
3.Call the MS-DOS EXEC function to load the overlay by issuing an Int 21H with the registers set up as follows:
AH = 4BH AL = 03H (EXEC subfunction to load overlay) DS:DX = segment:offset of overlay file pathname ES:BX = segment:offset of overlay parameter block
Upon return from the EXEC function, the carry flag is clear if the overlay was found and loaded. The carry flag is set if the file could not be found or if some other error occurred.
1.Execute the code within the overlay by transferring to it with a far call. The overlay should be designed so that either the entry point or a pointer to the entry point is at the beginning of the module after it is loaded. This technique allows you to maintain the root and overlay modules separately, because the root module does not contain any "magical" knowledge of addresses within the overlay segment.
To prevent users from inadvertently running an overlay directly from the command line, you should assign overlay files an extension other than .COM or .EXE. It is most convenient to relate overlays to their root segment by assigning them the same filename but a different extension, such as .OVL or .OV1, .OV2, and so on.
Figure 12-6 shows the use of EXEC to load and execute an overlay.
.
.
.
; allocate memory for overlay
mov bx,1000h ; get 64 KB (4096 paragraphs)
mov ah,48h ; function 48h = allocate block
int 21h ; transfer to MS-DOS
jc error ; jump if allocation failed
mov pars,ax ; set load address for overlay
mov pars+2,ax ; set relocation segment for overlay
; set segment of entry point
mov word ptr entry+2,ax
mov stkseg,ss ; save root's stack pointer
mov stkptr,sp
mov ax,ds ; set ES = DS
mov es,ax
mov dx,offset oname ; DS:DX = overlay pathname
mov bx,offset pars ; ES:BX = parameter block
mov ax,4b03h ; function 4bh, subfunction 03h
int 21h ; transfer to MS-DOS
mov ax,_DATA ; make our data segment
mov ds,ax ; addressable again
mov es,ax
cli ; (for bug in some early 8088s)
mov ss,stkseg ; restore stack pointer
mov sp,stkptr
sti ; (for bug in some early 8088s)
jc error ; jump if EXEC failed
; otherwise EXEC succeeded...
push ds ; save our data segment
call dword ptr entry ; now call the overlay
pop ds ; restore our data segment
.
.
.
oname db 'OVERLAY.OVL',0 ; pathname of overlay file
pars dw 0 ; load address (segment) for file
dw 0 ; relocation (segment) for file
entry dd 0 ; entry point for overlay
stkseg dw 0 ; save SS register
stkptr dw 0 ; save SP register
Figure 12-6. A code skeleton for loading and executing an overlay with the EXEC function. The overlay file may be in either .COM or .EXE format.