Chapter 3.2: Assembly Language

Microprocessors
Assembly languageSource line formatInstructions and directivesOperand typesImplicit operandsInstruction timingIP and flagsA first program
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Where assembly fits

Relate assembly language to machine language and to high-level languages.

Line format

Read a source line as label, mnemonic, operands and comment; tell an instruction from a directive.

Operands

Recognize register, immediate and memory operands, and the implicit operands of instructions.

Instruction timing

Compare register and memory forms of an instruction in clock cycles.

IP and flags

Follow how IP advances and which instructions change the flags.

A first program

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.
One line, three views

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

source.asmtextAssemblerone-to-oneobject / .COMmachine codeLoaderloads into RAMFor .EXE files a linker sits in between: .obj → .exe
AssemblerTranslates mnemonics and labels into machine code and variable names into addresses. Examples: MASM, TASM, NASM.
emu8086Editor + assembler + 8086 emulator. Shows registers, memory and flags step by step.
.COM / .EXE.COM is the simple single-segment format, at most 64 KB (ORG 100h). .EXE may use several segments.

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.
In practice a mixed approach is common: the program is written in a high-level language and only small, time-critical parts in assembly.

Assembly Language

Source Line Format

[label:]  mnemonic  [dest], [source]  [; comment]
start:labelMOVmnemonicAXdest,5source; AX ← 5comment→ AX ← 5
LineLabelMnemonicOperandsComment
start: MOV AX, 5 ; AX ← 5startMOVAX, 5; AX ← 5
INC AX—INCAX—
RET—RETnone—
  • With two operands the order is destination, source: MOV AX, BX → AX ← BX.
  • The assembler is case-insensitive: mov ax, bx and MOV AX, BX are the same.
  • Everything from ; to the end of the line is a comment and produces no machine code.

Assembly Language

Instruction or Directive?

LineKindWhat it puts in memory
MOV AX, 5instructionmachine code: B8 05 00
ORG 100hdirectivenothing; sets the location counter to 0100h
num DB 0directive (data definition)1 byte of data: 00
k EQU 5directivenothing; the name k stands for 5
ENDdirectivenothing; the source ends here
PUTC 'A'macro callcode for the instructions in the macro body
InstructionAn operation the processor carries out at run time. Each one becomes a machine instruction.
DirectiveInformation for the assembler: where code goes, how much space to reserve, what names stand for.
MacroA named text pattern replaced by several lines at assembly time.

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: again and AGAIN are the same label; defining both gives a “duplicate definition” error.
Name_countsum22sumcxlast-val
Valid?yesyesstarts with a digitregister name− is an operator

Working with Registers

Operand Types and Implicit Operands

RegisterMOV AX, BX: the value comes from inside the processor; the fastest kind.
ImmediateMOV AX, 5: the value is part of the instruction bytes; it can only be a source.
MemoryMOV AX, [BX]: the value is read from memory in a bus cycle.
InstructionExplicit operandImplicit operand(s)
MUL BLBLAL (multiplicand), AX (product)
DIV BLBLAX (dividend), AL (quotient), AH (remainder)
LOOP againagainCX (counter)
CBW—AL → AX
PUSH AXAXSS: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
AX000ChBX000AhCX0000hCF0ZF0AX = 12 (dec)

AL = 12, BL = 10. AH starts at 0.

AX0078hBX000AhCX0000hCF0ZF?AX = 120 (dec)implicit: AL, AX

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.

AX001EhBX000AhCX0002hCF0ZF0AX = 30 (dec)count in CL

SHR AX, CL: a shift count above 1 must be in CL. 120 ÷ 4 = 30 → AX = 001Eh.

AX001EhBX000AhCX0003hCF0ZF0AX = 30 (dec)

CX = 3: the implicit counter of LOOP. CL is part of CX, so the old count 2 is gone.

AX001FhBX000AhCX0002hCF0ZF0AX = 31 (dec)

Pass 1: AX + 1 = 31; LOOP decrements CX: CX = 2, not zero → again.

AX0020hBX000AhCX0001hCF0ZF0AX = 32 (dec)

Pass 2: AX + 1 = 32; LOOP decrements CX: CX = 1, not zero → again.

AX0021hBX000AhCX0000hCF0ZF0AX = 33 (dec)

Pass 3: AX + 1 = 33; LOOP decrements CX: CX = 0, the loop ends.

AX0021hBX000AhCX0000hCF0ZF0AX = 33 (dec)

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?

Register
  • Inside the CPU; no bus cycle needed.
  • Very few of them (the 8086 has 14 16-bit registers).
  • Ideal for intermediate results and counters.
Memory
  • 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.
