Skip to main content

Address-format code for Y=(W+X)*(Y-Z)

Short answer

For Y = (W+X)*(Y-Z), three three-address, six two-address, seven one-address, and eight zero-address instructions are needed. The one-address code saves intermediate values in temporaries.

Write codes using 3, 2, 1 and 0 address instruction formats to perform the operation Y = (W+X)*(Y-Z).

Assumptions: the operands are memory locations, R1, R2, ... are CPU registers, T1, T2, ... are temporary memory locations, and Y receives the result. Multiplication and division are done before addition and subtraction, and equal-priority operators go left to right. Y appears on both sides, so the result is written to Y only in the last instruction, after the old value of Y has been used.

Fully bracketed: Y = (W+X)*(Y-Z). Postfix (reverse Polish) form: W X + Y Z - *.

Three-address instructions (OP dest, src1, src2)

ADD R1, W, X   ; R1 = W + X
SUB R2, Y, Z   ; R2 = Y - Z
MUL Y, R1, R2  ; Y = R1 * R2

Number of instructions: 3. Shortest program, but each instruction is long (three addresses).

Two-address instructions (OP dest, src; dest = dest OP src)

MOV R1, W   ; R1 = W
ADD R1, X   ; R1 = R1 + X
MOV R2, Y   ; R2 = Y
SUB R2, Z   ; R2 = R2 - Z
MUL R1, R2  ; R1 = R1 * R2
MOV Y, R1   ; Y = R1

Number of instructions: 6. A MOV is needed first because the destination operand is overwritten.

One-address instructions (accumulator AC is the implied operand)

LOAD W    ; AC = W
ADD X     ; AC = AC + X
STORE T1  ; T1 = AC
LOAD Y    ; AC = Y
SUB Z     ; AC = AC - Z
MUL T1    ; AC = AC * T1
STORE Y   ; Y = AC

Number of instructions: 7. Intermediate results that cannot stay in AC are saved in memory temporaries.

Zero-address instructions (stack machine)

PUSH W  ; push W
PUSH X  ; push X
ADD     ; pop two, compute, push result
PUSH Y  ; push Y
PUSH Z  ; push Z
SUB     ; pop two, compute, push result
MUL     ; pop two, compute, push result
POP Y   ; Y = top of stack

Number of instructions: 8. Only PUSH and POP carry an address; ADD, SUB, MUL, DIV work on the top two stack items, so the program follows the postfix form directly.

Summary: 3-address = 3, 2-address = 6, 1-address = 7, 0-address = 8 instructions. Fewer addresses per instruction means shorter instructions but more of them.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Organization and Architecture chapter 2 questions →