Sub topic 3
Instruction formats: zero, one, two and three addresses
This chapter assumes you know binary and hexadecimal numbers, what a register and a memory word are, and that an instruction has an opcode and operand fields. The addressing modes that decide how each operand field is interpreted come in a later chapter. Here an address field simply names a memory location or a register.
What an instruction format fixes
An instruction format is the layout of bits inside one instruction. It has an opcode field that selects the operation, followed by zero or more address fields. The number of address fields is the main way to classify formats, and it decides three things:
- How many bits an instruction needs.
- How many instructions a program needs for the same computation.
- Where the machine keeps temporary results.
The four classic formats differ in where operands and the result live.
| Format | Typical instruction | Where operands come from | Where the result goes |
|---|---|---|---|
| Three-address | `ADD X, Y, Z` | Two source fields | A separate destination field |
| Two-address | `ADD X, Y` | The two fields, X and Y | Overwrites X |
| One-address | `ADD Y` | Y and the accumulator | The accumulator |
| Zero-address | `ADD` | The top two stack entries | Pushed onto the stack |
The four formats in detail
Three-address. ADD R1, A, B means R1 ← M[A] + M[B]. Sources are preserved and no extra copying is needed, so programs are short. The cost is a long instruction.
Two-address. ADD X, Y means X ← X + Y. One field is both a source and the destination, so the original value of X is lost. If you still need it, you must copy it first with a MOV. Most real CISC machines of this type use a register for X.
One-address (accumulator). The accumulator is a special register that is an implicit operand and implicit destination. LOAD A copies M[A] into the accumulator, ADD B computes AC ← AC + M[B], and STORE T writes AC to memory. Only one accumulator exists, so intermediate results must be stored to memory.
Zero-address (stack). ALU instructions name no operands. They pop the top two stack entries, compute, and push the result. Only PUSH A and POP A carry an address, so a stack machine's instruction set does have one-address instructions. The ALU operations are the zero-address ones.
Worked example: X = (A + B) × (C + D)
Assume the three-address and two-address machines have registers R1 and R2. The accumulator machine uses a memory temporary T.
Three-address Two-address One-address Zero-address
ADD R1, A, B MOV R1, A LOAD A PUSH A
ADD R2, C, D ADD R1, B ADD B PUSH B
MUL X, R1, R2 MOV R2, C STORE T ADD
ADD R2, D LOAD C PUSH C
MUL R1, R2 ADD D PUSH D
MOV X, R1 MUL T ADD
STORE X MUL
POP X
3 instructions 6 instructions 7 instructions 8 instructionsThe zero-address program is the postfix form of the expression, A B + C D + *. A compiler for a stack machine converts an expression to postfix and emits one instruction per token. That is why the instruction count is easy to predict.
The following program runs all four sequences and checks that each gives 45 when A=2, B=3, C=4, D=5.
import operator
OPS = {"ADD": operator.add, "MUL": operator.mul}
def run3(prog, st):
for op, d, s1, s2 in prog:
st[d] = OPS[op](st[s1], st[s2])
def run2(prog, st):
for op, d, s in prog:
st[d] = st[s] if op == "MOV" else OPS[op](st[d], st[s])
def run1(prog, st):
acc = 0
for op, *arg in prog:
if op == "LOAD":
acc = st[arg[0]]
elif op == "STORE":
st[arg[0]] = acc
else:
acc = OPS[op](acc, st[arg[0]])
def run0(prog, st):
stack = []
for op, *arg in prog:
if op == "PUSH":
stack.append(st[arg[0]])
elif op == "POP":
st[arg[0]] = stack.pop()
else:
b, a = stack.pop(), stack.pop()
stack.append(OPS[op](a, b))
def fresh():
return {"A": 2, "B": 3, "C": 4, "D": 5}
s = fresh()
run3([("ADD", "R1", "A", "B"), ("ADD", "R2", "C", "D"),
("MUL", "X", "R1", "R2")], s)
print(s["X"]) # 45
s = fresh()
run2([("MOV", "R1", "A"), ("ADD", "R1", "B"), ("MOV", "R2", "C"),
("ADD", "R2", "D"), ("MUL", "R1", "R2"), ("MOV", "X", "R1")], s)
print(s["X"]) # 45
s = fresh()
run1([("LOAD", "A"), ("ADD", "B"), ("STORE", "T"), ("LOAD", "C"),
("ADD", "D"), ("MUL", "T"), ("STORE", "X")], s)
print(s["X"]) # 45
s = fresh()
run0([("PUSH", "A"), ("PUSH", "B"), ("ADD",), ("PUSH", "C"), ("PUSH", "D"),
("ADD",), ("MUL",), ("POP", "X")], s)
print(s["X"]) # 45Instruction size
Instruction length is the opcode bits plus the sum of all address-field bits. A field that names a memory word needs log2(number of words) bits. A field that names a register needs only log2(number of registers) bits.
Suppose the machine has a 6-bit opcode and 12-bit memory addresses, with all operands in memory:
| Format | Length in bits |
|---|---|
| Three-address | 6 + 3 × 12 = 42 |
| Two-address | 6 + 2 × 12 = 30 |
| One-address | 6 + 12 = 18 |
| Zero-address | 6 |
Now compare programs. The three-address program has 3 instructions of 42 bits, which is 126 bits. The one-address program has 7 instructions of 18 bits, which is 126 bits. The shorter instructions do not automatically give a smaller program. Count both.
With register operands the picture changes. A register field of 3 bits (8 registers) makes ADD R1, R2, R3 only 6 + 9 = 15 bits. This is why register-based formats can afford three addresses.
Memory accesses
Each instruction fetch costs one memory access. Each memory operand read or written adds one more. An instruction that names only registers needs just its fetch.
Take ADD X, Y, Z with all three operands in memory. It makes 1 fetch + 2 reads + 1 write = 4 accesses. The accumulator ADD Y makes 1 fetch + 1 read = 2. The zero-address ADD makes 1 fetch if the top of the stack is held in registers. If the stack lives in memory, it adds 2 pops and 1 push, so 4 accesses.
Edge cases and common errors
- Two-address overwrites a source. In
ADD X, Y, X is lost. When a later statement reuses X, add aMOVfirst. This is why the two-address program above needs 6 instructions and not 5. - A one-address ALU operation has two sources. The second source is the accumulator, not a field. Do not count it as an address field when computing the instruction length.
- Stack machines still need addresses.
PUSHandPOPare one-address instructions. When a GATE question asks for the instruction count, include them. - Operand order matters for non-commutative operations. In
SUB, the stack machine computes second-from-top minus top. The zero-address code for A − B pushes A first, then B. Reversing the push order gives B − A. - Count what the question asks. Some questions ask for instruction count, others for total program bits or memory accesses. State which you computed. Also check whether the question lets you use registers or says all operands are in memory.
- Fixed against variable length. A format that gives every instruction the same length is simple to decode but wastes bits on short instructions. Variable-length formats save space but make fetch and decode harder. The next chapter shows how an expanding opcode fits several formats into one fixed length.
Exercise
Evaluate Y = A × B + C × D on a one-address accumulator machine that has LOAD, STORE, ADD and MUL, with a memory temporary T. How many instructions are needed, and what is the program length in bits if the opcode is 8 bits and addresses are 16 bits?
Show answer
One program is LOAD A, MUL B, STORE T, LOAD C, MUL D, ADD T, STORE Y. That is 7 instructions. Each is 8 + 16 = 24 bits, so the program is 7 × 24 = 168 bits.
Exercise
A zero-address machine has a 6-bit opcode and 10-bit addresses. Write the code for Z = (P − Q) × R, and give the total program size in bits. PUSH and POP carry an address and ALU instructions do not.
Show answer
The postfix form is P Q − R ×. The code is PUSH P, PUSH Q, SUB, PUSH R, MUL, POP Z. That is 4 instructions with an address (PUSH P, PUSH Q, PUSH R, POP Z) at 6 + 10 = 16 bits each, which is 64 bits. It also has 2 instructions without an address (SUB, MUL) at 6 bits each, which is 12 bits. The total is 76 bits.