CPUAXBXCXDX0 bus cyclessystem buseach access ≥ 4 clock periodsMemory (1 MB)code · variablesarrays · stack
Good assembly code keeps frequently used values in registers and goes to memory only when it must. Keeping a loop counter in 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, BX2
MOV AX, 54
MOV AX, num10
MOV CX, [BX]8 + 5 = 13
ADD AX, BX3
ADD AX, num9 + 6 = 15
INC CX2
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 count reads, 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.
These are best-case times from the Intel 8086 data sheet; word access at an odd address and a flushed prefetch queue make them longer. The ratio holds: a memory-operand instruction is several times slower than its register form.

IP and Flags · Step through the code

Code Trace: How Does IP Advance?

B8 05 000100MOVIP = 0100hBB 02 000103MOV400106INC03 C30107ADDC30109RET

The program is loaded at CS:0100h; IP = 0100h points to the first instruction.

B8 05 000100MOVBB 02 000103MOVIP = 0103h400106INC03 C30107ADDC30109RET

MOV AX, 5 (B8 05 00) is fetched: IP = 0103h; once executed, AX = 0005h.

B8 05 000100MOVBB 02 000103MOV400106INCIP = 0106h03 C30107ADDC30109RET

Three more bytes: IP = 0106h, BX = 0002h.

B8 05 000100MOVBB 02 000103MOV400106INC03 C30107ADDIP = 0107hC30109RET

INC AX is a single byte (40h): IP = 0107h, AX = 0006h.

B8 05 000100MOVBB 02 000103MOV400106INC03 C30107ADDC30109RETIP = 0109h

ADD AX, BX is 2 bytes: IP = 0109h, AX = 0008h.

B8 05 000100MOVBB 02 000103MOV400106INC03 C30107ADDC30109RETIP = 0000h (back to PSP)

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
AL00hCF1ZF0SF0AF0PF0CF only

STC: CF = 1. It sets a flag directly.

ALFFhCF1ZF0SF0AF0PF0no flags

MOV AL, 0FFh: AL changed, none of the flags did. MOV does not affect flags.

AL00hCF1ZF1SF0AF1PF1CF kept

INC AL: FFh + 1 = 00h → ZF = 1, AF = 1, PF = 1. CF is still 1: INC and DEC never touch the carry flag.

AL00hCF1ZF1SF0AF1PF1no flags

MOV CX, 0: the result is zero, but ZF is not recomputed; it keeps the value from the previous instruction.

ALFFhCF1ZF0SF1AF1PF1CF kept

DEC AL: 00h − 1 = FFh → SF = 1, ZF = 0. CF is still 1: DEC does not record the borrow in CF.

AL00hCF1ZF1SF0AF1PF1all updated

ADD AL, 1: FFh + 1 = 100h → AL = 00h. ADD rewrites every status flag: this time CF = 1 is ADD’s own carry.

AL00hCF0ZF1SF0AF1PF1CF only

CLC: CF = 0.

ALFFhCF0ZF1SF0AF1PF1no flags

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.
  • RET returns 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.
In emu8086, new → COM template opens the template; emulate loads the program into the emulator, and single step shows how the registers change after each instruction.

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
segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 0100hSP = FFFEhCS:IPSS:SPAX0000hBX0000hCL00h

The program is loaded at 0700:0100. CS = DS = ES = SS = 0700h, IP = 0100h, SP = FFFEh; the top of the stack holds 0000h.

segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 0103hSP = FFFEhCS:IPSS:SPAX0005hBX0000hCL00h

MOV AX, 5 (3 bytes): AX = 0005h, IP = 0103h.

segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 0106hSP = FFFEhCS:IPSS:SPAX0005hBX0003hCL00h

MOV BX, 3 (3 bytes): BX = 0003h, IP = 0106h.

segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 0108hSP = FFFEhCS:IPSS:SPAX0008hBX0003hCL00h

ADD AX, BX (2 bytes): AX = 0008h, IP = 0108h.

segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 010AhSP = FFFEhCS:IPSS:SPAX0008hBX0003hCL08h

MOV CL, AL (2 bytes): CL = 08h, IP = 010Ah.

segment 0700h0000hPSP (256 bytes)0100hMOV AX, 50103hMOV BX, 30106hADD AX, BX0108hMOV CL, AL010AhRET010Bhnum = 00⋮FFFEhstack: 0000hIP = 0000hSP = 0000hCS:IPAX0008hBX0003hCL08h

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

OffsetBytesSource lineNote
0100hB8 05 00MOV AX, 5B8 + register number (AX = 0); immediate little-endian
0103hBB 03 00MOV BX, 3B8 + 3 (BX)
0106h03 C3ADD AX, BXC3 = 11 000 011: register–register, AX, BX
0108h8A C8MOV CL, ALC8 = 11 001 000: CL, AL
010AhC3RETone-byte instruction
010Bh00num DB 0data, not an instruction
  • The .COM file is exactly these 12 bytes: there is no header; DOS copies them into memory starting at 0100h.
  • ORG 100h produces no bytes; it only sets where the first byte goes.
  • One instruction can have several encodings: 01 D8 is also valid for ADD 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

