EXE vs COM, Assembling/Linking, One- & Two-Pass Assemblers
Open in the Padhaiko app3.9 EXE and COM programs
MS-DOS and 8086 systems support two distinct executable file formats: .COM (Command) and .EXE (Executable). Both use a 256-byte Program Segment Prefix (PSP) created by DOS at load time (0000H–00FFH), but they differ sharply in memory model, header structure, and startup register setup.
COM and EXE Program Formats
.COMProgram: A raw, headerless binary memory image restricted to a single 64 KB segment (TINYmemory model). Code, data, stack, and the 256-byte PSP all share the same segment (CS = DS = SS = ES). Execution always starts atCS:0100H, requiringORG 100Hat the start of the source code..EXEProgram: A structured multi-segment executable capable of exceeding 64 KB (usingSMALL,MEDIUM,COMPACT,LARGE, orHUGEmodels). Every.EXEfile begins with a header (at least 512 bytes, starting with signatureMZ=4D5AH) containing a relocation table and initialCS:IPandSS:SPvalues so the DOS loader can place code, data, and stack in separate segments.
| Aspect | COM Program (.COM) | EXE Program (.EXE) |
|---|---|---|
| Memory Model & Size Limit | TINY model; maximum size 64 KB - 256 bytes (65,280 B) | Multi-segment (SMALL, LARGE, etc.); can exceed 64 KB up to available RAM |
| File Structure | Exact binary image; no header and no relocation table | Begins with a >= 512-byte header (MZ) + relocation table |
| Segment Registers at Load | CS = DS = SS = ES all point to the PSP segment | DS = ES point to PSP; CS and SS are loaded from EXE header |
Entry Point & ORG | Always starts at CS:0100H (ORG 100H mandatory) | Defined by END label (CS:IP set from EXE header; typically 0000H) |
| Stack Setup | DOS sets SP = FFFEH and pushes 0000H inside the same segment | Requires explicit .STACK segment; SS:SP initialized from header |
Initializing DS | Not needed (DS already equals CS) | Mandatory (MOV AX, @DATA then MOV DS, AX) |
| Program Termination | RET (pops 0000H -> INT 20H at PSP), INT 20H, or INT 21H AH=4CH | INT 21H with AH = 4CH (standard clean exit) |
; === Minimal .COM Template (Tiny Model) ===
.MODEL TINY
.CODE
ORG 100H ; Leave 256 bytes (0000H-00FFH) for PSP
START:
JMP MAIN ; Jump over inline data to code
NUM1 DW 1000H
NUM2 DW 0250H
MAIN:
MOV AX, NUM1 ; DS already equals CS in .COM
ADD AX, NUM2
RET ; Returns to CS:0000H (INT 20H in PSP)
END START3.10 Assembling, linking and execution
Converting human-readable 8086 assembly source code into a running program is a multi-stage pipeline involving an editor, assembler, linker, and OS loader.
[ Source: prog.asm ]
|
v (Assembler: MASM / TASM)
+--------+----------------------------+
| prog.obj (Relocatable Object Code) |---> prog.lst (Listing: offsets + hex)
+-------------------------------------+---> prog.crf (Cross-reference symbols)
|
v (Linker: LINK / TLINK + libraries.lib)
+--------+----------------------------+
| prog.exe (Header + Machine Code) |---> prog.map (Segment memory map)
+-------------------------------------+
|
+---(Optional: EXE2BIN / TLINK /t)---> prog.com (Raw binary image)
|
v (DOS Loader / COMMAND.COM)
[ Build 256-byte PSP -> Load to RAM -> Relocate Segments -> Set CS:IP, SS:SP -> Run ]Stages from Source File to Execution
- Editing (
.asm): The programmer writes instructions and directives in an ASCII text file (prog.asm). - Assembling (
MASM/TASM): Checks syntax, assigns offsets, translates mnemonics into machine code, and outputs:.obj(Object File): Binary machine code and relocation/external symbol records (not yet runnable)..lst(List File, optional): Side-by-side offset addresses, assembled hex bytes, source lines, and symbol table..crf(Cross-Reference File, optional): Lists where each identifier is defined and referenced.
- Linking (
LINK/TLINK): Combines one or more.objfiles and.liblibraries, resolves cross-moduleEXTRN/PUBLICsymbols, computes segment relative offsets, prepends theMZrelocation header, and produces.exe(plus optional.mapsegment layout summary). For.COMprograms,EXE2BIN(orTLINK /t) strips the header into.com. - Loading & Execution (DOS Loader): Allocates free RAM, constructs the 256-byte PSP at segment offset
0000H–00FFH(containingINT 20Hat0000Hand command-line arguments at0080H), loads the program immediately after the PSP, patches segment relocation entries (for.EXE), setsCS:IP,SS:SP,DS,ES, and jumps to the entry point.
3.11 One pass and two pass assemblers
When assembling top-to-bottom, an assembler frequently encounters a forward reference—an instruction that uses a label or variable before that symbol has been defined further down in the file (for example, JMP EXIT on line 5 when EXIT: is on line 20). Because the assembler does not yet know the offset of EXIT, it cannot immediately fill in the jump displacement. Assemblers handle this in two ways:
Two-Pass vs. One-Pass Assembler Operation
- Two-Pass Assembler: Reads the source program twice:
- Pass 1 (Symbol Discovery): Initializes the Location Counter (
LC = 0), scans every statement, evaluatesEQU/ORG, determines the byte size of each instruction/data item to advanceLC, and records every label/variable with its segment-relative address in the Symbol Table (SYMTAB). No final machine code is emitted yet. - Pass 2 (Code Synthesis): Resets
LC = 0, rescans the source (or an intermediate file from Pass 1), looks up instruction opcodes in the Opcode Table (OPTAB), fetches target addresses for all symbols (including forward references) from SYMTAB, and emits.objand.lstfiles.
- Pass 1 (Symbol Discovery): Initializes the Location Counter (
- One-Pass Assembler: Reads the source program only once (
load-and-goor single-pass.objemitter):- When a forward reference is encountered, the assembler leaves placeholder zero bytes in memory (or object buffer) and adds the instruction's location to a Forward Reference Table (Backpatching List) linked to that undefined symbol.
- As soon as the symbol's definition is reached later in the same pass, the assembler records its address in SYMTAB and walks the backpatching list to fill in ("patch") every pending placeholder.
| Aspect | One-Pass Assembler | Two-Pass Assembler |
|---|---|---|
| Source File Scans | Single scan (faster I/O) | Two complete scans (Pass 1 + Pass 2) |
| Forward Reference Handling | Uses placeholder bytes + in-memory backpatching list | Pass 1 records all symbols in SYMTAB; Pass 2 resolves cleanly |
| Memory Requirement | Higher RAM needed to buffer assembled code / fixup chains | Lower in-memory buffer needed; intermediate file can reside on disk |
| Restrictions | Cannot easily handle forward references in EQU or size-changing expressions | Handles general forward references, SHORT/NEAR optimization, and full .lst output |
| Typical Use | Load-and-go classroom/debugging assemblers | Production assemblers (MASM, TASM) |
1
Suppose DOS loads a 512-byte .COM program starting its PSP at segment 1200H. (a) What are the initial values of CS, DS, SS, ES, IP, and SP when execution begins? (b) At what 20-bit physical address is the very first instruction fetched, and what happens when the program executes RET?
Initial Registers Loaded by DOS for .COM:
- All four segment registers point to the PSP segment:
CS = DS = SS = ES = 1200H. - Execution skips the 256-byte (
0100H) PSP, soIP = 0100H. - DOS initializes
SP = FFFEHand pushes0000Honto the stack (SS:FFFEH, physical21FFEH), leaving65,280 bytes(FF00Hbytes from0100HtoFFFEH) shared by code, data, and stack.
First Physical Address and RET Behavior:
- First instruction physical address =
(CS * 10H) + IP = (1200H * 10H) + 0100H = 12100H. - At the start of the PSP (
1200:0000H, physical12000H), DOS always placesCDH 20H, which is the machine code forINT 20H(Terminate Program). - When the
.COMprogram finishes withRET, the CPU pops0000HfromSS:FFFEHintoIP, jumping toCS:0000Hand executingINT 20Hto return cleanly to DOS.
(a) CS = DS = SS = ES = 1200H, IP = 0100H, SP = FFFEH (with 0000H on stack); (b) First instruction is fetched at 12100H, and RET pops 0000H into IP, executing INT 20H at 1200:0000H to exit to DOS.
2
Trace how a two-pass assembler processes the following 8086 code fragment starting at LC = 0000H (instruction byte lengths are given in parentheses):
START: MOV AX, 0005H(3 bytes)CMP AX, 0000H(3 bytes)JZ DONE(2 bytes: opcode74H+ 8-bit signed relative displacementdisp8)DEC AX(1 byte)MOV BX, AX(2 bytes)DONE: MOV AH, 4CH(2 bytes)INT 21H(2 bytes)
Construct the Pass 1 Symbol Table and compute the Pass 2 relative displacement byte for JZ DONE.
Pass 1: Advancing Location Counter (LC) and Building SYMTAB:
| Line | Statement | LC (Start) | Size | Symbol Added |
|---|---|---|---|---|
| 1 | START: MOV AX, 0005H | 0000H | 3 | START = 0000H |
| 2 | CMP AX, 0000H | 0003H | 3 | — |
| 3 | JZ DONE | 0006H | 2 | (Forward ref DONE unresolved) |
| 4 | DEC AX | 0008H | 1 | — |
| 5 | MOV BX, AX | 0009H | 2 | — |
| 6 | DONE: MOV AH, 4CH | 000BH | 2 | DONE = 000BH |
| 7 | INT 21H | 000DH | 2 | — |
Note: Let us verify the cumulative sizes carefully: 0000H + 3 = 0003H; 0003H + 3 = 0006H; 0006H + 2 = 0008H; 0008H + 1 = 0009H; 0009H + 2 = 000BH; 000BH + 2 = 000DH; 000DH + 2 = 000FH. At the end of Pass 1, SYMTAB contains START = 0000H and DONE = 000BH.
Pass 2: Resolving JZ DONE Machine Code:
JZ DONEsits at0006Hand is 2 bytes long, soIP_next = 0006H + 0002H = 0008H.- From SYMTAB,
DONE = 000BH. disp8 = 000BH - 0008H = +03H(skips the 1-byteDEC AXand 2-byteMOV BX, AX).- Thus Pass 2 emits
74 03at offset0006H.
Pass 1 builds SYMTAB = {START: 0000H, DONE: 000BH}. In Pass 2, IP_next = 0008H, giving disp8 = 000BH - 0008H = 03H, so JZ DONE assembles to 74H 03H.
3
Show how a one-pass assembler resolves JZ DONE in the code from Example 2 without scanning the source file a second time.
Recording the Unresolved Forward Reference:
- At
LC = 0006H, the one-pass assembler encountersJZ DONE. - It emits opcode
74Hat0006Hand a placeholder00Hat0007H, while adding an entry to the Backpatching / Forward Reference List forDONE:DONE -> [patch_addr = 0007H, ip_next = 0008H, width = 1 byte]
Backpatching When DONE: Is Reached:
- When the single scan reaches
LC = 000BH(DONE:), it insertsDONE = 000BHinto SYMTAB. - Seeing pending entries for
DONEon the backpatching list, it immediately computes000BH - 0008H = 03Hand overwrites memory/buffer byte0007Hfrom00Hto03H, producing74H 03Hin a single pass.
The one-pass assembler emits 74H 00H at 0006H–0007H, logs 0007H (with IP_next = 0008H) in a forward-reference list for DONE, and backpatches 0007H with 000BH - 0008H = 03H as soon as DONE: is reached at 000BH.
Exercises
Compare .EXE and .COM program formats across memory size, file header, initial segment registers, ORG directive, and program termination.
Key Differences Between .COM and .EXE:
- Size & Model:
.COMusesTINYmodel (<=64 KB - 256 Btotal for PSP + code + data + stack);.EXEsupports multiple segments (SMALLtoHUGE, > 64 KB). - Header:
.COMhas no header (raw binary image);.EXEhas a >= 512-byteMZheader with relocation table and initialCS:IP,SS:SP. - Initial Segments:
.COMstarts withCS = DS = SS = ES = PSP;.EXEsetsDS = ES = PSPwhileCSandSScome from the header (so code must initializeDS). - Origin & Entry:
.COMrequiresORG 100Hand starts atCS:0100H;.EXEnormally starts at0000HviaEND label. - Termination:
.COMcan end withRET(jumping toINT 20HatCS:0000H),INT 20H, orINT 21H AH=4CH;.EXEusesINT 21H AH=4CH.
.COM is a headerless single-segment (<= ~64 KB) raw image starting at ORG 100H with CS=DS=SS=ES, whereas .EXE has an MZ relocation header, supports multi-segment programs > 64 KB, initializes CS/SS separately from DS/ES, and exits via INT 21H AH=4CH.
Describe the complete sequence of steps—and the intermediate/output files produced—when writing, assembling, linking, and executing an 8086 assembly program under MS-DOS.
Build and Execution Stages:
- Edit (
prog.asm): Create the assembly source file. - Assemble (
MASM prog.asmorTASM prog.asm): Translates mnemonics and directives into relocatable binary object fileprog.obj, optionally producing listingprog.lstand cross-referenceprog.crf. - Link (
LINK prog.objorTLINK prog.obj): Combines.objmodules and.liblibraries, resolves external symbols, adds theMZrelocation header, and produces executableprog.exe(and optionalprog.map). For.COM,EXE2BINstrips the header. - Load & Execute: DOS allocates RAM, creates the 256-byte PSP (
0000H–00FFH), loads segments, patches relocation entries, setsCS:IPandSS:SP, and starts execution.
Edit .asm -> Assemble (MASM/TASM) to .obj (.lst, .crf) -> Link (LINK/TLINK) to .exe (.map, or EXE2BIN to .com) -> DOS loader builds PSP, relocates addresses, initializes registers, and executes.
Rewrite the .EXE program below as a valid .COM program: .MODEL SMALL / .STACK 100H / .DATA (VAL DB 41H) / .CODE (MAIN PROC, MOV AX, @DATA, MOV DS, AX, MOV DL, VAL, MOV AH, 02H, INT 21H, MOV AH, 4CH, INT 21H, MAIN ENDP, END MAIN).
Rules for Converting .EXE to .COM:
- Change
.MODEL SMALLto.MODEL TINYand remove.STACK 100Hand.DATA(everything goes into.CODE). - Add
ORG 100Hright after.CODEso code/data begin after the 256-byte PSP. - Jump past inline data (
JMP START) so the CPU does not try to executeVAL DB 41Has an opcode. - Remove
MOV AX, @DATA/MOV DS, AXbecauseDSalready equalsCSin a.COMprogram.
Converted .COM Program:
.MODEL TINY
.CODE
ORG 100H
MAIN PROC
JMP START
VAL DB 41H ; Inline data inside the single code segment
START:
MOV DL, VAL ; DS = CS automatically
MOV AH, 02H ; Display character 'A'
INT 21H
RET ; Or MOV AH, 4CH / INT 21H
MAIN ENDP
END MAINUse .MODEL TINY and .CODE only, add ORG 100H, place VAL DB 41H after a JMP START bypass, remove @DATA/DS initialization, and exit with RET or INT 21H AH=4CH.
What is the forward reference problem in assembly language, and how does a two-pass assembler solve it differently from a one-pass assembler?
The Forward Reference Problem: A forward reference occurs when an instruction references a label or variable before that symbol is defined later in the source code (e.g., jumping forward to END_LOOP). On the first encounter, the assembler does not yet know the target symbol's offset address.
Two-Pass vs. One-Pass Solution:
- Two-Pass Assembler: Pass 1 scans the entire source solely to track the Location Counter and populate the Symbol Table with every label's offset; Pass 2 rescans and generates machine code using the completed Symbol Table.
- One-Pass Assembler: Scans once, emits zero placeholders for unknown targets while recording their addresses in a backpatching list, and patches those memory/buffer locations as soon as the symbol definition is reached later in the pass.
A forward reference uses a symbol before its definition. A two-pass assembler resolves it by building the full Symbol Table in Pass 1 and generating code in Pass 2, whereas a one-pass assembler emits placeholders plus a backpatching list and patches them once the symbol is defined.
In a two-pass assembler starting at LC = 0000H, suppose: 0000H: MOV CX, 0004H (3 bytes) 0003H: JMP SKIP (2 bytes: EBH disp8) 0005H: ADD AX, BX (2 bytes) 0007H: INC CX (1 byte) 0008H: SKIP: MOV DX, AX (2 bytes) Find the Symbol Table value of SKIP from Pass 1 and the machine code byte disp8 generated for JMP SKIP in Pass 2.
Pass 1 Symbol Address for SKIP:
MOV CX, 0004H:0000H(3 bytes -> nextLC = 0003H)JMP SKIP:0003H(2 bytes -> nextLC = 0005H)ADD AX, BX:0005H(2 bytes -> nextLC = 0007H)INC CX:0007H(1 byte -> nextLC = 0008H)- Therefore
SKIP = 0008Hin Pass 1 SYMTAB.
Pass 2 Jump Displacement:
JMP SKIPoccupies0003H–0004H, soIP_next = 0005H.disp8 = SKIP - IP_next = 0008H - 0005H = 03H.- Machine code emitted at
0003HisEBH 03H.
Pass 1 records SKIP = 0008H; Pass 2 computes disp8 = 0008H - 0005H = 03H, assembling JMP SKIP as EBH 03H.