PadhaikoAll subjects

EXE vs COM, Assembling/Linking, One- & Two-Pass Assemblers

Microprocessors (ENEX 201)

Open in the Padhaiko app

3.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.

Definition

COM and EXE Program Formats

AspectCOM Program (.COM)EXE Program (.EXE)
Memory Model & Size LimitTINY model; maximum size 64 KB - 256 bytes (65,280 B)Multi-segment (SMALL, LARGE, etc.); can exceed 64 KB up to available RAM
File StructureExact binary image; no header and no relocation tableBegins with a >= 512-byte header (MZ) + relocation table
Segment Registers at LoadCS = DS = SS = ES all point to the PSP segmentDS = ES point to PSP; CS and SS are loaded from EXE header
Entry Point & ORGAlways starts at CS:0100H (ORG 100H mandatory)Defined by END label (CS:IP set from EXE header; typically 0000H)
Stack SetupDOS sets SP = FFFEH and pushes 0000H inside the same segmentRequires explicit .STACK segment; SS:SP initialized from header
Initializing DSNot needed (DS already equals CS)Mandatory (MOV AX, @DATA then MOV DS, AX)
Program TerminationRET (pops 0000H -> INT 20H at PSP), INT 20H, or INT 21H AH=4CHINT 21H with AH = 4CH (standard clean exit)
Minimal .COM Program Skeleton vs. .EXE Program Skeleton8086 ASM
; === 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 START

3.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.

8086 Assembly Build & Execution PipelineText
  [ 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 ]
Definition

Stages from Source File to Execution

  1. Editing (.asm): The programmer writes instructions and directives in an ASCII text file (prog.asm).
  2. 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.
  3. Linking (LINK / TLINK): Combines one or more .obj files and .lib libraries, resolves cross-module EXTRN/PUBLIC symbols, computes segment relative offsets, prepends the MZ relocation header, and produces .exe (plus optional .map segment layout summary). For .COM programs, EXE2BIN (or TLINK /t) strips the header into .com.
  4. Loading & Execution (DOS Loader): Allocates free RAM, constructs the 256-byte PSP at segment offset 0000H–00FFH (containing INT 20H at 0000H and command-line arguments at 0080H), loads the program immediately after the PSP, patches segment relocation entries (for .EXE), sets CS: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:

Definition

Two-Pass vs. One-Pass Assembler Operation

AspectOne-Pass AssemblerTwo-Pass Assembler
Source File ScansSingle scan (faster I/O)Two complete scans (Pass 1 + Pass 2)
Forward Reference HandlingUses placeholder bytes + in-memory backpatching listPass 1 records all symbols in SYMTAB; Pass 2 resolves cleanly
Memory RequirementHigher RAM needed to buffer assembled code / fixup chainsLower in-memory buffer needed; intermediate file can reside on disk
RestrictionsCannot easily handle forward references in EQU or size-changing expressionsHandles general forward references, SHORT/NEAR optimization, and full .lst output
Typical UseLoad-and-go classroom/debugging assemblersProduction assemblers (MASM, TASM)
Example 1

1 (4 marks)

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, so IP = 0100H.
  • DOS initializes SP = FFFEH and pushes 0000H onto the stack (SS:FFFEH, physical 21FFEH), leaving 65,280 bytes (FF00H bytes from 0100H to FFFEH) shared by code, data, and stack.
Initial 8086 State at .COM Entry
CS1200H
DS1200H
SS1200H
ES1200H
IP0100H
SPFFFEH
[21FFEH]00H
[21FFFH]00H
[12000H]CDH
[12001H]20H

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, physical 12000H), DOS always places CDH 20H, which is the machine code for INT 20H (Terminate Program).
  • When the .COM program finishes with RET, the CPU pops 0000H from SS:FFFEH into IP, jumping to CS:0000H and executing INT 20H to return cleanly to DOS.
Answer:

