A COM file carries nothing but the program, so everything about how it runs is decided by DOS: where it goes in memory, what the registers hold, and how much room it has. This page goes through it in the order DOS does.
The details differ from version to version. Microsoft has published the source of
MS-DOS 1.25, 2.0 and 4.0, and what this page says about those three is read from it:
COMMAND.ASM of 1.25, and EXEC.ASM and MISC.ASM of
2.0 and 4.0. The source of MS-DOS 3.x, 5 and 6 is not published, so their behaviour is
not verified here; where the page gives no version, the rule holds in
all three versions that were read.
COM or EXE
From MS-DOS 2.0, DOS decides by the content. Its EXEC service
(INT 21h service 4Bh) reads the start of the file as an EXE header. If the
first two bytes are MZ, or ZM, the reverse order some early
linkers wrote, it loads the file as an MZ
.exe, with its header and relocations; otherwise it loads it as a COM
program. From 2.0 on, the name plays no part in this choice, so a .com
file can be an EXE inside, and many later ones are: DR DOS 6.0’s
COMMAND.COM, and the .com tools of Windows NT such as
more.com, tree.com, format.com and
chcp.com, which are Windows programs.
The header EXEC reads first is 30 bytes long in MS-DOS 2.0 and 26 bytes in MS-DOS 4.0,
and the signature is only looked at when all of it was read. So a file shorter than
that is loaded as a COM program even if it starts with MZ.
MS-DOS 1.25 decided by the name. Its kernel has no EXEC service:
COMMAND.COM loads programs itself. For a command typed without an
extension it tries to open NAME.COM, then NAME.EXE, then
NAME.BAT, and loads the file as whichever of the three it opened. Its
EXE loader takes the header’s fields as they are, without checking for
MZ. So under DOS 1 a .COM file that starts with
MZ is loaded as a COM program, and an .EXE file without
MZ as an EXE.
From 2.0 the extension matters only for finding the file. When a command is typed
without one and both NAME.COM and NAME.EXE exist, DOS runs the
COM file. On Windows NT the default PATHEXT list still puts
.COM before .EXE.
The explorer follows the rule of DOS 2.0 and later: a .com file of 28 bytes
or more that starts with MZ or ZM opens as an
.exe. A shorter one opens as a COM program, and its
Loading by DOS version table gives each version’s choice.
Version by version
The rules on this page, side by side. The sizes assume the largest free block of memory is 64 KB or more after the PSP, as it normally is when a program is run from the command line.
| MS-DOS 1.25 | MS-DOS 2.0 | MS-DOS 4.0 | |
|---|---|---|---|
| Loaded by | COMMAND.COM | EXEC (INT 21h 4Bh) | EXEC (INT 21h 4Bh) |
| COM or EXE | By the extension | MZ or ZM in a 30-byte header | MZ or ZM in a 26-byte header |
| An empty file | Refused: “Bad command or file name” | Loaded, and run from whatever memory holds at 0100h | Refused: error 11, invalid format |
| Largest COM file | 65,278 bytes | 65,534 bytes | 65,279 bytes |
| A larger one | “Program too big to fit in memory” | Error 8, not enough memory | Error 8, not enough memory |
| SP at the start | FFFDh | FFFEh | FFFEh |
| AL and AH | INT 21h 29h’s result for each argument: 00h, 01h with wildcards, FFh for a bad drive | 00h, or FFh for a bad drive | 00h, or FFh for a bad drive |
| CX:DX | The batch file’s FCB while a batch file runs, otherwise 0 | Not set (DX holds the PSP’s segment) | Not set (DX holds the PSP’s segment) |
PSP 06h | (Memory size − 100h) AND FFF0h | Memory size, at most FFF0h | Memory size − 100h, at most FEF0h |
MS-DOS 3.x, 5 and 6 are not verified, since their source is not published. They are the versions most COM programs ran under, but none of the values above can be confirmed for them from a source, so this page does not give theirs.
Memory
A COM program gets the largest free block of memory, which is normally all of it. (In
MS-DOS 1.25, which has no memory blocks, it is all the memory between the end of DOS
and the part of COMMAND.COM that stays at the top.) DOS builds the
Program Segment Prefix in the first 256 bytes of
the block and copies the file in straight after it, at offset 0x100.
There are no relocations, because every address in the program is an offset within
this one segment, whatever segment it is.
0x0000PSP built by DOS256 bytes0x0100the file, byte for bytefile size…free: the program’s uninitialised data, buffers and heapthe rest0xFFFEstack, a zero word on top, growing downas used
0xFFFD)The word at offset 0x02 of the
PSP gives the segment just past the end of the block. Because the program owns the
whole block, it has to give memory back before it can allocate more or run another
program: INT 21h service 4Ah shrinks the block, usually to the end of the
program and its stack.
The size limit
Code, data and stack all live in one 64 KB segment, and the first 256 bytes of it are
the PSP, so 65,536 − 256 = 65,280 bytes (0xFF00) of a file fit in the
segment. No version read for this page has exactly that limit. Each reads the file
with a byte count of its own and treats a read that fills the count as a program too
big for memory, so the largest file it loads is one byte less than the count:
| Version | Asks to read | Largest COM file | A larger file |
|---|---|---|---|
| MS-DOS 1.25 | FEFFh bytes: its record of the memory size, capped at FFFFh, less 100h | 65,278 bytes | Refused: “Program too big to fit in memory” |
| MS-DOS 2.0 | FFFFh bytes, into the paragraph after the PSP | 65,534 bytes; the bytes from file offset FF00h on go past the end of the segment | Refused: error 8, not enough memory |
| MS-DOS 3.x | Not verified: the source is not published | ||
| MS-DOS 4.0 | FF00h bytes | 65,279 bytes | Refused: error 8, not enough memory |
| MS-DOS 5 and 6 | Not verified: the source is not published | ||
These are for a free block of 64 KB or more after the PSP. With less memory, every version asks for the room there is after the PSP, and the file must be shorter than that. In practice a program must be smaller anyway, to leave room for its stack and any data it does not keep in the file.
Larger files with a .com name and no MZ do exist, among
them a few DOS games. No loader reads the rest of such a file for the program, and no
version loads part of one: MS-DOS 1.25 and 4.0 refuse the whole file. MS-DOS 2.0 loads
a file of up to 65,534 bytes whole, the part from FF00h running on past
the end of the program’s segment, where the program can reach it only with a segment
value of its own (the PSP’s segment plus 1000h). Anything longer it
refuses too. A program that wants more data than that must open and read its own file
with the file services, as any program can. The explorer shows the bytes past
FF00h as a
separate block, and lists what
each version would do with the file.
Registers at the start
| Register | MS-DOS 1.25 | MS-DOS 2.0 | MS-DOS 4.0 |
|---|---|---|---|
CS, DS, ES, SS | The segment of the PSP, which is also the program’s segment | ||
IP | 0100h: the first byte of the file | ||
SP | FFFDh with 64 KB or more: the memory size, which 1.25 caps at FFFFh, less the zero word. With less memory, the memory size − 2. | FFFEh with 64 KB or more after the PSP. With less, the read count − 1: an odd value 257 bytes below the end of the block. | FFFEh with 64 KB or more; with less, the end of the block − 2. |
| Top of the stack | A zero word | ||
AL | What INT 21h service 29h returned when COMMAND.COM parsed the first argument into the FCB: 00h, 01h if the name has wildcards (* or ?), or FFh if its drive letter is not valid | FFh if the drive in the first FCB is not valid, otherwise 00h | |
AH | The same for the second argument | ||
CX:DX | While a batch file runs, the far address of COMMAND.COM’s FCB for the batch file; otherwise 0000h:0000h | Not set for the program; the code leaves the PSP’s segment in DX and the starting AX in BX | |
DOS does not promise anything for the other registers, and a program should not
depend on them; Ralf Brown’s Interrupt List notes that under DESQview AX is
always 0000h at the start. The zero word on the stack is there so that a
plain RET ends the program (see below).
The loader uses the top of the stack itself before the program’s first instruction
runs: for the zero word, the far return address it jumps to the program through, and
in 1.25 a call and an INT 21h that saves nine registers. In a file long enough to
reach there, those bytes do not survive: from file offset FEDFh in
MS-DOS 1.25, and from FEFAh in 2.0 and 4.0.
Ending the program
| Instruction | What happens |
|---|---|
RET | Pops the zero word into IP, so the program jumps to offset 0 of its segment, where the PSP starts with INT 20h. This works only when SP is back where it started. |
INT 20h | Ends the program. CS must be the PSP’s segment, which in a COM program it always is. |
INT 21h, AH = 00h | The same, as a DOS service. |
INT 21h, AH = 4Ch | Ends the program with a return code in AL, which a batch file can test with IF ERRORLEVEL. From DOS 2.0, and the way Microsoft recommends. |
INT 21h, AH = 31h | Ends the program but keeps the first DX paragraphs of it in memory, with a return code in AL: terminate and stay resident (TSR). |
INT 27h | The older TSR call: keeps everything up to offset DX of the segment. |
Resident programs are why many COM files put their data and the code that stays in memory at the front, and the code that sets them up at the end: the set-up code then falls outside the part that is kept. The file starts with a jump to it, which the explorer shows as the entry jump.
DR DOS 6: loading above 64 KB
Ralf Brown’s Interrupt List records one loader that treats some COM files
differently by their first bytes. DR DOS 6 loads a COM program that starts with
9Ch 55h (PUSHF, PUSH BP) above the first 64 KB of
memory, as it does an EXE program with no relocations, “to avoid the EXEPACK bug”. It
makes the room by extending the memory block that holds the program’s environment.
It does not do this when the name of the parent program in its memory control block
is WIN. The explorer notes a file that starts this way.
From CP/M
CP/M, the 8-bit system that DOS followed, also loaded its .com programs
at 0100h, with a 256-byte area in front. The PSP keeps that area’s layout
where it can: it starts with an exit call, it has an entry into the system at offset
5, a default file control block at 5Ch, and the command line
at 80h, all where CP/M had them. That is what made it practical to
translate CP/M programs to DOS, though the two systems’ COM files are not compatible:
CP/M’s hold 8080 or Z80 code.
Under Windows
32-bit versions of Windows run DOS COM programs in NTVDM, an emulated DOS machine.
64-bit Windows has no NTVDM, so it cannot run them without an emulator such as
DOSBox. The .com tools it still has
are Windows programs and run normally.