8086 Instruction Set
Master the complete instruction set of the 8086 microprocessor with detailed explanations, examples, and practical programming techniques.
8086 Instruction Set Architecture - Comprehensive Overview
The Intel 8086 instruction set architecture (ISA) represents a comprehensive collection of over 100 instructions designed to provide complete computational capabilities. Understanding the instruction set is fundamental for assembly language programming and computer architecture comprehension.
Instruction Set Design Philosophy
The 8086 ISA was designed with several key principles:
- Orthogonality: Instructions can be used with multiple addressing modes
- Regularity: Consistent patterns in instruction format and operation
- Completeness: Full set of operations for practical programming
- Efficiency: Balance between instruction power and implementation complexity
- Compatibility: Foundation for future x86 processor evolution
Instruction Categories and Classification
The 8086 instructions are logically organized into eight primary categories based on their functional purpose:
Instruction Format Architecture
8086 instructions have a variable-length format ranging from 1 to 6 bytes, providing flexibility while maintaining efficiency:
Instruction Encoding and ModR/M Byte
The ModR/M byte is crucial for instruction encoding, containing three fields:
ModR/M Byte Structure (8 bits):
- Bits 7-6 (MOD): Addressing mode (00, 01, 10, 11)
- Bits 5-3 (REG): Register field (specifies register or opcode extension)
- Bits 2-0 (R/M): Register/Memory field (specifies operand)
Instruction Execution Time and Complexity
Different instruction categories have varying execution times and complexity:
- Simple Register Operations: 2-3 clock cycles
- Memory Access Operations: 8-17 clock cycles
- Multiplication/Division: 70-190 clock cycles
- String Operations: Variable depending on count
Data Transfer Instructions - Foundation of Programming
Data transfer instructions form the foundation of all programming operations by moving data between different storage locations without modifying the data content. These instructions are essential for loading operands, storing results, and managing data flow within programs.
Categories of Data Transfer Operations
The 8086 provides comprehensive data transfer capabilities through several instruction types:
MOV Instruction - The Universal Data Mover
The MOV instruction is the most fundamental and frequently used instruction in 8086 programming, capable of transferring data between various locations:
MOV Instruction Variants and Syntax:
| Syntax | Description | Example | Clock Cycles |
|---|
| MOV reg, reg | Register to register | MOV AX, BX | 2 |
| MOV reg, imm | Immediate to register | MOV AX, 1234H | 4 |
| MOV reg, mem | Memory to register | MOV AX, [2000H] | 8-12 |
| MOV mem, reg | Register to memory | MOV [2000H], AX | 9-13 |
| MOV mem, imm | Immediate to memory | MOV [2000H], 5678H | 10-14 |
| MOV seg, reg/mem | Load segment register | MOV DS, AX | 2-8 |
Detailed MOV Examples with Explanations:
; Basic register-to-register transfers
MOV AX, BX ; Copy contents of BX to AX (AX = BX)
MOV AL, BL ; Copy lower byte of BX to lower byte of AX
MOV AH, BH ; Copy upper byte of BX to upper byte of AX
; Immediate value loading
MOV AX, 1234H ; Load 16-bit immediate value 1234H into AX
MOV AL, 56H ; Load 8-bit immediate value 56H into AL
MOV CX, 0 ; Initialize CX register to zero
; Memory operations with direct addressing
MOV AX, [2000H] ; Load word from memory address 2000H into AX
MOV [3000H], BX ; Store BX contents to memory address 3000H
MOV AL, [1500H] ; Load byte from memory address 1500H into AL
; Memory operations with register indirect addressing
MOV AX, [BX] ; Load word from memory address pointed by BX
MOV [SI], DX ; Store DX to memory address pointed by SI
MOV AL, [DI] ; Load byte from memory address pointed by DI
; Segment register operations
MOV DS, AX ; Load data segment register with AX contents
MOV ES, BX ; Load extra segment register with BX contents
MOV AX, CS ; Copy code segment register to AX
; Note: Cannot load CS directly with MOV
; Common programming patterns
MOV AX, 0 ; Clear accumulator (alternative to XOR AX, AX)
MOV SP, 1000H ; Initialize stack pointer
MOV BP, SP ; Set up base pointer for stack frame
XCHG Instruction - Efficient Data Exchange
The XCHG instruction provides atomic data exchange between two operands, eliminating the need for temporary storage:
XCHG Instruction Formats:
XCHG AX, BX ; Exchange contents of AX and BX
XCHG AL, BL ; Exchange lower bytes of AX and BX
XCHG AX, [2000H] ; Exchange AX with memory word at 2000H
XCHG BL, [SI] ; Exchange BL with memory byte pointed by SI
; Equivalent operation without XCHG (requires temporary storage):
; To exchange AX and BX without XCHG:
; MOV temp, AX ; temp = AX (but no temp register available!)
; MOV AX, BX ; AX = BX
; MOV BX, temp ; BX = temp
; Using stack as temporary storage:
PUSH AX ; Save AX on stack
MOV AX, BX ; AX = BX
POP BX ; BX = original AX (completes exchange)
Stack Operations - PUSH and POP
Stack instructions provide Last-In-First-Out (LIFO) data storage mechanism essential for subroutine calls and temporary data storage:
Stack Operation Characteristics:
- Stack Growth: Downward (decreasing memory addresses)
- Stack Pointer (SP): Always points to top of stack
- Data Size: Always 16-bit words (2 bytes)
- Automatic SP Update: SP decrements on PUSH, increments on POP
PUSH and POP Examples:
; PUSH operations (SP decrements by 2)
PUSH AX ; Push AX contents onto stack
PUSH BX ; Push BX contents onto stack
PUSH [2000H] ; Push memory word at 2000H onto stack
PUSH CS ; Push code segment register onto stack
; POP operations (SP increments by 2)
POP BX ; Pop top of stack into BX
POP AX ; Pop top of stack into AX
POP [3000H] ; Pop top of stack to memory location 3000H
POP DS ; Pop top of stack into data segment register
; Stack usage example - saving and restoring registers
PUSH AX ; Save AX
PUSH BX ; Save BX
PUSH CX ; Save CX
; ... perform operations that modify AX, BX, CX ...
POP CX ; Restore CX (last saved, first restored)
POP BX ; Restore BX
POP AX ; Restore AX (first saved, last restored)
XCHG Instruction
Exchanges contents of two operands.
XCHG AX, BX ; Exchange AX and BX contents
XCHG AL, DL ; Exchange AL and DL
XCHG AX, [SI] ; Exchange AX with memory contents
LEA Instruction
Load Effective Address - calculates and loads the address rather than the data.
LEA BX, [SI+DI+10H] ; BX = SI + DI + 10H (address calculation)
LEA SI, [BX+100H] ; SI = BX + 100H
; Note: LEA doesn't access memory, only calculates address
Stack Operations
PUSH AX ; Push AX onto stack, SP = SP - 2
POP BX ; Pop top of stack into BX, SP = SP + 2
PUSHF ; Push flags register onto stack
POPF ; Pop flags from stack
I/O Instructions
IN AL, 60H ; Read byte from port 60H
OUT 61H, AL ; Write AL to port 61H
IN AX, DX ; Read word from port specified by DX
OUT DX, AX ; Write AX to port specified by DX
2. Arithmetic Instructions
These instructions perform mathematical operations and affect the processor flags.
Basic Arithmetic
ADD AX, BX ; AX = AX + BX
SUB AX, 100H ; AX = AX - 100H
INC CX ; CX = CX + 1
DEC DX ; DX = DX - 1
NEG AX ; AX = -AX (two's complement)
Multiplication Instructions
; Unsigned multiplication
MUL BL ; AX = AL × BL (8-bit)
MUL BX ; DX:AX = AX × BX (16-bit)
; Signed multiplication
IMUL BL ; AX = AL × BL (signed 8-bit)
IMUL BX ; DX:AX = AX × BX (signed 16-bit)
Division Instructions
; Unsigned division
DIV BL ; AL = AX ÷ BL, AH = remainder
DIV BX ; AX = DX:AX ÷ BX, DX = remainder
; Signed division
IDIV BL ; AL = AX ÷ BL (signed), AH = remainder
IDIV BX ; AX = DX:AX ÷ BX (signed), DX = remainder
Carry Operations
ADC AX, BX ; AX = AX + BX + Carry Flag
SBB AX, CX ; AX = AX - CX - Carry Flag
; Useful for multi-precision arithmetic
Compare Instruction
CMP AX, BX ; Compare AX with BX (sets flags)
CMP AL, 50H ; Compare AL with immediate value
CMP [SI], CX ; Compare memory contents with CX
; CMP performs subtraction but doesn't store result
3. Logic Instructions
Logic instructions perform bitwise operations and are essential for bit manipulation and testing.
Bitwise Operations
AND AX, BX ; AX = AX AND BX (bitwise)
OR AX, 0F0H ; AX = AX OR 0F0H
XOR AX, AX ; Clear AX (AX = 0)
NOT AX ; AX = bitwise complement of AX
TEST Instruction
TEST AX, 0001H ; Test if bit 0 is set (like AND but no result stored)
TEST AL, AL ; Test if AL is zero
TEST BX, 8000H ; Test if bit 15 is set
Practical Examples
; Set bit 3 of AL
OR AL, 08H ; AL = AL OR 00001000B
; Clear bit 5 of AX
AND AX, 0FFDFH ; AX = AX AND 1111111111011111B
; Toggle bit 7 of BL
XOR BL, 80H ; BL = BL XOR 10000000B
; Check if number is even (bit 0 = 0)
TEST AX, 1 ; If ZF=1, number is even
4. Shift and Rotate Instructions
These instructions shift or rotate bits within a register or memory location.
Logical Shift Instructions
SHL AX, 1 ; Shift AX left by 1 bit (multiply by 2)
SHR BX, 1 ; Shift BX right by 1 bit (divide by 2)
SHL CX, CL ; Shift CX left by CL bits
SHR DX, CL ; Shift DX right by CL bits
Arithmetic Shift Instructions
SAL AX, 1 ; Arithmetic shift left (same as SHL)
SAR BX, 1 ; Arithmetic shift right (preserves sign bit)
SAR CX, CL ; Shift CX right by CL bits (signed)
Rotate Instructions
ROL AL, 1 ; Rotate AL left by 1 bit
ROR BL, 1 ; Rotate BL right by 1 bit
RCL AX, 1 ; Rotate AX left through carry
RCR BX, 1 ; Rotate BX right through carry
Application Examples
; Multiply by 8 (fast multiplication)
MOV AX, 25 ; AX = 25
SHL AX, 3 ; AX = 25 × 8 = 200
; Divide by 4 (fast division)
MOV BX, 100 ; BX = 100
SHR BX, 2 ; BX = 100 ÷ 4 = 25
; Extract upper nibble
MOV AL, 0ABH ; AL = 10101011B
SHR AL, 4 ; AL = 00001010B = 0AH
5. String Instructions
String instructions operate on sequences of bytes or words, providing efficient data processing capabilities.
String Movement
MOVSB ; Move byte from [SI] to [DI], increment SI,DI
MOVSW ; Move word from [SI] to [DI], increment SI,DI by 2
REP MOVSB ; Repeat MOVSB CX times
CLD ; Clear direction flag (forward)
STD ; Set direction flag (backward)
String Comparison
CMPSB ; Compare byte at [SI] with [DI]
CMPSW ; Compare word at [SI] with [DI]
REPE CMPSB ; Repeat while equal and CX > 0
REPNE CMPSB ; Repeat while not equal and CX > 0
String Scanning
SCASB ; Compare AL with [DI]
SCASW ; Compare AX with [DI]
REPNE SCASB ; Scan for byte equal to AL
REPE SCASW ; Scan while words equal to AX
Practical String Example
; Copy 100 bytes from source to destination
MOV SI, 2000H ; Source address
MOV DI, 3000H ; Destination address
MOV CX, 100 ; Count
CLD ; Forward direction
REP MOVSB ; Copy string
; Find character in string
MOV DI, 4000H ; String address
MOV AL, 'A' ; Character to find
MOV CX, 50 ; Maximum length
CLD ; Forward direction
REPNE SCASB ; Scan for character
; If found: ZF=1, DI points to character+1
6. Control Transfer Instructions
These instructions control program flow by changing the sequence of instruction execution.
Unconditional Jumps
JMP SHORT LABEL1 ; Short jump (-128 to +127 bytes)
JMP NEAR LABEL2 ; Near jump (within same segment)
JMP FAR LABEL3 ; Far jump (different segment)
JMP BX ; Indirect jump through register
JMP [BX] ; Indirect jump through memory
Conditional Jumps
; Based on comparison results
JE/JZ LABEL ; Jump if equal/zero
JNE/JNZ LABEL ; Jump if not equal/not zero
JG/JNLE LABEL ; Jump if greater (signed)
JL/JNGE LABEL ; Jump if less (signed)
JA/JNBE LABEL ; Jump if above (unsigned)
JB/JNAE LABEL ; Jump if below (unsigned)
; Based on individual flags
JC LABEL ; Jump if carry
JNC LABEL ; Jump if no carry
JS LABEL ; Jump if sign (negative)
JNS LABEL ; Jump if no sign (positive)
JO LABEL ; Jump if overflow
JNO LABEL ; Jump if no overflow
JP LABEL ; Jump if parity even
JNP LABEL ; Jump if parity odd
Loop Instructions
LOOP LABEL ; Decrement CX, jump if CX ≠ 0
LOOPE LABEL ; Decrement CX, jump if CX ≠ 0 and ZF = 1
LOOPNE LABEL ; Decrement CX, jump if CX ≠ 0 and ZF = 0
JCXZ LABEL ; Jump if CX = 0
Procedure Instructions
CALL PROCEDURE ; Call near procedure
CALL FAR PTR PROC; Call far procedure
RET ; Return from near procedure
RET 4 ; Return and adjust stack by 4 bytes
RETF ; Return from far procedure
7. Flag Manipulation Instructions
These instructions directly control the processor flags.
STC ; Set carry flag (CF = 1)
CLC ; Clear carry flag (CF = 0)
CMC ; Complement carry flag
STD ; Set direction flag (DF = 1)
CLD ; Clear direction flag (DF = 0)
STI ; Set interrupt flag (IF = 1)
CLI ; Clear interrupt flag (IF = 0)
SAHF ; Store AH into flags
LAHF ; Load flags into AH
8. Processor Control Instructions
These instructions control processor operation and handle special situations.
NOP ; No operation (do nothing)
HLT ; Halt processor until interrupt
WAIT ; Wait for signal on TEST pin
ESC ; Escape to external processor
LOCK ; Lock bus during next instruction
Numerical Problems and Examples
Problem 1: Multi-byte Addition
Question: Add two 32-bit numbers: 12345678H + 9ABCDEF0H
Solution:
; Number 1: 12345678H (stored as 5678H, 1234H)
; Number 2: 9ABCDEF0H (stored as DEF0H, 9ABCH)
MOV AX, 5678H ; Load lower word of first number
MOV BX, 1234H ; Load upper word of first number
MOV CX, 0DEF0H ; Load lower word of second number
MOV DX, 9ABCH ; Load upper word of second number
ADD AX, CX ; Add lower words
ADC BX, DX ; Add upper words with carry
; Result: BX:AX = AD02A568H
; Verification: 12345678H + 9ABCDEF0H = AD02A568H
Problem 2: Finding Maximum in Array
Question: Find the maximum value in an array of 10 words starting at address 2000H.
Solution:
MOV SI, 2000H ; Point to array start
MOV AX, [SI] ; Load first element as initial max
MOV CX, 9 ; Remaining elements to check
ADD SI, 2 ; Point to second element
FIND_MAX:
CMP AX, [SI] ; Compare current max with array element
JGE SKIP ; Jump if current max >= array element
MOV AX, [SI] ; Update max if array element is greater
SKIP:
ADD SI, 2 ; Point to next element
LOOP FIND_MAX
; Result: AX contains the maximum value
Problem 3: Binary to BCD Conversion
Question: Convert binary number in AX to BCD format.
Solution (for numbers 0-99):
; Convert binary number in AL to packed BCD
MOV AL, 47 ; Example: 47 decimal
MOV AH, 0 ; Clear upper byte
MOV BL, 10 ; Divisor
DIV BL ; AL = quotient (4), AH = remainder (7)
MOV CL, 4 ; Shift count
SHL AL, CL ; Move tens digit to upper nibble
OR AL, AH ; Combine with units digit
; Result: AL = 47H (packed BCD)
; 47 decimal = 01000111B binary = 47H BCD
Problem 4: String Length Calculation
Question: Calculate the length of a null-terminated string starting at address 3000H.
Solution:
MOV DI, 3000H ; Point to string start
MOV AL, 0 ; Search for null terminator
MOV CX, 0FFFFH ; Maximum possible length
CLD ; Forward direction
REPNE SCASB ; Scan for null character
; Calculate length
MOV AX, 0FFFFH ; Original CX value
SUB AX, CX ; Subtract remaining count
DEC AX ; Subtract 1 (don't count null terminator)
; Result: AX contains string length
Problem 5: Factorial Calculation
Question: Calculate factorial of number in AL (AL ≤ 8).
Solution:
MOV AL, 5 ; Calculate 5!
MOV BL, AL ; Copy number to BL
MOV AX, 1 ; Initialize result
CMP BL, 0 ; Check if number is 0
JZ DONE ; 0! = 1
FACTORIAL_LOOP:
MUL BL ; AX = AX × BL
DEC BL ; Decrement counter
JNZ FACTORIAL_LOOP
DONE:
; Result: AX = 120 (5! = 5×4×3×2×1 = 120)
Instruction Timing and Optimization
Understanding instruction timing helps in writing efficient 8086 assembly code.
Timing Examples (in clock cycles):
- MOV reg, reg: 2 clocks
- MOV reg, mem: 8-12 clocks
- ADD reg, reg: 3 clocks
- MUL reg8: 70-77 clocks
- DIV reg8: 80-90 clocks
- REP MOVSB: 9+17×CX clocks
Optimization Techniques:
; Instead of:
MOV AX, 0 ; 4 bytes
; Use:
XOR AX, AX ; 2 bytes, same result
; Instead of:
MUL 2 ; Slow multiplication
; Use:
SHL AX, 1 ; Fast bit shift
; Instead of:
MOV [SI], AL
INC SI
; Use:
STOSB ; String instruction (if DI is used)
Instruction Set Summary
The 8086 instruction set provides a rich foundation for assembly language programming.
Programming Guidelines:
- Choose appropriate addressing modes for different data types
- Use string instructions for bulk data operations
- Minimize memory accesses for better performance
- Leverage bit manipulation instructions for efficient coding
- Understand flag effects for conditional operations
- Use stack operations for temporary storage and procedure calls