Introduction
A .com file is the simplest kind of program for MS-DOS: an exact image
of 16-bit x86 code and data, with no header at all. The format comes from CP/M, the
earlier system that DOS was modelled on, where programs were also loaded at address
0x100. Small utilities, early games and many DOS system tools were COM
programs.
Larger programs use the MZ .exe
format, which has a header and can be bigger than one 64 KB segment. DOS tells the two
apart by content, not by name: a file that starts with MZ is loaded as an
EXE whatever its extension. Windows still has a few command-line tools, such as
more.com and tree.com, that keep the old name but are
PE programs inside.
Structure
There is no structure to read: the whole file is the program. DOS builds a 256-byte
Program Segment Prefix (PSP), holding the command line and other process details, at
the start of a free segment, copies the file directly after it at offset
0x100, sets all the segment registers to that segment, and jumps to
0x100. Code, data and stack all share the one 64 KB segment, so a COM
program can be at most 65,280 bytes, less the room it needs for its stack.
0x0000PSP, built by DOS: command line, file handles, return address256 bytes0x0100the file, code and data, starting with the first instructionup to 65,280 bytes…stack, growing down from the top of the segmentrest of segment