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, demos 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. From MS-DOS 2.0,
DOS tells the two apart by content, not by name: a file that starts with
MZ is loaded as an EXE whatever its extension. (MS-DOS 1.25 went by the
name: see COM or EXE.) DR DOS 6.0’s
COMMAND.COM is such a file, and Windows
still has a few command-line tools, such as more.com and
tree.com, that keep the old name but are
PE32 or PE32+
programs inside.
The file has no structure to describe, so the reference is about what is around it: how DOS loads and starts the program, the 256-byte Program Segment Prefix it builds in front of it, the DOS and BIOS services a program calls, and the things often found inside. The code itself is described in the x86 instruction reference.
A COM file dropped on any page opens in the explorer. It shows the file as DOS would
run it, from 0100h, with an instruction decoded at every byte and laid out
in columns, so that a jump into the middle of another instruction can be followed. It
also finds the entry jump and what it skips, which bytes the code reaches, a whole
.exe carried inside, the trailer of MS-DOS 7’s tools, a UPX-packed
program, which it unpacks, the interrupt calls with the DOS or BIOS service and
subfunction each asks for and the meaning of its parameters, and the text the program
prints. It also says what MS-DOS 1.25, 2.0 and 4.0 would each do with the file, from
their published source.
Structure
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 65,280 bytes (0xFF00) of a file fit in it
after the PSP, less the room the program needs for its data and stack. The largest
file DOS will load differs by version: 65,278 bytes in MS-DOS 1.25, 65,534 in 2.0 (the
last 254 running past the segment) and 65,279 in 4.0. The source of MS-DOS 3.x, 5 and
6 is not published, so their limits are not verified (see
the size limit).
0x0000PSP, built by DOS: the exit call, memory size, saved vectors, file handles, environment, FCBs and the command line256 bytes0x0100the file: code and data, starting with the first instructionup to 65,280 bytes in the segment…free memory and the stack, which grows down from0xFFFE(0xFFFDin MS-DOS 1.25)rest of the segment
An example
These 23 bytes are a complete program that prints a line and ends. The string’s
address, 0x109, is its offset in the file plus 0x100.
0100 B4 09 mov ah, 0x9 ; INT 21h service 09h: print a string
0102 BA 09 01 mov dx, 0x109 ; DS:DX = the string
0105 CD 21 int 0x21
0107 CD 20 int 0x20 ; end the program
0109 48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21 24 "Hello, world!$"
Reference pages
- Loading and running – how each version of DOS tells COM from EXE, the memory and registers a program starts with, the size limits, and the ways to end
- The Program Segment Prefix – every field of the 256 bytes in front of the program, the CP/M call, the command line and the FCBs
- DOS and BIOS services – the INT 21h functions of MS-DOS and Windows 95 with their subfunctions, and the BIOS, multiplex, mouse and expanded memory services, as the explorer names them
- What the file holds – entry jumps, code and data, an embedded .exe, MS-DOS 7’s trailer, UPX and PKLITE, files over 64 KB, and what the explorer shows
Telling it apart
A COM file has no signature, so only its name says what it is. The explorer takes a
file as a DOS COM program when its name ends in .com and:
- it does not start with
MZorZM, which make DOS 2.0 and later load it as an .exe, and the explorer too (a file under 28 bytes is the exception, shown as a COM program: MS-DOS 2.0 loads as a COM program any file shorter than the 30-byte header it reads, and MS-DOS 4.0 any file shorter than 26 bytes); - it is not plain text whose first line starts with
$or!: that is an OpenVMS DCL command procedure, also named.com, and the explorer shows it as text.
A file that starts with C9h is shown, but with a warning: CP/M 3 marks
its programs with a 256-byte header this way, and C9h is no way for a DOS
program to start. CP/M programs are 8080 or Z80 code, which the explorer does not
disassemble.
Not described
Unknown
- Reference and viewer: how MS-DOS 3.x, 5 and 6 load a COM file. Microsoft has published the source of 1.25, 2.0 and 4.0 only, so the loading rules for other versions would have to be worked out from their binaries. The viewer’s “Loading by DOS version” table covers 1.25, 2.0, 4.0 and DR DOS 6.
- Reference and viewer: PKLITE’s packed format, which PKWARE never published. Third-party unpackers exist, so it could be worked out from them; packed programs are recognised by their mark but not unpacked.
Links
Software
- DOSBox – DOS emulator that runs COM 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
- NASM – assembler that writes COM files with
-f binandorg 100h(Windows, macOS, Linux) - UPX – executable packer that compresses COM programs among many others (Windows, macOS, Linux)
- Ghidra – reverse engineering suite, loading the file as raw 16-bit code at offset
0x100(Windows, macOS, Linux) - IDA Pro – commercial disassembler and decompiler (Windows, macOS, Linux)
Reference
- COM file – Wikipedia’s article: history, size limit, CP/M and Windows
- Program Segment Prefix – Wikipedia’s table of the PSP’s fields
- Ralf Brown’s Interrupt List – the standard list of DOS and BIOS interrupt services