A COM file has no header and no table of contents, but it is rarely just a run of code. This page lists what is commonly found inside one, how each is recognised, and how the explorer shows it. Everything here is found from the bytes and the code, not read from fields, so it is the explorer’s reading of the file rather than a structure the format defines.
The program
Byte 0 of the file is at offset 0100h in memory, and it is the first
instruction. Every address in the program is an offset in its one segment, so a file
offset becomes an address by adding 0x100. Code and data are mixed
freely: there is nothing to say which bytes are which.
The explorer’s Code section disassembles the whole of the file that fits in the
segment, from 0100h, as 16-bit code. As in every Code section, an
instruction is decoded at every byte and laid out in columns: the first column is the
plain disassembly, each instruction straight after the one before, and the others hold
the instructions that start inside them, so a jump into the middle of an instruction
can be followed. The instructions are described in the
x86 instruction reference.
Code and data
Nothing in the file says which bytes are code. The explorer finds out what it can by
following the program from 0100h: through each jump, call and conditional
branch, stopping at a RET, an unconditional jump, HLT,
INT 20h, INT 27h or an INT 21h service that ends
the program. In the tree, the blocks it reaches are Reached code and
the rest Data or unreached code.
This cannot be complete. A program can reach code through a table of addresses, a value in a register, or by modifying itself, none of which can be followed from the bytes alone, and a packer’s unpacker runs over data that the search reaches nowhere. So unreached bytes are usually data, but not always; the Code section still disassembles them.
The entry jump
Since execution starts at the first byte, a program that wants data at the front
starts with a jump over it: E9 with a 16-bit displacement, or
EB with an 8-bit one. Assemblers produce this when the data is declared
before the code; resident programs do it on purpose, so that the part kept in memory
comes first and the set-up code at the end can be dropped (see
Ending the program).
When the first instruction is such a jump, forwards and inside the file, the explorer shows it as its own block and starts the rest of the program at its target.
What the jump skips
The bytes between the entry jump and its target are shown as one block. They may be data, such as a banner, tables, buffers or the variables of a resident part, or code that is called later; only the program says which. The Code section still disassembles them, so code among them can be read there.
An embedded .exe
Some COM files carry a complete MZ program
inside, with a small loader of their own. The file starts with a jump over the MZ
image to code after it, which sets up the embedded program the way DOS would load an
EXE and starts it. The explorer looks for an MZ header (or one starting
ZM, which DOS 2 and later accept too) within the first 64 bytes after
the entry jump whose image, as its header gives it, ends before the jump target. It
shows the bytes before it, the embedded program, and the bytes between its end and the
jump target as separate blocks.
The embedded program’s block shows its header fields, relocation table and load module, and has an Open button that opens it as a program of its own, with the MZ viewer and its own disassembly at its own addresses. The COM file’s Code section leaves the image out, since reading it as code at the COM file’s addresses would be wrong.
MS-DOS 7’s FORMAT.COM (Windows 95) is built this way: see the
worked example.
The ENUNS trailer
The COM tools of MS-DOS 7 (Windows 95), such as FORMAT.COM,
SYS.COM and KEYB.COM, end with seven extra bytes: the text
ENUNS and a 16-bit value.
| Offset from the end | Size | Contents |
|---|---|---|
| −7 | 5 | The text ENUNS |
| −2 | 2 | A value, little-endian |
The value differs from file to file: 0x1877 in FORMAT.COM,
0x0677 in SYS.COM and 0x0657 in
KEYB.COM, from one Windows 95 startup disk. It is not the file’s size.
Microsoft does not document the trailer, and we have not found what the value holds.
ENU is Microsoft’s code for US English, which suggests the trailer marks
the language of the build; that is a guess, not something we could confirm.
The explorer shows the trailer as its own block when a file ends with it, and stops the program before it.
Packed programs
Executable packers compress a COM program and add a small unpacker that runs first, expands the program in memory and jumps to it. The disassembly of a packed program shows the unpacker; the program itself is only in memory once it has run. The explorer knows two packers.
UPX
UPX is open source, and its source describes the layout of a packed COM file, which is four parts one after the other:
- Entry code. It checks with
cmp sp, …that the segment has room for the unpacked program, and ends at once withINT 20hif not. Then it copies the rest of the file to the top of the segment, backwards, and jumps to the decompressor there. - The pack header, at the end of the entry code.
- The compressed program, straight after the header.
- The decompressor, which expands the program back to
0100h, undoes the call filter, and returns to0100h.
The pack header of a COM program is 22 bytes from header version 10, and 20 in
versions 4 to 9, without the last two fields. Versions 3 and earlier use a 24-byte
header (UPX’s packhead.cpp). UPX looks for it in the first 128 bytes.
Numbers are little-endian.
| Offset | Size | Contents |
|---|---|---|
0 | 4 | The text UPX! |
4 | 1 | The version of the header layout |
5 | 1 | The format: 1 DOS COM, 2 DOS SYS, 3 DOS EXE (UPX numbers its many other formats from 4 on) |
6 | 1 | The method. 2, 3 and 4 are NRV2B with a 32-bit, 8-bit or 16-bit bit buffer; 5 to 7 are NRV2D and 8 to 10 NRV2E in the same three forms; 14 is LZMA. A COM program is always 4. |
7 | 1 | The compression level, 1 to 10, in the low four bits. Before version 10, bit 7 set means call filter 06h was used. |
8 | 4 | The Adler-32 checksum of the decompressed bytes, before the filter is undone |
12 | 4 | The Adler-32 checksum of the compressed bytes |
16 | 2 | The length of the program unpacked, which is the original file |
18 | 2 | The length of the compressed program |
20 | 1 | The filter (from version 10) |
21 | 1 | The header checksum (from version 10): the sum of bytes 4 to 20, modulo 251 |
NRV2B is a Lempel-Ziv code. Its control bits come in 16-bit
little-endian words for a COM program, read from the most significant bit, and the
literal bytes and offset bytes are mixed in among those words in the one stream, in
the order they are read. A 1 bit means the next byte is a literal, copied to the
output. A 0 bit starts a match. Its offset begins as a gamma number: start
at 1, then double it and add the next bit, again and again, until the bit after it is
1. A gamma number of 2 reuses the last offset; otherwise the offset is (number − 3) ×
256, plus the next byte, plus 1, and the value (number − 3) × 256 + byte =
FFFFFFFFh ends the stream. The match length is then two bits, or, when
they are both 0, a gamma number plus 2; one more is added when the offset is over
D00h. The match copies length + 1 bytes from that far back in the
output.
The call filter makes calls and jumps compress better. Before
compressing, UPX replaced the 16-bit displacement after each E8h
(CALL) or E9h (JMP) byte with the position of
the displacement + 100h + the displacement, then skipped the two bytes.
Filters 01h to 03h store that value little-endian and
04h to 06h big-endian; 01h and
04h do only CALLs, 02h and 05h
only JMPs, and 03h and 06h both. The
decompressor scans the same way and subtracts it again.
The explorer reads the header, checks its checksum, decompresses the program, checks both Adler-32 checksums and undoes the filter. The compressed block then has an Open button that shows the unpacked program as a COM file of its own.
PKLITE
PKLITE, from PKWARE, packs COM and EXE programs. PKWARE never published its format,
and no description of it from PKWARE exists, so the explorer does not unpack it. It
names a PKLITE file by its copyright notice, the text PKLITE Copr. in the
first 512 bytes, which the packer puts after its unpacker (at 30h in the
files we have seen). The word PKLITE alone is not taken as a mark, since a
program may simply print it.
Bytes past 64 KB
Only the first 65,280 bytes (0xFF00) of a file fit in the program’s
segment after the PSP. When a file is longer, the explorer ends the program and the
Code section at 0xFF00 and shows the rest as a block of its own. Whether
DOS loads such a file at all depends on the version (see
the size limit): MS-DOS 2.0 loads up to
65,534 bytes, the rest running on past the segment, while MS-DOS 1.25 and 4.0 refuse
it. The explorer’s Loading by DOS version table gives each version’s result for the
file, and flags a size that a version refuses, such as 65,280 bytes exactly, which
fits the segment but which MS-DOS 4.0 will not load.
Interrupt calls
A COM program talks to DOS and the BIOS through software interrupts, choosing the
service with a number in AH. The explorer’s Interrupt calls table lists
every INT in the code the entry point reaches, and every one a plain
disassembly finds in the bytes it does not reach, with its address and the
interrupt’s name.
For each call it works out the registers from the instructions before it and names
the service and subfunction: AH and AL for DOS and most of
the BIOS, BL for INT 10h AH = 12h, and the whole
of AX for the mouse. Where the arguments are known it adds their meaning:
the string or file name at DS:DX, the file access mode, the handle, the
seek origin, the drive, the return code. The full list is on
DOS and BIOS services, with the rules for
working out the registers.
The table is a reading of the code, not of the program’s behaviour. A call in bytes the entry point does not reach is marked as such and may be data; a service chosen by a value set somewhere else is shown as not known; and a service that a program reaches with a register holding something the code above it does not show will be misnamed.
Text
The Text table lists runs of at least six printable characters with at least four
letters among them, with their addresses. A run that ends with $ is marked:
that is the end marker of INT 21h service 09h, which prints a string, so
such runs are usually the program’s messages. A run followed by a zero byte is marked
too, the way C programs end their strings.
Worked example
FORMAT.COM from a Windows 95 startup disk, MS-DOS 7, is 49,543 bytes.
Its first 32 bytes:
00000000 E9 8D A8 43 6F 6E 76 65 72 74 65 64 00 00 00 00 ...Converted....
00000010 4D 5A 78 00 55 00 49 00 20 00 01 00 FF FF 13 05 MZx.U.I. .......
0x10The jump goes to 0xA990. The MZ header at file offset 0x10
gives an image of 85 pages with 0x78 bytes in the last, 43,128 bytes, with
73 relocations, so it ends at 0xA888, eight bytes before the jump target.
The explorer shows:
| File offset | Size | Block |
|---|---|---|
0x0000 | 3 | Entry jump: jmp 0xa990 |
0x0003 | 13 | Before the embedded program: “Converted” and four zero bytes |
0x0010 | 43,128 | Embedded MZ program: CS:IP 0532:1A00, 73 relocations |
0xA888 | 8 | After the embedded program: zero bytes |
0xA890 | 6,384 | Program from the jump target |
0xC180 | 7 | ENUNS trailer, value 0x1877 |
The code at the jump target is the loader. It finds its own address with a
call to the next instruction, works out the segment after the PSP, and
reads the embedded header’s fields through their addresses in memory:
[0x11e] is file offset 0x1E, the embedded program’s
e_ss.
a990 E8 00 00 call 0xa993
a993 5B pop bx
a994 50 push ax
a995 8C C0 mov ax, es
a997 05 10 00 add ax, 0x10 ; the segment after the PSP
a99a 8B 0E 1E 01 mov cx, word ptr [0x11e] ; e_ss
a99e 03 C8 add cx, ax
a9a0 89 4F FB mov word ptr [bx - 0x5], cx
a9a3 8B 0E 26 01 mov cx, word ptr [0x126] ; e_cs
a9a7 03 C8 add cx, ax
a9a9 89 4F F7 mov word ptr [bx - 0x9], cx
a9ac 8B 0E 20 01 mov cx, word ptr [0x120] ; e_sp
…
a9ba 8B 3E 28 01 mov di, word ptr [0x128] ; e_lfarlc
a9be 8B 16 18 01 mov dx, word ptr [0x118] ; e_cparhdr
…
a9c6 8B 0E 16 01 mov cx, word ptr [0x116] ; e_crlc
a9ca E3 1A jcxz 0xa9e6
So the COM file is a wrapper: the program that formats disks is the embedded EXE, and the wrapper relocates and starts it itself, as DOS would.