Sub topic 2
Computing Effective Addresses and Operands
This chapter assumes you know binary and hexadecimal numbers, basic logic gates, and the roles of registers and memory. The ALU and control topics build on the effective-address idea, so read them in order.
Computing effective addresses and operands
An addressing mode tells the processor how to find an operand. The effective address (EA) is the memory location actually used. The instruction's address field x is the starting value.
def fetch_operand(mode, x, regs, memory):
"""Return (effective_address, operand) for a simple addressing mode."""
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(mode)
return ea, memory[ea]
memory = {100: 500, 500: 7, 600: 42}
regs = {"R1": 100, "XR": 4}
print(fetch_operand("indexed", 596, regs, memory)) # (600, 42)Two's complement refresher
An n-bit two's complement number ranges from -2^(n-1) to 2^(n-1) - 1. To negate a value, invert every bit and add 1. Subtraction computes A - B as A + NOT(B) + 1.
Inside the ALU
The ALU is a combinational circuit. It takes two operands and a select signal that chooses the operation, such as add, AND, OR, or shift. It outputs a result and status flags.
Ripple-carry and carry-lookahead adders
A ripple-carry adder chains full adders. Each stage waits for the carry from the stage before it, so delay grows linearly with word length n. A carry-lookahead adder computes carries in parallel from generate (g = a AND b) and propagate (p = a XOR b) signals. Its delay grows roughly with log n, at the cost of more gates.
Status flags: zero, negative, carry and overflow
- Zero: the result is 0.
- Negative: the most significant bit (MSB) of the result is 1.
- Carry: a carry leaves the MSB, which signals unsigned overflow.
- Overflow: the carry into the MSB differs from the carry out of it, which signals signed overflow.
def add8(a, b):
total = a + b
result = total & 0xFF
carry = total >> 8
sign_a, sign_b, sign_r = (a >> 7) & 1, (b >> 7) & 1, (result >> 7) & 1
overflow = int(sign_a == sign_b and sign_r != sign_a)
return result, carry, overflow
print(add8(0x7F, 0x01)) # (128, 0, 1)Hardware multiplication and division
Shift-and-add multiplication scans the multiplier one bit at a time. For each bit that is 1, it adds the multiplicand to a partial product, then shifts. An n-bit multiply takes n steps. Restoring division shifts the remainder, subtracts the divisor, and restores the previous remainder if the result is negative.
Datapath and register transfer notation
Register transfer notation writes each micro-step as destination <- source. Every instruction begins with the same fetch steps:
T0: MAR <- PC
T1: MDR <- M[MAR], PC <- PC + 1
T2: IR <- MDRExercise
An 8-bit addition computes 0x7F + 0x01. What are the result and the overflow flag?
Show answer
The result is 0x80 (128) and overflow is 1, because 127 + 1 exceeds the signed 8-bit maximum of 127.