Instruction Formats & Addressing Modes
0 to 4-address instruction formats, Immediate, Direct, Indirect, Register, Relative, and Base-Index addressing modes
- Compare 3-address, 2-address, 1-address (Accumulator), and 0-address (Stack) formats
- Calculate Effective Addresses across all basic and complex addressing modes
- Select optimal addressing modes for array indexing, code relocation, and subroutines
๐ฏ Weightage: 15.5% โ High-yield topic; expression-conversion and effective-address numericals are exam staples.
1 Instruction Structure
1.1 The Five Instruction Formats
An instruction generally has the form OPCODE + OPERAND(S). Based on how many operand addresses are explicitly specified, instructions are classified into 5 formats:
| Format | Operands Specified | General Form |
|---|---|---|
| ๐ฅ 4-Address | 3 source/destination + 1 next-instruction address | OP A1, A2, A3, A4 |
| ๐ง 3-Address | 2 sources + 1 destination | OP D, S1, S2 |
| ๐จ 2-Address | 1 source + 1 source/destination (shared) | OP D, S |
| ๐ฉ 1-Address (Accumulator) | 1 operand (uses implicit Accumulator as the other) | OP A |
| ๐ฆ 0-Address (Stack-based) | No explicit operand (uses stack top implicitly) | OP |
1.2 Worked Example: Evaluate `(A + B) (C D)` in ALL Five Formats
โ 4-Address Format
Each instruction specifies 2 source operands, 1 destination, and 1 address of the NEXT instruction to execute (rarely used in modern systems โ mainly historical/theoretical).
1ADD R1, A, B, I2 ; R1 = A + B ; next โ I22SUB R2, C, D, I3 ; R2 = C โ D ; next โ I33MUL R3, R1, R2, I4 ; R3 = R1 ร R2 ; next โ I4โ 3-Address Format
1ADD R1, A, B ; R1 = A + B2SUB R2, C, D ; R2 = C โ D3MUL R3, R1, R2 ; R3 = R1 ร R2โ 2-Address Format
1MOV R1, A ; R1 = A2ADD R1, B ; R1 = R1 + B (= A + B)3MOV R2, C ; R2 = C4SUB R2, D ; R2 = R2 โ D (= C โ D)5MUL R1, R2 ; R1 = R1 ร R2โ 1-Address (Accumulator-Based) Format
Every instruction implicitly uses the Accumulator (AC) as one operand and the destination.
1LOAD A ; AC = A2ADD B ; AC = AC + B (= A + B)3STORE T ; T = AC (store partial result)4LOAD C ; AC = C5SUB D ; AC = AC โ D (= C โ D)6MUL T ; AC = AC ร Tโ 0-Address (Stack-Based) Format
Operands are implicitly the top elements of the stack; requires converting the expression to Postfix (Reverse Polish) Notation first: A B + C D โ ร
1PUSH A2PUSH B3ADD ; pushes (A+B) onto stack4PUSH C5PUSH D6SUB ; pushes (CโD) onto stack7MUL ; pops top 2, pushes (A+B)ร(CโD)1.3 Advantages / Disadvantages of Each Format
| Format | โ Advantages | โ Disadvantages |
|---|---|---|
| 4-Address | Maximum flexibility, explicit control flow | Very long instructions, wastes memory/bandwidth, rarely used practically |
| 3-Address | Fewer total instructions needed, direct expression mapping | Longer instruction word (needs 3 operand fields) |
| 2-Address | Shorter than 3-address, still fairly flexible | Overwrites one source operand (destructive), may need extra MOV instructions |
| 1-Address | Very short instructions (only 1 operand field) | Heavy reliance on Accumulator causes more memory traffic (LOAD/STORE overhead) |
| 0-Address | Shortest instructions (no operand field at all โ just opcode) | Requires expression in postfix form; heavy stack push/pop traffic |
Solved Numericals
Q1. Convert X = (A โ B) / (C + D ร E) into 3-address code.
Solution
: `` SUB R1, A, B MUL R2, D, E ADD R3, C, R2 DIV X, R1, R3 ``
Q2. Convert X = (A + B) ร (C + D) into 2-address code.
Solution
: `` MOV R1, A ADD R1, B MOV R2, C ADD R2, D MUL R1, R2 MOV X, R1 ``
Q3. Convert X = A ร B + C ร D into 1-address (accumulator) code.
Solution
: `` LOAD A MUL B STORE T LOAD C MUL D ADD T STORE X ``
Q4. Convert X = (A + B) ร (C โ D) into 0-address (postfix stack) code โ verify postfix conversion.
Solution
: Infix โ Postfix: (A + B) ร (C โ D) โ A B + C D โ ร `` PUSH A PUSH B ADD PUSH C PUSH D SUB MUL ``
Q5. How many instructions are needed in 3-address format to evaluate X = A + B ร C โ D / E (assuming standard precedence)?
Solution
: Expression = A + (BรC) โ (D/E) `` MUL R1, B, C DIV R2, D, E ADD R3, A, R1 SUB X, R3, R2 `` 4 instructions needed.
### โ ๏ธ Common Traps & Exam Tips - ๐ 2-address format is "destructive" โ the destination operand OVERWRITES one of the sources; always track which operand survives. - ๐ 0-address format REQUIRES postfix (RPN) conversion first โ a very common exam step students skip, leading to wrong instruction sequences. - ๐ 4-address format is mostly theoretical/historical โ rarely implemented in real CPUs (mentioned mainly for conceptual completeness/comparison in exams). - ๐ Fewer address fields โ shorter instructions but MORE total instructions needed (and vice versa) โ a fundamental trade-off tested conceptually.
2 Basic Addressing Modes
2.1 Definitions, Examples & Effective Address Calculation
โ Immediate Addressing Mode
- The operand itself is specified directly within the instruction (no memory access needed for the operand).
- Example:
MOV R1, #5โ R1 = 5 - Effective Address: N/A (operand IS the data, not an address)
โ Absolute / Direct Addressing Mode
- The instruction contains the actual memory address of the operand directly.
- Example:
MOV R1, 200โ R1 = Memory[200] - Effective Address (EA):
EA = Address field value(used as-is)
โ Indirect Addressing Mode
- The instruction contains the address of a memory location that itself holds the address of the actual operand (one extra level of memory reference).
- Example:
MOV R1, @200โ R1 = Memory[Memory[200]] - Effective Address (EA):
EA = Memory[Address field value]
โ Implied (Implicit) Addressing Mode
- The operand location is implicit in the opcode itself โ no address field needed.
- Example:
CLC(Clear Carry flag) โ operand is the Carry flag, implied by the instruction itself. - Effective Address: N/A (location is fixed/implicit by definition of the instruction)
โ Register Addressing Mode
- The operand is located in a CPU register, specified directly in the instruction.
- Example:
MOV R1, R2โ R1 = R2 (contents of register R2) - Effective Address: N/A (register itself holds operand; no memory access needed)
โ Register Indirect Addressing Mode
- A register holds the memory address of the actual operand (register acts as a pointer).
- Example:
MOV R1, (R2)โ R1 = Memory[R2] - Effective Address (EA):
EA = Contents of Register
2.2 Advantages / Disadvantages
| Mode | โ Advantages | โ Disadvantages |
|---|---|---|
| Immediate | Fastest (no memory access for operand) | Limited by instruction word size (small constants only) |
| Direct/Absolute | Simple, single memory access | Limited address range (fits in instruction), no dynamic addressing |
| Indirect | Large address range achievable, supports pointers | Slower (2 memory accesses needed) |
| Implied | Shortest instruction (no address field at all) | Inflexible, fixed operand only |
| Register | Very fast (no memory access), short instruction | Limited by small number of CPU registers available |
| Register Indirect | Combines register speed with memory flexibility (pointer-like) | Still needs 1 memory access to fetch actual operand |
2.3 Comparison Table Basic Modes
| Mode | Definition | Effective Address Formula | Example | Use Case |
|---|---|---|---|---|
| Immediate | Operand embedded in instruction | N/A (operand = value itself) | MOV R1, #5 | Loading constants |
| Direct/Absolute | Address field = actual operand address | EA = Address Field | MOV R1, 200 | Simple, fixed-location variables |
| Indirect | Address field points to a location holding the real address | EA = Memory[Address Field] | MOV R1, @200 | Pointers, dynamic data structures |
| Implied | Operand location fixed by the opcode itself | N/A | CLC | Flag/accumulator operations |
| Register | Operand is inside a register | N/A (register = operand) | MOV R1, R2 | Fast temporary computations |
| Register Indirect | Register holds address of the operand | EA = Contents of Register | MOV R1, (R2) | Pointer-based access, linked structures |
### โ ๏ธ Common Traps & Exam Tips - ๐ Direct vs Indirect is THE most confused pair: Direct = address field IS the address; Indirect = address field POINTS TO a cell CONTAINING the address (one extra dereference) โ always count memory accesses (Direct = 1, Indirect = 2). - ๐ Immediate mode has NO effective address โ the value in the instruction IS the data, not an address pointing to data โ don't try to compute an "EA" for immediate mode. - ๐ Register vs Register Indirect: Register mode โ operand is directly IN the register; Register Indirect โ register holds an ADDRESS, and operand is IN memory at that address.
3 Complex & Relative Modes
3.1 Base Register Addressing
- Effective Address = Base Register content + Address field (displacement).
- The Base Register typically holds the starting address of a program/data segment; the address field provides an offset from this base.
- Use Case: ๐ Relocatable code โ the same program can be loaded at different memory locations; only the Base Register needs updating, while all instruction offsets remain unchanged.
1EA = [Base Register] + Displacement3.2 Index Addressing (with Displacement)
- Effective Address = Index Register content + Address field (displacement).
- The Index Register typically holds an offset that changes systematically (e.g., incremented in a loop) to step through consecutive memory locations.
- Use Case: ๐ Array/table processing โ accessing successive array elements by incrementing the index register each iteration, while the base address field stays fixed.
1EA = [Index Register] + Displacement๐ก Base vs Index โ Conceptual Distinction: Both use the SAME formula structure (Register + Displacement), but differ in purpose/usage pattern: Base Register = fixed starting point of a segment (changes rarely, e.g., on program relocation); Index Register = frequently changing offset (changes every loop iteration, e.g., array traversal).
3.3 Relative Addressing Mode
- Effective Address = Program Counter (PC) content + Address field (displacement).
- Commonly used for branch/jump instructions, where the target is specified as an offset RELATIVE to the current instruction's address.
- Use Case: ๐ Relocatable code & short branch instructions โ branch targets remain correct even if the whole program is loaded at a different base address, since offsets are relative, not absolute.
1EA = [PC] + Displacement3.4 Auto-Increment / Auto-Decrement Modes
- Auto-Increment: The register (acting as a pointer) is used to access the operand, and is THEN automatically incremented (typically by the operand size) โ useful for sequential access (e.g., stepping through an array).
- Auto-Decrement: The register is FIRST automatically decremented, and THEN used to access the operand โ useful for stack push operations or reverse traversal.
1Auto-Increment: EA = [Register] ; then Register โ Register + d2Auto-Decrement: Register โ Register โ d ; then EA = [Register]- Use Case: ๐ Stack operations (PUSH/POP) and efficient array/string traversal without needing a separate explicit increment instruction.
3.5 Use Case Summary
| Mode | Primary Use Case |
|---|---|
| Base Register | Relocatable programs, segment-based memory organization |
| Index | Array/table traversal, iterating over data structures |
| Relative | Branch/jump instructions, position-independent code |
| Auto-Increment/Decrement | Stack operations (PUSH/POP), sequential array/string processing |
| Register Indirect | Pointer-based data structures (linked lists, dynamic memory) |
### โ ๏ธ Common Traps & Exam Tips - ๐ Base vs Index vs Relative all share the SAME mathematical formula (Register + Displacement), but the register USED differs: Base Register (segment start), Index Register (loop counter/offset), PC (relative to current instruction) โ questions often test which register is implied by the mode NAME. - ๐ Auto-Increment happens AFTER access; Auto-Decrement happens BEFORE access โ a frequently reversed/confused detail in exams (memory aid: "increment LATER, decrement EARLIER"). - ๐ Relative addressing is essential for position-independent code โ a key reason branch instructions use it instead of absolute addressing.
MASTER COMPARISON TABLE ALL Addressing Modes
| Mode | Definition | Effective Address (EA) Formula | Example | Use Case |
|---|---|---|---|---|
| ๐ฃ Immediate | Operand embedded directly in instruction | N/A (operand = value) | MOV R1, #5 | Loading constants |
| ๐ต Direct/Absolute | Address field = actual operand address | EA = Address Field | MOV R1, 200 | Simple fixed-location variables |
| ๐ข Indirect | Address field points to a cell holding real address | EA = Memory[Address Field] | MOV R1, @200 | Pointers, dynamic data |
| ๐ก Implied | Operand location fixed by opcode itself | N/A | CLC | Flag/accumulator-specific ops |
| ๐ Register | Operand resides in a CPU register | N/A (register = operand) | MOV R1, R2 | Fast temporary computation |
| ๐ด Register Indirect | Register holds the address of the operand | EA = Contents of Register | MOV R1, (R2) | Pointer-based/linked structures |
| ๐ค Base Register | Base register + displacement | EA = [Base Register] + Displacement | MOV R1, 20(BR) | Relocatable code/segments |
| โช Index | Index register + displacement | EA = [Index Register] + Displacement | MOV R1, 20(IX) | Array/table traversal |
| โซ Relative | PC + displacement | EA = [PC] + Displacement | BEQ +8 | Branch/jump, position-independent code |
| ๐ท Auto-Increment | Access, THEN increment register | EA = [Register]; Reg โ Reg + d | MOV R1, (R2)+ | Sequential array/string access |
| ๐ถ Auto-Decrement | Decrement register, THEN access | Reg โ Reg โ d; EA = [Register] | MOV R1, -(R2) | Stack PUSH, reverse traversal |
Solved Numericals/Conceptual Problems (continued)
Q6. A register R2 contains the value 300. Using Register Indirect addressing (MOV R1, (R2)), what value is loaded into R1?
Solution
: In Register Indirect mode, EA = Contents of Register = 300, so the operand loaded is Memory[300]. If Memory[300] = 500, then R1 = 500.
Q7. Using Indirect Addressing, an instruction has address field = 400. Memory[400] = 900. What is the Effective Address?
Solution
: EA = Memory[Address Field] = Memory[400] = 900
Q8. A Base Register holds 1000. An instruction specifies a displacement of 50. Find the Effective Address.
Solution
: EA = Base Register + Displacement = 1000 + 50 = 1050
Q9. An Index Register holds 20, and is used to traverse an array of 4-byte integers starting at base address 2000 (displacement = base address here). Find the address of the 5th element (index register incremented appropriately, 0-indexed access shown for element index 5 โ offset = 5ร4=20).
Solution
: EA = Displacement (array base) + Index Register content = 2000 + 20 = 2020 (i.e., the 5th element, 0-indexed, assuming Index Register was incremented to 20 = 5ร4 bytes)
Q10. The PC currently holds 500 (pointing to the next instruction). A relative branch instruction has a displacement of โ20. Find the target address.
Solution
: EA = PC + Displacement = 500 + (โ20) = 480
Q11. A register R1 = 100 is used in Auto-Increment mode to access a byte array (element size = 1 byte). What is the Effective Address for the CURRENT access, and what will R1 hold AFTER this instruction?
Solution
: EA (current access) = 100; After access, R1 โ R1 + 1 = 101
Q12. A register R1 = 100 is used in Auto-Decrement mode to access a word array (element size = 4 bytes). What is R1 BEFORE access, and what is the Effective Address used?
Solution
: R1 is FIRST decremented: R1 โ 100 โ 4 = 96; THEN EA = 96 (the access uses the already-decremented value)
### โ ๏ธ Common Traps & Exam Tips - ๐ Always identify EXACTLY how many memory dereferences a mode needs: Immediate/Register/Implied = 0 memory accesses for operand location; Direct/Register-Indirect/Base/Index/Relative = 1 memory access; Indirect = 2 memory accesses (address field โ address โ operand). - ๐ For Auto-Increment/Auto-Decrement numericals, always state clearly whether the given register value is BEFORE or AFTER the implicit update โ this ordering is the most common scoring error.
Quick Revision Summary
1โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ2 INSTRUCTION FORMATS (by number of explicit addresses)3 4-Address โ 3-Address โ 2-Address โ 1-Address (Accumulator) โ 0-Address (Stack)4 Fewer address fields โ shorter instructions, but MORE total instructions needed5 0-Address REQUIRES postfix (RPN) expression conversion first6
7 ADDRESSING MODES โ EFFECTIVE ADDRESS FORMULAS8 Immediate โ N/A (operand = value itself)9 Direct/Absolute โ EA = Address Field10 Indirect โ EA = Memory[Address Field] (2 memory accesses)11 Implied โ N/A (location fixed by opcode)12 Register โ N/A (operand = register content)13 Register Indirect โ EA = Contents of Register14 Base Register โ EA = [Base Register] + Displacement15 Index โ EA = [Index Register] + Displacement16 Relative โ EA = [PC] + Displacement17 Auto-Increment โ EA = [Register]; THEN Register โ Register + d18 Auto-Decrement โ Register โ Register โ d; THEN EA = [Register]19โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโKey Conceptual Recap
- Instruction Structure: 3-address maps most directly to expressions (no destructive overwrite); 2-address is destructive; 1-address relies heavily on the Accumulator; 0-address needs postfix conversion and uses a stack.
- Direct vs Indirect: count dereferences โ Direct = 1 memory access, Indirect = 2.
- Register vs Register Indirect: Register = operand IN register; Register Indirect = register holds an ADDRESS pointing to the operand.
- Base / Index / Relative: same formula shape (
Register + Displacement) but different registers used for different purposes โ Base (relocation), Index (array traversal), Relative/PC (branching, position-independent code). - Auto-Increment happens AFTER access; Auto-Decrement happens BEFORE access โ critical ordering distinction, frequently tested numerically.
Mastery check
How well can you recall this?
Finished this lesson?