(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.

Example 2

2 (6 marks)

Trace how a two-pass assembler processes the following 8086 code fragment starting at LC = 0000H (instruction byte lengths are given in parentheses):

  1. START: MOV AX, 0005H (3 bytes)
  2. CMP AX, 0000H (3 bytes)
  3. JZ DONE (2 bytes: opcode 74H + 8-bit signed relative displacement disp8)
  4. DEC AX (1 byte)
  5. MOV BX, AX (2 bytes)
  6. DONE: MOV AH, 4CH (2 bytes)
  7. 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:

Pass 1 Location Counter & Symbol Table Trace
LineStatementLC (Start)SizeSymbol Added
1START: MOV AX, 0005H0000H3START = 0000H

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 DONE sits at 0006H and is 2 bytes long, so IP_next = 0006H + 0002H = 0008H.
  • From SYMTAB, DONE = 000BH.
  • disp8 = 000BH - 0008H = +03H (skips the 1-byte DEC AX and 2-byte MOV BX, AX).
  • Thus Pass 2 emits 74 03 at offset 0006H.
Answer:

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.

Example 3

3 (4 marks)

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 encounters JZ DONE.
  • It emits opcode 74H at 0006H and a placeholder 00H at 0007H, while adding an entry to the Backpatching / Forward Reference List for DONE: DONE -> [patch_addr = 0007H, ip_next = 0008H, width = 1 byte]

Backpatching When DONE: Is Reached:

  • When the single scan reaches LC = 000BH (DONE:), it inserts DONE = 000BH into SYMTAB.
  • Seeing pending entries for DONE on the backpatching list, it immediately computes 000BH - 0008H = 03H and overwrites memory/buffer byte 0007H from 00H to 03H, producing 74H 03H in a single pass.
Answer:

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

Question 1

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:

  1. Size & Model: .COM uses TINY model (<= 64 KB - 256 B total for PSP + code + data + stack); .EXE supports multiple segments (SMALL to HUGE, > 64 KB).
  2. Header: .COM has no header (raw binary image); .EXE has a >= 512-byte MZ header with relocation table and initial CS:IP, SS:SP.
  3. Initial Segments: .COM starts with CS = DS = SS = ES = PSP; .EXE sets DS = ES = PSP while CS and SS come from the header (so code must initialize DS).
  4. Origin & Entry: .COM requires ORG 100H and starts at CS:0100H; .EXE normally starts at 0000H via END label.
  5. Termination: .COM can end with RET (jumping to INT 20H at CS:0000H), INT 20H, or INT 21H AH=4CH; .EXE uses INT 21H AH=4CH.
Answer:

.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.

Question 2

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:

  1. Edit (prog.asm): Create the assembly source file.
  2. Assemble (MASM prog.asm or TASM prog.asm): Translates mnemonics and directives into relocatable binary object file prog.obj, optionally producing listing prog.lst and cross-reference prog.crf.
  3. Link (LINK prog.obj or TLINK prog.obj): Combines .obj modules and .lib libraries, resolves external symbols, adds the MZ relocation header, and produces executable prog.exe (and optional prog.map). For .COM, EXE2BIN strips the header.
  4. Load & Execute: DOS allocates RAM, creates the 256-byte PSP (0000H–00FFH), loads segments, patches relocation entries, sets CS:IP and SS:SP, and starts execution.
Answer:

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.

Question 3

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 SMALL to .MODEL TINY and remove .STACK 100H and .DATA (everything goes into .CODE).
  • Add ORG 100H right after .CODE so code/data begin after the 256-byte PSP.
  • Jump past inline data (JMP START) so the CPU does not try to execute VAL DB 41H as an opcode.
  • Remove MOV AX, @DATA / MOV DS, AX because DS already equals CS in a .COM program.

Converted .COM Program:

Converted .COM Program8086 ASM
.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 MAIN
Answer:

Use .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.

Question 4

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.
Answer:

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.

Question 5

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 -> next LC = 0003H)
  • JMP SKIP: 0003H (2 bytes -> next LC = 0005H)
  • ADD AX, BX: 0005H (2 bytes -> next LC = 0007H)
  • INC CX: 0007H (1 byte -> next LC = 0008H)
  • Therefore SKIP = 0008H in Pass 1 SYMTAB.

Pass 2 Jump Displacement:

  • JMP SKIP occupies 0003H–0004H, so IP_next = 0005H.
  • disp8 = SKIP - IP_next = 0008H - 0005H = 03H.
  • Machine code emitted at 0003H is EBH 03H.
Answer:

Pass 1 records SKIP = 0008H; Pass 2 computes disp8 = 0008H - 0005H = 03H, assembling JMP SKIP as EBH 03H.

Past exam questions for Microprocessors

← Assembly Language Programming, Syntax & DirectivesInterrupt Services: INT 21H and INT 10H →