On Wed, 8 Aug 2001 21:48:49 +0000 (UTC), "Nemo."
Quote:
>I am very curious tu know how is made the header of a 16 bit dos program
>(.exe). I found many tutorial on it over internet, but no one describe
>wxactely what each section mean. What is relacotion table, what is his
>utility, where start the data segment, etc.
I wrote this some time ago. I believe it remains accurate, though I'd
more than welcome any and all criticism of it:
The .EXE file was introduced in DOS version 2 to acknowledge the fact
that programs were becoming larger. A special header is included at
the beginning of the file to provide some additional information that
DOS can use, when loading the program into memory. Providing this
extra information, instead of relying on a fixed set of assumptions,
allows programs to have program code much larger than 64k-byte, while
still allowing DOS to load the program into different parts of the
computer's RAM. This is partly because some of the Intel instructions
require a segment value and the segment isn't known either at compile
or at link time. It is only known when DOS loads the program, at run
time.
The first 28 bytes of every EXE-formated program is the header. The
size of the header is stored in the header as the number of 16-byte
paragraphs, so the logical size must at least be 32 bytes. I believe
that the extra 4 bytes (at minimum) reserved to the header can be used
as part of the relocation table, if desired. It can also be used for
other things.
Each entry in the header is a 2-byte, 16-bit word value.
EXE Header Signature -- exeSignature
This value is set to the two initials of an MS-DOS developer, 'MZ'.
This word value is 0x5A4D, since this is a little-endian machine.
This is just a "magic" value that is placed at the beginning of every
.EXE file. If the file isn't identified with these two bytes, then it
probably isn't an .EXE file and DOS will not load it as such
(hopefully.)
Last Page Byte Count -- exeExtraBytes
Each disk block or "page" of the EXE file has a fixed size, considered
to be 512 bytes for legacy reasons. EXE programs do not, however,
exactly begin and end precisely on such boundaries. They might be 100
bytes or 10,000 bytes. But rarely do they work out to occupy an exact
number of 512-byte disk "pages." This value specifies how many bytes
in the last page are valid, if the value is other than zero. If zero,
then the entire last page is considered valid.
Page Count of EXE -- exePages
This specifies how many blocks or pages are used by the entire EXE
program. This value includes the size of the header, itself. This
should be equal to: FLOOR( (exeFileSize+511) / 512 ).
Pointer Count in Relocation Table -- exeRelocItems
This is number of entries in the relocation table, provided elsewhere
in the EXE file.
Header Size -- exeHeaderSize
This value is the size, in paragraphs (16-byte "chunks"), of the EXE
header. Though the fixed size part of the header is 28 bytes, this
value allows the EXE file to include other information after the
28-byte header, but before the beginning of the program, itself. For
example, the relocation entries may be located directly after the
28-byte header.
Minimum Memory Allocation -- exeMinAlloc
This is the minimum number of memory paragraphs, beyond the amount
required to actually load the program. Often, this value is 0. DOS
will not load the program if there isn't enough memory available for
both the actual program size plus this additional amount beyond that
actual value.
Maximum Memory Allocation -- exeMaxAlloc
This is the maximum number of memory paragraphs to allocate for the
program. DOS will allocate this much, if available, falling back to
the minimum allocation, if less is available. This value helps
accommodate stack and heap memory space desired by the program.
Initial SS Value -- exeInitSS
This is the initial value of the SS segment register. The DOS loader
will adjust this value by the base segment value of the memory
allocated to run the program.
Initial SP Value -- exeInitSP
This is the initial value of the SP stack pointer. This value isn't
changed by the DOS loader.
Checksum -- exeChecksum
This may have originally been intended to provide DOS with a further
check on the validity of a program, before trying to run it. It
doesn't appear to be implemented, though. I think any value may be
placed here, including zero.
Initial IP Value -- exeInitIP
This is the initial value of the IP register. Basically, this sets
the starting point for an EXE program. This value isn't changed by the
DOS loader.
Initial CS Value -- exeInitCS
This is the initial value of the CS segment register. The DOS loader
will adjust this value by the base segment value of the memory
allocated to run the program.
Relocation Table Offset -- exeRelocTable
This is the byte position, within the EXE file, of the relocation
table. Set this to the address just at the end of the 28-byte header,
usually, even if the relocation table is empty.
Overlay Number -- exeOverlay
This is usually 0, for resident programs. This value isn't always
included in descriptions of EXE header structures and isn't used by
the DOS program loader, I believe.
Jon