Chapter 3.2: Assembly Language
Introduction
Learning Outcomes
Relate assembly language to machine language and to high-level languages.
Read a source line as label, mnemonic, operands and comment; tell an instruction from a directive.
Recognize register, immediate and memory operands, and the implicit operands of instructions.
Compare register and memory forms of an instruction in clock cycles.
Follow how IP advances and which instructions change the flags.
Write a simple .COM program in emu8086 and single-step through it.
Assembly Language
Assembly Language
- A low-level language: each instruction corresponds to exactly one machine instruction.
- Instead of raw numbers it uses readable mnemonics:
MOV,ADD,JMP… - Writing it requires knowing the machine: registers, memory, the system bus.
- It is the best way to understand how a computer is organized and how a program really runs.
- Every processor family has its own assembly language; this course uses the Intel 8086 and emu8086.
Assembly: MOV AX, 5
Machine code in hex: B8 05 00
What the CPU sees:10111000 00000101 00000000
The 16-bit value 0005h is stored low byte first (05 00).
Assembly Language
The Assembler and emu8086
Examples in this course use emu8086 syntax; most also assemble with MASM/TASM.
Assembly Language
Why Assembly?
A program written in assembly language:
Pros
- Is smaller (no unnecessary code).
- Is faster, since it does only what it has to.
- Can do what high-level languages cannot: port I/O, interrupt handling, direct access to flags.
- Is indispensable for boot loaders, device drivers and embedded systems.
Cons
- Not portable: 8086 code does not run on ARM.
- Tedious to write, read and debug.
- Unproductive for large projects; modern compilers usually generate code that is fast enough.
Assembly Language
Source Line Format
[label:] mnemonic [dest], [source] [; comment]| Line | Label | Mnemonic | Operands | Comment |
|---|---|---|---|---|
start: MOV AX, 5 ; AX ← 5 | start | MOV | AX, 5 | ; AX ← 5 |
INC AX | — | INC | AX | — |
RET | — | RET | none | — |
- With two operands the order is destination, source:
MOV AX, BX→ AX ← BX. - The assembler is case-insensitive:
mov ax, bxandMOV AX, BXare the same. - Everything from
;to the end of the line is a comment and produces no machine code.
Assembly Language
Instruction or Directive?
| Line | Kind | What it puts in memory |
|---|---|---|
MOV AX, 5 | instruction | machine code: B8 05 00 |
ORG 100h | directive | nothing; sets the location counter to 0100h |
num DB 0 | directive (data definition) | 1 byte of data: 00 |
k EQU 5 | directive | nothing; the name k stands for 5 |
END | directive | nothing; the source ends here |
PUTC 'A' | macro call | code for the instructions in the macro body |
Assembly Language
Labels and Names
start: MOV CX, 3 ; code label again: INC AX LOOP again ; an address RET num DB 7 ; data name
- A code label ends with a colon and stands for the address of an instruction (its offset within CS); it generates no code itself.
- A variable name has no colon; the assembler records its name, type (DB, DW…) and address.
- A name starts with a letter or underscore, never a digit; letters, digits and underscores may follow.
- Instruction, register and directive names (MOV, AX, DB, END…) are reserved.
- Case does not matter:
againandAGAINare the same label; defining both gives a “duplicate definition” error.
| Name | _count | sum2 | 2sum | cx | last-val |
|---|---|---|---|---|---|
| Valid? | yes | yes | starts with a digit | register name | − is an operator |
Working with Registers
Operand Types and Implicit Operands
MOV AX, BX: the value comes from inside the processor; the fastest kind.MOV AX, 5: the value is part of the instruction bytes; it can only be a source.MOV AX, [BX]: the value is read from memory in a bus cycle.| Instruction | Explicit operand | Implicit operand(s) |
|---|---|---|
MUL BL | BL | AL (multiplicand), AX (product) |
DIV BL | BL | AX (dividend), AL (quotient), AH (remainder) |
LOOP again | again | CX (counter) |
CBW | — | AL → AX |
PUSH AX | AX | SS:SP (top of stack) |
XLAT | — | AL (index and result), BX (table address) |
An implicit operand is not written, yet it changes: MUL BL also overwrites AH. A program that needs to keep a register’s value must know the implicit operands of the instructions it uses.
Working with Registers · Step through the code
Code Trace: Implicit Operands of MUL, SHR and LOOP
ORG 100h MOV AL, 12 MOV BL, 10 MUL BL ; AX = AL × BL MOV CL, 2 SHR AX, CL ; count in CL MOV CX, 3 ; LOOP counter again: ADD AX, 1 LOOP again ; CX - 1 RET
AL = 12, BL = 10. AH starts at 0.
MUL BL names only BL; the multiplicand is implicitly AL and the product implicitly AX: AX = 120 = 0078h. The product fits in 8 bits, so CF = 0.
SHR AX, CL: a shift count above 1 must be in CL. 120 ÷ 4 = 30 → AX = 001Eh.
CX = 3: the implicit counter of LOOP. CL is part of CX, so the old count 2 is gone.
Pass 1: AX + 1 = 31; LOOP decrements CX: CX = 2, not zero → again.
Pass 2: AX + 1 = 32; LOOP decrements CX: CX = 1, not zero → again.
Pass 3: AX + 1 = 33; LOOP decrements CX: CX = 0, the loop ends.
RET. The program never decrements CX explicitly, yet LOOP changed it three times: AX = 0021h = 33.
AX, CL and CX were “hidden” operands in this program; the special roles of the registers come from these implicit uses.
Working with Registers
Register or Memory?
- Inside the CPU; no bus cycle needed.
- Very few of them (the 8086 has 14 16-bit registers).
- Ideal for intermediate results and counters.
- Outside the CPU; each access takes at least one bus cycle (4 clock periods on the 8086).
- Large: up to 1 MB.
- Holds the program code, variables, arrays and the stack.
CX instead of memory, for example, speeds up the loop.14 registers: 8 general-purpose + 4 segment + IP + FLAGS.
Working with Registers
Instruction Timing: Register vs. Memory
| Instruction (8086) | Clocks |
|---|---|
MOV AX, BX | 2 |
MOV AX, 5 | 4 |
MOV AX, num | 10 |
MOV CX, [BX] | 8 + 5 = 13 |
ADD AX, BX | 3 |
ADD AX, num | 9 + 6 = 15 |
INC CX | 2 |
INC count (word) | 15 + 6 = 21 |
- A memory operand adds the effective address calculation (EA) time: direct address 6,
[BX]5,[BX+SI]7 clocks. - Instructions that write memory take longer:
INC countreads, then writes (two bus cycles). - Loading AX from a direct address uses the short accumulator form: 10 clocks.
- In a loop that runs 1000 times, incrementing a counter in CX costs 1000 × 2 = 2000 clocks; in memory, 1000 × 21 = 21,000. At 5 MHz that is 4.2 ms instead of 0.4 ms.
IP and Flags · Step through the code
Code Trace: How Does IP Advance?
The program is loaded at CS:0100h; IP = 0100h points to the first instruction.
MOV AX, 5 (B8 05 00) is fetched: IP = 0103h; once executed, AX = 0005h.
Three more bytes: IP = 0106h, BX = 0002h.
INC AX is a single byte (40h): IP = 0107h, AX = 0006h.
ADD AX, BX is 2 bytes: IP = 0109h, AX = 0008h.
RET pops 0000h off the stack: IP = 0000h, SP = 0000h. The INT 20h at PSP:0000 ends the program.
ORG 100h MOV AX, 5 ; 3 bytes MOV BX, 2 ; 3 bytes INC AX ; 1 byte ADD AX, BX ; 2 bytes RET ; 1 byte
IP and Flags · Step through the code
Code Trace: Which Instructions Touch the Flags?
ORG 100h STC ; CF = 1 MOV AL, 0FFh ; flags kept INC AL ; CF untouched MOV CX, 0 ; ZF kept DEC AL ADD AL, 1 ; new CF CLC NOT AL ; flags kept RET
STC: CF = 1. It sets a flag directly.
MOV AL, 0FFh: AL changed, none of the flags did. MOV does not affect flags.
INC AL: FFh + 1 = 00h → ZF = 1, AF = 1, PF = 1. CF is still 1: INC and DEC never touch the carry flag.
MOV CX, 0: the result is zero, but ZF is not recomputed; it keeps the value from the previous instruction.
DEC AL: 00h − 1 = FFh → SF = 1, ZF = 0. CF is still 1: DEC does not record the borrow in CF.
ADD AL, 1: FFh + 1 = 100h → AL = 00h. ADD rewrites every status flag: this time CF = 1 is ADD’s own carry.
CLC: CF = 0.
NOT AL: AL = FFh, yet ZF is still 1. NOT changes no flags; to test the result, follow it with TEST AL, AL or CMP.
MOV, NOT, XCHG, LEA, PUSH and POP leave the flags alone; INC and DEC change all but CF. Before a conditional jump, check which instruction last set the flag.
A First Program
.COM Program Template
ORG 100h ; .COM: code starts at 0100h MOV AX, 5 ; AX = 5 MOV BX, 3 ; BX = 3 ADD AX, BX ; AX = AX + BX = 8 MOV CL, AL ; CL = 08h RET ; back to DOS num DB 0 ; variables go after RET
ORG 100h: a .COM program is loaded at offset 0100h of its segment; the first 256 bytes (the PSP) belong to DOS.- Instructions execute top to bottom, in order.
RETreturns to the operating system.- Variables go at the end (after RET) so the CPU never executes them as instructions.
- A .COM program starts with
CS = DS = ES = SS,IP = 0100h,SP = FFFEh.
A First Program · Step through the code
Code Trace: A .COM Program in Memory
ORG 100h ; code at 0100h MOV AX, 5 MOV BX, 3 ADD AX, BX ; AX = 8 MOV CL, AL ; CL = 08h RET ; back to DOS num DB 0 ; data
The program is loaded at 0700:0100. CS = DS = ES = SS = 0700h, IP = 0100h, SP = FFFEh; the top of the stack holds 0000h.
MOV AX, 5 (3 bytes): AX = 0005h, IP = 0103h.
MOV BX, 3 (3 bytes): BX = 0003h, IP = 0106h.
ADD AX, BX (2 bytes): AX = 0008h, IP = 0108h.
MOV CL, AL (2 bytes): CL = 08h, IP = 010Ah.
RET pops 0000h: IP = 0000h, SP = 0000h. The INT 20h at PSP:0000 ends the program; num at 010Bh is never executed.
The instructions are 3, 3, 2, 2 and 1 bytes long; IP advances by each length. The data sits after RET, so it is never executed.
A First Program
The Program in Memory, Byte by Byte
| Offset | Bytes | Source line | Note |
|---|---|---|---|
| 0100h | B8 05 00 | MOV AX, 5 | B8 + register number (AX = 0); immediate little-endian |
| 0103h | BB 03 00 | MOV BX, 3 | B8 + 3 (BX) |
| 0106h | 03 C3 | ADD AX, BX | C3 = 11 000 011: register–register, AX, BX |
| 0108h | 8A C8 | MOV CL, AL | C8 = 11 001 000: CL, AL |
| 010Ah | C3 | RET | one-byte instruction |
| 010Bh | 00 | num DB 0 | data, not an instruction |
- The .COM file is exactly these 12 bytes: there is no header; DOS copies them into memory starting at 0100h.
ORG 100hproduces no bytes; it only sets where the first byte goes.- One instruction can have several encodings:
01 D8is also valid forADD AX, BX. MASM picks 03 C3; both do the same thing. - Nothing in memory tells code from data: had IP reached 010Bh, the 00 there would have been decoded as an instruction.
A First Program
The emu8086 Emulator Window
- single step executes one instruction; changed registers and memory show up at once.
- step back undoes the last step, so you can trace a bug back to its cause.
- The highlighted line in the disassembly is the next instruction (CS:IP), not the one just executed.
- The disassembly shows constants in hex with 5 digits (00005h).
- Extra windows: screen output, variables, stack and flags.
The memory column shows physical addresses: 0700:0100 = 07100h. The drawing shows only the main parts of the window.
A First Program
Syntax Rules
25 or 25d decimal · 19h hex · 00011001b binary · 31o octal. All the same value: 25.0: 0FFh, 0B800h. Written as FFh, the assembler takes it for a name (label or variable).'A' → ASCII code 41h. MOV CL, 'A' and MOV CL, 41h produce the same machine code.-1 is stored as FFh in 8 bits and FFFFh in 16 bits (two’s complement).mov, MOV, Mov are the same; so are var1 and VAR1.; is a comment. Good habit: say “why”, not “what”.A First Program
Common Mistakes
| Wrong | Why? | Correct |
|---|---|---|
MOV AL, BX | Sizes differ (8 ↔ 16 bits) | MOV AL, BL or MOV AX, BX |
MOV DS, 1234h | No immediate into a segment register | MOV AX, 1234h · MOV DS, AX |
MOV AL, [AX] | Only BX, SI, DI, BP can address memory | MOV BX, AX · MOV AL, [BX] |
MOV AL, FFh | FFh is taken for a name | MOV AL, 0FFh |
MOV IP, 200h | IP cannot be written with MOV | use JMP |
MOV AL, 300 | 300 > 255, does not fit in 8 bits | MOV AX, 300 |
These rules come from the operand restrictions of MOV.
A First Program
More Syntax Errors
| Wrong | Why? | Correct |
|---|---|---|
MOV CS, AX | only JMP, CALL, RET, INT change CS | a far jump |
MOV ES, DS | no segment-to-segment MOV | MOV AX, DS · MOV ES, AX |
MOV [BX], [SI] | two memory operands | MOV AL, [SI] · MOV [BX], AL |
INC [BX] | size unknown | INC BYTE PTR [BX] |
MOV AL, [BX+BP] | two base registers together | [BX+SI] or [BP+DI] |
ADD 5, AX | destination cannot be immediate | ADD AX, 5 |
PUSH 5 | not before the 80186 | MOV AX, 5 · PUSH AX |
SHL AX, 2 | 8086 count is 1 or CL only | MOV CL, 2 · SHL AX, CL |
PUSH immediate and SHL AX, 2 are valid from the 80186 on. MASM rejects them when assembling for the 8086; emu8086 accepts them, but the code will not run on a real 8086.
A First Program
Assembly Error or Logic Error?
MOV AL, BX: size mismatchJMP strt: undefined labelMOV AL, 300: constant too large
- Forgetting RET: data executed as code
- Using
JAfor signed numbers - ADD instead of ADC: the carry is lost
- Not loading DS in an .EXE
Lab · 8086 instructions step by step
Lab: First Programs
Summary
Summary
[label:] mnemonic dest, source ; comment. Directives (ORG, DB, EQU) produce no machine code.ORG 100h; data goes after RET.Summary
Exercises
- With AX = 0ABCDh, what are AX, AL and BX after
MOV AH, 0? - With AX = 1234h and BL = 03h, what is AX after
MUL BL? What happens to the 12h in AH? - On an 8 MHz 8086, a loop runs 2000 times. How many clocks and milliseconds are saved by using
INC CX(2 clocks) instead ofINC count(21 clocks)? - With AL = 80h, give AL, CF, ZF, SF and OF after
ADD AL, 80h. - A program starts at 0100h. What is IP after four instructions of 3, 2, 1 and 4 bytes?
- Why are
MOV CX, ALandMOV ES, 0B800hwrong? Fix them.
Answers
- AX = 00CDh, AL = CDh; BX is unchanged.
- MUL BL multiplies AL (34h = 52) by BL: 52 × 3 = 156 = 009Ch. The product goes into all of AX; the 12h in AH is lost.
- 2000 × (21 − 2) = 38,000 clocks; one clock is 1/8 MHz = 0.125 µs → 4.75 ms.
- 80h + 80h = 100h → AL = 00h, CF = 1, ZF = 1, SF = 0, OF = 1 (−128 + −128 does not fit).
- 0100h + 10 = 010Ah.
- Size mismatch →
MOV CL, AL(orMOV CH, 0+MOV CL, AL); no immediate into a segment register →MOV AX, 0B800h·MOV ES, AX.
Summary
References
- emu8086 documentation, 8086 assembler tutorial for beginners (part 1: numbering systems, part 2: memory access).
- Intel, 8086 16-Bit HMOS Microprocessor data sheet.
- Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson.
- Muhammad Ali Mazidi, Janice G. Mazidi, The 80x86 IBM PC and Compatible Computers, Pearson.