<-- Resources Index / The EXE File Format - Excerpt from the MS-DOS Programmer's Reference (combined sections 5.7.2 and portion of 5.8) 
1
The EXE File Format - Excerpt from the MS-DOS Programmer's Reference (combined sections 5.7.2 and portion of 5.8)

5.7.2  The .EXE File Format

An .EXE file contains a file header and a relocatable-program image. The file header contains information that
MS-DOS uses when loading the program, such as the size of the program and the initial values of the registers.
The file header also points to a relocation table containing a list of pointers to relocatable-segment addresses
in the program image.  The form of the file header corresponds to that of the EXEHEADER structure:

EXEHEADER STRUC
    exSignature   dw 5A4Dh  ;Specifies the .EXE file signature. This field must be set to 5A4Dh
                            ;(the ASCII values for the letters M and Z).

    exExtraBytes  dw ?      ;Specifies the number of bytes in the last (partial) page in the file, as
                            ;represented by the remainder, if any, when the total number of bytes in the file
                            ;is divided by 512 (bytes per page).

    exPages       dw ?      ;Specifies the number of whole and partial pages in the file. Dividing this total
                            ;number of bytes in the file by 512 (bytes per page) gives the number of whole
                            ;pages. If the division leaves a remainder, the number of pages is increased by one
                            ;and the remainder is stored in the exExtraBytes field. For example, in a file 513
                            ;bytes long, the exPages field is 2 and the exExtraBytes field is 1.

    exRelocItems  dw ?      ;number of pointers in relocation table

    exHeaderSize  dw ?      ;Specifies the size of the file header, in paragraphs. Since each paragraph has 16
                            ;bytes, the file header size is always a multiple of 16.

    exMinAlloc    dw ?      ;Specifies the minimum amount of extra memory, in paragraphs, required by the
                            ;program. The extra memory is in addition to the memory required to load the program
                            ;image.  If the values of both exMinAlloc and exMaxAlloc are zero, the program is
                            ;loaded as high as possible in memory.

    exMaxAlloc    dw ?      ;Specifies the maximum amount of extra memory, in paragraphs, requested by the
                            ;program. If the values of both exMinAlloc and exMaxAlloc are zero, the program is
                            ;loaded as high as possible in memory.

    exInitSS      dw ?      ;Specifies the initial value of the SS register. The value is a relocatable-segment
                            ;address. MS-DOS adjusts (relocates) this value when loading the program.

    exInitSP      dw ?      ;Specifies the initial value of the SP register.

    exCheckSum    dw ?      ;Specifies the checksum of the file. This value is equal to the one's complement
                            ;(inverse) of the sum of all 16-bit values in the file, excluding this field.

    exInitIP      dw ?      ;Specifies the initial value of the IP register.

    exInitCS      dw ?      ;Specifies the initial value of the CS register. This value is a relocatable-segment
                            ;address. MS-DOS adjusts (relocates) the value when loading the program.

    exRelocTable  dw ?      ;Specifies the offset, in bytes, from the beginning of the file to the relocation
                            ;table.

    exOverlay     dw ?      ;overlay number; Specifies a value used for overlay management. If this value is
                            ;zero, the .EXE file contains the main program. The exOverlay field can be followed
                            ;by additional information used by the system for overlay management. The content and
                            ;structure of this information depends on the method of overlay management used by
                            ;the main program.
EXEHEADER ENDS

The EXEHEADER structure contains values that MS-DOS uses when loading a relocatable program - values such as the
size of the program and the initial values of the registers.

This structure appears at the beginning of the file header for an .EXE file. The complete .EXE file header
consists of this structure and a relocation table. The size of the file header, in paragraphs, is specified by
the exHeaderSize field.

The program image, which contains the processor code and initialized data for a program, starts immediately
after the file header. Its size, in bytes, is equal to the size of the .EXE file minus the size of the file
header, which is equal to the value in the exHeaderSize field multiplied by 16. MS-DOS loads the .EXE program by
copying this image directly from the file into memory and then adjusts the relocatable-segment addresses
specified in the relocation table. 

The relocation table is an array of relocation pointers, each of which points to a relocatable-segment address
in the program image. The exRelocItems field in the file header specifies the number of pointers in the array,
and the exRelocTable field specifies the file offset at which the relocation table starts. Each relocation
pointer consists of two 16-bit values: an offset and a segment number. 

To load an .EXE program, MS-DOS first reads the file header to determine the .EXE signature and calculate the
size of the program image. It then attempts to allocate memory. First, it adds the size of the program image to
the size of the PSP and to the amount of memory specified in the exMinAlloc field of the EXEHEADER structure. If
the total exceeds the size of the largest available memory block, MS-DOS stops loading the program and returns
an error value. Otherwise, it adds the size of the program image to the size of the PSP and to the amount of
memory specified in the exMaxAlloc field of the EXEHEADER structure. If this second total is less than the size
of the largest available memory block, MS-DOS allocates the amount of memory indicated by the calculated total.
Otherwise, it allocates the largest possible block of memory. 

After allocating memory, MS-DOS determines the segment address, called the start-segment address, at which to
load the program image. If the value in both the exMinAlloc and exMaxAlloc fields is zero, MS-DOS loads the
image as high as possible in memory. Otherwise, it loads the image immediately above the area reserved for the
PSP. 

Next, MS-DOS reads the items in the relocation table and adjusts all segment addresses specified by the
relocation pointers. For each pointer in the relocation table, MS-DOS finds the corresponding
relocatable-segment address in the program image and adds the start-segment address to it. Once adjusted, the
segment addresses point to the segments in memory where the program's code and data are loaded. 

Then MS-DOS builds the 256-byte PSP in the lowest part of the allocated memory, setting the AL and AH registers
just as it does when loading .COM programs. MS-DOS uses the values in the file header to set the SP and SS
registers and adjusts the initial value of the SS register by adding the start-segment address to it. MS-DOS
also sets the ES and DS registers to the segment address of the PSP. 

Finally, MS-DOS reads the inital CS and IP values from the program's file header, adjusts the CS register value
by adding the start-segment address to it, and transfers control to the program at the adjusted address. 

See Also:
    Interrupt 21h Function 4B00h Load and Execute Program
    Interrupt 21h Function 4B01h Load Program
    Interrupt 21h Function 4B03h Load Overlay
 1:1