Instructions of a computer do not all look alike. Some name three operands and others none. The number of address fields in an instruction, and the way these fields are interpreted, determine how long a program is and how fast it runs.
Instruction Formats
Four common formats are used, according to the number of address fields. As Table 3.7 shows, a three-address format has the shortest programs but the longest instructions, and the zero-address format uses a stack.
Table 3.7: Programs for X = (A + B) * (C + D) in different formats
| Format | Program |
|---|---|
| Three-address | ADD R1, A, B; ADD R2, C, D; MUL X, R1, R2 |
| Two-address | MOV R1, A; ADD R1, B; MOV R2, C; ADD R2, D; MUL R1, R2; MOV X, R1 |
| One-address | LOAD A; ADD B; STORE T; LOAD C; ADD D; MUL T; STORE X |
| Zero-address | PUSH A; PUSH B; ADD; PUSH C; PUSH D; ADD; MUL; POP X |
Definition: Addressing mode. The rule by which the address field of an instruction is interpreted to obtain the effective address of the operand.
Addressing Modes
The common modes are described below.
- Implied mode: the operand is implied by the instruction, as in the accumulator.
- Immediate mode: the operand is in the instruction itself.
- Register mode: the operand is in a register named by the instruction.
- Register indirect mode: the register holds the address of the operand.
- Autoincrement or autodecrement mode: like register indirect, but the register is changed after or before use.
- Direct mode: the address field gives the address of the operand.
- Indirect mode: the address field gives the address of the address of the operand.
- Relative mode: the effective address is the address field added to the PC.
- Indexed mode: the effective address is the address field added to an index register.
- Base register mode: the effective address is the address field added to a base register.
Example 3.8: Effective address in different modes
Problem: An instruction at address 250 has the address field 500. After it is fetched PC = 252. R1 = 400, XR = 100, and M[500] = 800. Find the effective address (EA) in the modes direct, indirect, relative, indexed and register indirect.
Solution:
- Direct: EA = 500.
- Indirect: EA = M[500] = 800.
- Relative: EA = 252 + 500 = 752.
- Indexed: EA = 100 + 500 = 600.
- Register indirect: EA = R1 = 400.
Result: The effective addresses are 500, 800, 752, 600 and 400.
pc, addr, r1, xr, m500 = 252, 500, 400, 100, 800
print("direct", addr, "indirect", m500)
print("relative", pc + addr, "indexed", xr + addr, "reg ind", r1)
Output
direct 500 indirect 800
relative 752 indexed 600 reg ind 400
Common mistakes
- Using the address of the instruction instead of the incremented PC in relative mode.
- Confusing immediate mode, where the field is the operand, with direct mode, where it is an address.
- Treating indirect mode as a single memory read. It needs two reads.
Key takeaways
- The number of address fields gives three-, two-, one- and zero-address formats.
- Immediate mode: the operand is in the instruction.
- Indirect mode needs two memory accesses.
- Relative and indexed modes add the address field to PC or XR.
Practice questions
- What is the effective address?
- Which instruction format uses a stack?
- What is the effective address in indexed mode?
Answers to the practice questions
- The address of the operand actually used after the addressing mode has been applied.
- The zero-address format.
- The address field plus the content of the index register.
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit III
