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 has a maximum size of 65,280 (FF00
h) bytes (256 bytes short of 64 KiB) and stores all its
code and
data in one
segment. Since it lacks
relocation information, it is
loaded by the operating system at a pre-set address, at offset 0100h immediately following the
PSP, where it is executed (hence the limitation of the executable's size): the
entry point is fixed at 0100h. This was not an issue on 8-bit machines since they can only address 64k of memory, but 16-bit machines have a much larger address space, which is why the format fell out of use. 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. COM files fit this model perfectly. Before the introduction of
MP/M and
Concurrent CP/M, there was no possibility of running more than one program or command at a time: the program loaded at 0100h was run, and no other. 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). .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. It is possible to make a .COM file to run under both operating systems in form of a
fat binary. There is no true compatibility at the instruction level; the instructions at the
entry point are chosen to be equal in functionality but different in both operating systems, and make program execution jump to the section for the operating system in use. It is basically two different programs with the same functionality in a single file, preceded by code selecting the one to use. 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. ==Large programs==