Lably
Skip to content

COM file

Type of simple executable file

  • 7 min read
  • 6 chapters

Related on Lably

Through Operating system, Text file and DOS

The .COM file name extension has no relation to the .com (for "commercial") top-level Internet domain name. However, this similarity in name has been exploited by malware writers.

DOS binary format

The COM format is the original binary executable format used in CP/M (including SCP and MSX-DOS) as well as DOS. It is very simple; it has no header (with the exception of CP/M 3 files), and contains no standard metadata, only code and data. This simplicity exacts a price: the binary was designed to have a maximum size of 65,280 (FF00h) bytes (256 bytes short of 64 KiB) and store all its code and data in one 8086 memory segment.

Although COM files are written as if they all start at the same fixed entry point (0100h)), MS-DOS chooses which 64K memory segment to load it into and so there is no problem having multiple COM files in memory at the same time. The bytes are placed starting at offset 0100h (256 decimal) because the previous bytes are reserved for system use (i.e., the DOS program segment prefix).

Before MS-DOS and the 8086 processor, in the Intel 8080 CPU architecture, only 65,536 bytes of memory could be addressed (address range 0000h to FFFFh). Under CP/⁠M, the first 256 bytes of this memory, from 0000h to 00FFh were reserved for system use by the zero page, and any user program had to be loaded at exactly 0100h to be executed. MS-DOS's COM files were designed to mimic that configuration to ease porting programs from CP/⁠M.

Although the file format is the same in DOS and CP/M, COM files for the two operating systems are not compatible; DOS COM files contain x86 instructions and possibly DOS system calls, while CP/M COM files contain 8080 instructions and CP/M system calls (programs restricted to certain machines could also contain additional instructions for 8085 or Z80).

MS-DOS was designed to run on the Intel 8086 processor which could address sixteen different memory "segments", each of which held 65,536 bytes of memory. Since COM files were never intended to span multiple segments, Microsoft created a successor file format that allowed for larger programs: .EXE.

COM files in DOS set all x86 segment registers to the same value and the SP (stack pointer) register to the offset of the last word available in the first 64 KiB segment (typically FFFEh) or the maximum size of memory available in the block the program is loaded into for both, the program plus at least 256 bytes stack, whatever is smaller, thus the stack begins at the very top of the corresponding memory segment and works down from there.

In the original DOS 1.x API, which was a derivative of the CP/M API, program termination of a COM file would be performed by calling the INT 20h (Terminate Program) function or else INT 21h Function 0, which served the same purpose, and the programmer also had to ensure that the code and data segment registers contained the same value at program termination to avoid a potential system crash. Although this could be used in any DOS version, Microsoft recommended the use of INT 21h Function 4Ch for program termination from DOS 2.x onward, which did not require the data and code segment to be set to the same value.

Under CP/M 3, if the first byte of a COM file is C9h, there is a 256-byte header; since C9h corresponds to the 8080 instruction RET, this means that the COM file will immediately terminate if run on an earlier version of CP/M that does not support this extension. (Because the instruction sets of the 8085 and Z80 are supersets of the 8080 instruction set, this works on all three processors.) C9h is an invalid opcode on the 8088/8086, however it is the opcode for LEAVE since the 80188/80186. Albeit possible, LEAVE is unlikely to be used as the first instruction in a valid program. Thus the executable loader in some versions of DOS rejects COM files that start with C9h , avoiding a crash.

Files may have names ending in .COM, but not be in the simple format described above; this is indicated by a magic number at the start of the file. For example, the COMMAND.COM file in DR DOS 6.0 is actually in DOS executable format, indicated by the first two bytes being 4D 5A (MZ in ASCII), the initials of Mark Zbikowski.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication