Sub topic 7
Computing effective addresses and operands
This chapter assumes you know the instruction formats and addressing mode names from the previous chapters, and that you can read binary and hexadecimal numbers. Here you will turn each addressing mode into a short calculation, count the memory accesses it needs, and avoid the usual mistakes in GATE questions.
Two quantities to keep apart
An instruction's address field, written x, is a number stored inside the instruction. It is not always an address and it is not always the operand. Every question in this area asks for one of two values:
- The effective address (EA) is the memory address where the operand lives.
- The operand is the data the instruction works on.
The addressing mode tells you how to get from x to the operand. Immediate and register modes have no EA, because the operand is not in memory.
The formulas
| Mode | Effective address | Operand | Operand memory reads |
|---|---|---|---|
| Immediate | none | `x` | 0 |
| Register | none | `R[x]` | 0 |
| Direct | `x` | `M[x]` | 1 |
| Indirect | `M[x]` | `M[M[x]]` | 2 |
| Register indirect | `R[x]` | `M[R[x]]` | 1 |
| Indexed | `x + XR` | `M[x + XR]` | 1 |
| Base register | `x + BR` | `M[x + BR]` | 1 |
| Relative | `x + PC` | `M[x + PC]` | 1 |
The counts cover only the operand. Fetching the instruction itself costs at least one more memory read. A direct-mode load therefore makes 2 memory references in total, and an indirect-mode load makes 3.
Each mode exists for a reason:
- Indexed mode steps through arrays.
xis the array start and the index register changes on each pass. - Base-register mode relocates a program.
BRholds the load address andxis an offset within the program. - Relative mode makes position-independent branch targets, because the offset does not change if the code moves.
- Indirect and register-indirect modes implement pointers.
Worked example
Suppose a one-word instruction sits at address 200, so after the fetch PC = 201. Its address field is x = 20. The registers are R1 = 100, XR = 4, and BR = 1000. Memory holds M[20]=300, M[300]=9, M[24]=55, M[100]=5, M[221]=71, and M[1020]=88.
def operand(mode, x, regs, memory):
"""Return (effective_address, operand, operand_memory_reads)."""
if mode == "immediate":
return None, x, 0
if mode == "register":
return None, regs[x], 0
if mode == "direct":
ea, reads = x, 1
elif mode == "indirect":
ea, reads = memory[x], 2
elif mode == "register_indirect":
ea, reads = regs[x], 1
elif mode == "indexed":
ea, reads = x + regs["XR"], 1
elif mode == "base":
ea, reads = x + regs["BR"], 1
elif mode == "relative":
ea, reads = x + regs["PC"], 1
else:
raise ValueError(mode)
return ea, memory[ea], reads
memory = {20: 300, 300: 9, 24: 55, 100: 5, 221: 71, 1020: 88}
regs = {"R1": 100, "XR": 4, "BR": 1000, "PC": 201}
print(operand("immediate", 20, regs, memory)) # (None, 20, 0)
print(operand("register", "R1", regs, memory)) # (None, 100, 0)
print(operand("direct", 20, regs, memory)) # (20, 300, 1)
print(operand("indirect", 20, regs, memory)) # (300, 9, 2)
print(operand("register_indirect", "R1", regs, memory)) # (100, 5, 1)
print(operand("indexed", 20, regs, memory)) # (24, 55, 1)
print(operand("base", 20, regs, memory)) # (1020, 88, 1)
print(operand("relative", 20, regs, memory)) # (221, 71, 1)The same x = 20 gives eight different operands. Direct mode returns 300, which is the content of location 20. Indirect mode treats that 300 as a pointer and returns M[300] = 9. Register-indirect mode ignores x for the EA, so x is a register number there.
Common errors
Using the wrong PC value
The CPU increments the PC during the fetch (PC <- PC + 1 in the T1 step of the fetch sequence). By the time the EA is computed, the PC already points to the next instruction.
If the instruction is two words long, the PC advances by 2 words, or by the instruction's size in bytes on a byte-addressed machine.
Mixing up "immediate" and "direct"
With LOAD 20, immediate mode loads the number 20. Direct mode loads the contents of location 20. The mode name in the question decides which.
Counting one level of indirection too many
Indirect mode has exactly one pointer hop: EA = M[x]. The extra hop (M[M[x]]) is the operand read, not a second pointer lookup. A multilevel-indirect machine is possible, but a question will say so if it applies.
Forgetting units on byte-addressed memory
If memory is byte-addressed and an array holds 4-byte words, element i is at base + 4*i. Some machines scale the index register automatically. Others do not. Read the question for this.
Overlooking address-width wraparound
If the machine has 16-bit addresses, x + XR is taken modulo 2^16. Relative offsets are normally signed (two's complement), so a negative offset moves the target backward. Sign-extend the offset before adding it.
Auto-increment and auto-decrement
Some machines add side effects. In auto-increment mode, EA = R[x], and the register is then increased by the operand size. In auto-decrement mode, the register is decreased first and then used as the EA. These modes implement stack pushes and pops and walk through arrays without a separate add instruction. For a question that asks for the operand, apply the order of operations stated in the question: post-increment uses the old register value, and pre-decrement uses the new one.
A short procedure for GATE questions
- Write down
x, the mode, and the register and memory contents given. - Decide whether the question wants the EA, the operand, or the number of memory accesses.
- Apply the formula from the table, adjusting the PC if the mode is relative.
- Read memory only where the formula needs it. Immediate and register modes need no memory read for the operand.
- Add the instruction fetch to any total access count.
Exercise
A two-word instruction is stored at address 500. It uses relative addressing with a signed offset of -6 in its address field. Memory is word-addressed, and the PC is incremented during fetch by the instruction length. What is the effective address?
Show answer
After the fetch, PC = 500 + 2 = 502. EA = 502 + (-6) = 496.
Exercise
A one-word LOAD uses indirect addressing with x = 20. Using the memory contents from the worked example (M[20] = 300, M[300] = 9), give the EA, the operand, and the total number of memory references including the instruction fetch.
Show answer
EA = M[20] = 300. Operand = M[300] = 9. The operand needs 2 reads, plus 1 for the instruction fetch, so the total is 3 memory references.
Exercise
An array of 4-byte integers starts at byte address 2000 in a byte-addressed machine. An indexed-mode instruction has x = 2000, and the index register is not scaled by hardware. What must XR hold to reach element 10, and what is the EA?
Show answer
XR must hold 10 * 4 = 40. EA = 2000 + 40 = 2040.