Introduction
The MZ format is the program format of MS-DOS, used from DOS 2.0 in 1983. It is named
after its first two bytes, MZ, the initials of Mark Zbikowski, one of its
designers. An MZ program is 16-bit x86 code for the processor’s real mode. DOS can load
it anywhere in memory: a table in the file lists every place that holds a segment
address, and DOS adds the segment it loaded the program at to each one.
Every later Windows and OS/2 program still begins with an MZ header and a small DOS
program, the stub, so several formats share the .exe extension and the
same first two bytes. This page is the plain DOS one; all of them are on the
.EXE page. The smaller DOS programs with no header at
all are .com files. Many DOS programs
carry more after the part DOS loads: overlays, debug information, a 32-bit program for
a DOS extender, or the archive of a self-extractor.
The format is small and fully known, and many references describe it. The code itself is described in the x86 instruction reference.
A program dropped on any page opens in the explorer, which shows its structure, every header field and relocation, what any overlay holds, and its code: an instruction decoded at every byte, laid out in columns, so that a jump into the middle of another instruction can be followed. An MZ program has no resources.
Structure
The file starts with a header of 16-bit little-endian words. It gives the size of the image in 512-byte pages, the number of relocations, the header’s own size in 16-byte paragraphs, the memory the program needs, and the starting values of the stack and instruction pointers. The relocation table usually follows inside the header, and the load module, the program’s code and data, starts where the header ends.
0x00MZ headerMZ, the image size, the relocation count, the header size, memory, SS:SP, the checksum, CS:IP, the relocation table offset and the overlay number28 bytes0x1CExtended fields or a linker’s mark: anything up to the relocation tablevariese_lfarlcRelocation table offset and segment pairs4 bytes each…Padding to the end of the header, a whole number of paragraphsvariese_cparhdr × 16Load module the program’s code and data, which DOS copies into memoryto the end of the imageimage endOverlay anything after the image, which DOS does not loadoptional
How DOS loads it
- It reads the header and works out the size of the image from
e_cpande_cblp, and the size of the load module by taking away the header. - It finds memory for a 256-byte program segment prefix (PSP), the load module and
at least
e_minallocmore paragraphs, and takes up toe_maxallocparagraphs if they are free. - It copies the load module in straight after the PSP. Its first paragraph is the
load segment: the PSP’s segment plus
0x10. - For each relocation, it adds the load segment to the 16-bit word the entry points at.
- It sets SS to
e_ssand CS toe_cs, each plus the load segment, SP toe_spand IP toe_ip, points DS and ES at the PSP, and jumps to CS:IP.
The code
The load module has no table of segments: code, data and stack are one image, and the program finds its own parts through the segment addresses the relocations fix. The explorer splits the image at the segment starts that the header and the relocations show, and disassembles the segment CS points at as 16-bit code, with addresses counted from CS so that they match IP. See The load module and the x86 instruction reference.
Reference pages
- The MZ header – every field, the image size, the extended fields of Windows programs, e_lfanew and linkers’ marks
- The relocation table – its entries and how DOS applies them
- The load module – the program image, and how the explorer finds its segments
- Overlays and appended data – overlays, debug information, DOS extender programs and archives after the image
Telling it apart
MS-DOS, Windows and OS/2 programs all start with MZ. The newer formats put
the file offset of a second header in the 32-bit word at 0x3C,
e_lfanew. The explorer tries the newer formats first and takes a file as a
plain MS-DOS program when:
- it starts with
MZorZM; e_cblpis 512 or less,e_cpis not 0, ande_cparhdris at least 2, so that the header holds its 28 bytes of fields;- the header is no larger than the image;
- the relocation table is inside the file and does not start inside the first 28 bytes;
- the word at
0x3Cdoes not lead to a valid newer header:PE\0\0with an optional header magic of0x10Bor0x20B(a PE32 or PE32+ program), or an NE, LE or LX header whose tables fit in the file.
A Windows file whose new header is missing or damaged is therefore shown as an MS-DOS
program, with a note saying where e_lfanew points. A relocation table
offset of 0x40 or more is not enough on its own to mark a Windows program:
programs packed with PKLITE start their relocations at 0x52 too.
Not described
The gaps are those on the EXE overview. For the MZ format, the bytes that Borland TLINK writes in the header are not documented, and the DOS stub’s code is shown as bytes rather than disassembled.
Variants
- MS-DOS executable (MZ) – 16-bit DOS programs: an MZ header, relocations and the code
- 16-bit Windows executable (NE) – Windows 3.x and early OS/2 programs and DLLs, in movable segments
- Linear Executable (LE) – 32-bit Windows VxD drivers and DOS extender programs, in paged objects
- OS/2 executable (LX) – 32-bit OS/2 programs and DLLs, in paged objects
- Windows executable (PE32) – 32-bit Windows programs and DLLs: an MZ stub, a PE header with a 32-bit optional header, and sections
- Windows 64-bit executable (PE32+) – 64-bit Windows programs and DLLs (x64, ARM64, Itanium): the PE format with a 64-bit optional header
Links
Software
- DOSBox – DOS emulator that runs MZ programs (Windows, macOS, Linux)
- DOSBox-X – DOSBox fork aimed at accuracy and wider DOS and Windows 3.x support (Windows, macOS, Linux)
- FreeDOS – an open DOS that runs them directly
- DJGPP – GNU compilers for DOS, whose programs are a small MZ stub followed by a COFF image (DOS)
- Open Watcom – compiler suite whose linker writes MZ programs and whose
wdumptool lists them (Windows, Linux, DOS, OS/2) - Ghidra – the NSA’s free reverse engineering suite (Windows, macOS, Linux)
- IDA Pro – commercial disassembler and decompiler (Windows, macOS, Linux)
Reference
- EXE Format – delorie.com’s description of the MZ header, the image size and the checksum