Sub topic 3
Instruction Formats: Zero, One, Two and Three Addresses
Assume you know binary and hexadecimal notation, basic logic gates, and the roles of registers and memory. This chapter uses address to mean a field that identifies an operand, usually a register or a memory location.
An instruction format defines how an instruction divides its bits among the opcode and operand fields. The opcode specifies the operation, such as ADD or LOAD. An address field specifies where an operand is found.
Three-address instructions
A three-address instruction names two source operands and one destination:
ADD R1, R2, R3
R1 <- R2 + R3The instruction reads R2 and R3, adds them, and stores the result in R1. A typical format contains an opcode and three register fields.
Three-address instructions closely match expressions such as:
x = y + zThey reduce the number of instructions needed for complex calculations. Their cost is a larger instruction format. If each register field has 4 bits, three fields consume 12 bits before the opcode is stored.
Two-address instructions
A two-address instruction uses one field as both an input and the destination:
ADD R1, R2
R1 <- R1 + R2The first operand is overwritten. To preserve the original value of R1, the processor may need an extra MOVE instruction.
Two-address formats use fewer bits than three-address formats, but they can require more instructions and more data movement.
One-address instructions
A one-address instruction uses an implicit accumulator. The accumulator is a special register implied by the instruction:
LOAD X
ADD Y
STORE ZThe operations are:
AC <- M[X]
AC <- AC + M[Y]
M[Z] <- ACHere, AC is not written in the instruction. The format needs only one explicit address field, but every calculation depends on the accumulator.
Zero-address instructions
A zero-address instruction uses an implicit stack. Operands are taken from the top of the stack, and results are pushed back:
PUSH X
PUSH Y
ADD
POP ZThe ADD instruction removes the top two values, adds them, and pushes the result. It needs no address fields because the stack defines the operands.
| Format | Explicit addresses | Main implicit storage | Typical effect |
|---|---|---|---|
| Three-address | 3 | None | `R1 <- R2 + R3` |
| Two-address | 2 | Destination is also a source | `R1 <- R1 + R2` |
| One-address | 1 | Accumulator | `AC <- AC + M[X]` |
| Zero-address | 0 | Stack | `TOS <- TOS + next` |
Choosing a format
More explicit addresses usually mean fewer instructions for an expression, but they require more instruction bits. Fewer addresses produce compact instructions, but they increase dependence on implicit registers, stack operations, or extra data movement.
For an expression such as A = B + C * D, a compiler may choose different sequences depending on the format:
formats = {
"three_address": ["MUL T, C, D", "ADD A, B, T"],
"two_address": ["MOV T, C", "MUL T, D", "ADD T, B", "MOV A, T"],
"one_address": ["LOAD C", "MUL D", "ADD B", "STORE A"],
}
for name, instructions in formats.items():
print(name, ":", len(instructions), "instructions")The sequences are illustrative. Actual instruction sets may use different register and memory rules.
Exercise
In a two-address machine, translate X = Y + Z while preserving Y.
Show answer
Use MOV X, Y followed by ADD X, Z. The first instruction copies Y, and the second computes X <- X + Z.