Loadreloadstep backsingle steprunregisters (H / L)AX00 05BX00 00CX00 00IP0103SPFFFEmemory07100B80710105071020007103BB0710403disassemblyMOV AX, 00005hMOV BX, 00003hADD AX, BXMOV CL, ALRETscreenvarsstackflags
  • 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

Number bases 25 or 25d decimal · 19h hex · 00011001b binary · 31o octal. All the same value: 25.
Hex starting with a letter Prefix a 0: 0FFh, 0B800h. Written as FFh, the assembler takes it for a name (label or variable).
Characters 'A' → ASCII code 41h. MOV CL, 'A' and MOV CL, 41h produce the same machine code.
Signed numbers -1 is stored as FFh in 8 bits and FFFFh in 16 bits (two’s complement).
Case The assembler ignores case: mov, MOV, Mov are the same; so are var1 and VAR1.
Comments Anything after ; is a comment. Good habit: say “why”, not “what”.

A First Program

Common Mistakes

WrongWhy?Correct
MOV AL, BXSizes differ (8 ↔ 16 bits)MOV AL, BL or MOV AX, BX
MOV DS, 1234hNo immediate into a segment registerMOV AX, 1234h · MOV DS, AX
MOV AL, [AX]Only BX, SI, DI, BP can address memoryMOV BX, AX · MOV AL, [BX]
MOV AL, FFhFFh is taken for a nameMOV AL, 0FFh
MOV IP, 200hIP cannot be written with MOVuse JMP
MOV AL, 300300 > 255, does not fit in 8 bitsMOV AX, 300

These rules come from the operand restrictions of MOV.

A First Program

More Syntax Errors

WrongWhy?Correct
MOV CS, AXonly JMP, CALL, RET, INT change CSa far jump
MOV ES, DSno segment-to-segment MOVMOV AX, DS · MOV ES, AX
MOV [BX], [SI]two memory operandsMOV AL, [SI] · MOV [BX], AL
INC [BX]size unknownINC BYTE PTR [BX]
MOV AL, [BX+BP]two base registers together[BX+SI] or [BP+DI]
ADD 5, AXdestination cannot be immediateADD AX, 5
PUSH 5not before the 80186MOV AX, 5 · PUSH AX
SHL AX, 28086 count is 1 or CL onlyMOV 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?

Assembly (syntax) errorThe assembler finds it and reports the line number; no program is produced.
  • MOV AL, BX: size mismatch
  • JMP strt: undefined label
  • MOV AL, 300: constant too large
Logic errorThe program assembles and runs, but the result is wrong; the assembler cannot tell.
  • Forgetting RET: data executed as code
  • Using JA for signed numbers
  • ADD instead of ADC: the carry is lost
  • Not loading DS in an .EXE
Logic errors are found by single-stepping: after each instruction, compare the expected register and flag values with the emulator’s. The first instruction where they differ is where the bug is.

Lab · 8086 instructions step by step

Lab: First Programs

Summary

Summary

AssemblyA low-level language that maps one-to-one onto machine code: small, fast code and direct hardware access, but not portable.
Line format[label:] mnemonic dest, source ; comment. Directives (ORG, DB, EQU) produce no machine code.
OperandsRegisters are fastest; immediates can only be sources; memory operands need a bus cycle. MUL, LOOP and CBW use implicit operands.
First programA .COM program starts at 0100h with ORG 100h; data goes after RET.
IPCS:IP points to the next instruction; IP advances by the instruction’s length.
FlagsMOV, NOT and XCHG leave the flags alone; INC and DEC do not touch CF; ADD rewrites them all.

Summary

Exercises

  1. With AX = 0ABCDh, what are AX, AL and BX after MOV AH, 0?
  2. With AX = 1234h and BL = 03h, what is AX after MUL BL? What happens to the 12h in AH?
  3. On an 8 MHz 8086, a loop runs 2000 times. How many clocks and milliseconds are saved by using INC CX (2 clocks) instead of INC count (21 clocks)?
  4. With AL = 80h, give AL, CF, ZF, SF and OF after ADD AL, 80h.
  5. A program starts at 0100h. What is IP after four instructions of 3, 2, 1 and 4 bytes?
  6. Why are MOV CX, AL and MOV ES, 0B800h wrong? Fix them.
Answers
  1. AX = 00CDh, AL = CDh; BX is unchanged.
  2. 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.
  3. 2000 × (21 − 2) = 38,000 clocks; one clock is 1/8 MHz = 0.125 µs → 4.75 ms.
  4. 80h + 80h = 100h → AL = 00h, CF = 1, ZF = 1, SF = 0, OF = 1 (−128 + −128 does not fit).
  5. 0100h + 10 = 010Ah.
  6. Size mismatch → MOV CL, AL (or MOV 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.

The End

Chapter 3.2: Assembly Language
Previous: Chapter 3.1: Programming Languages · Next: Chapter 4.1: Memory Access
1 / 1 Sercan KÜLCÜ. All rights reserved.