Sub topic 7
Computing Effective Addresses and Operands
This chapter assumes you know binary and hexadecimal numbers, registers, and memory. You should also understand that an instruction contains an opcode and one or more address fields.
Address fields and effective addresses
An instruction's address field does not always directly name the operand. The processor uses an addressing mode, which is a rule for interpreting that field.
The effective address (EA) is the memory address finally used to access an operand. Immediate and register modes do not need a memory effective address.
| Addressing mode | Effective address or operand |
|---|---|
| Immediate | Operand is the address field itself |
| Register | Operand is the contents of the named register |
| Direct | EA = address field |
| Indirect | EA = memory[address field] |
| Register indirect | EA = contents of the named register |
| Indexed | EA = address field + index register |
Consider memory where M[100] = 500 and M[500] = 7.
- In direct mode with field
100, EA is100, so the operand isM[100] = 500. - In indirect mode with field
100, EA isM[100] = 500, so the operand isM[500] = 7. - In register-indirect mode with register
R1 = 100, EA is100, so the operand isM[100] = 500.
Indirect addressing therefore adds one extra memory lookup. That lookup can be useful when a memory word stores a pointer to another memory word.
A systematic calculation method
For every operand reference:
- Identify the addressing mode.
- Compute the effective address, if the mode uses memory.
- Read the operand from that address.
- Keep the address and value separate. Confusing them is a common source of errors.
For indexed addressing, the index register is usually used for arrays. If the instruction field is 596 and XR = 4, then:
EA = 596 + 4 = 600
The operand is M[600].
def fetch_operand(mode, x, regs, memory):
"""Return (effective_address, operand)."""
if mode == "immediate":
return None, x
if mode == "register":
return None, regs[x]
if mode == "direct":
ea = x
elif mode == "indirect":
ea = memory[x]
elif mode == "register_indirect":
ea = regs[x]
elif mode == "indexed":
ea = x + regs["XR"]
else:
raise ValueError(f"unknown mode: {mode}")
return ea, memory[ea]
memory = {100: 500, 500: 7, 600: 42}
regs = {"R1": 100, "XR": 4}
print(fetch_operand("direct", 600, regs, memory))
print(fetch_operand("indirect", 100, regs, memory))
print(fetch_operand("register_indirect", "R1", regs, memory))
print(fetch_operand("indexed", 596, regs, memory))Tracing the lookup
Exercise
Exercise
Memory contains M[200] = 900 and M[900] = 15. What are the effective address and operand for indirect addressing with address field 200?
Show answer
EA = M[200] = 900, and the operand is M[900] = 15.