Skip to main content

Address-format code for Y=(A-B+C)*(E+F/G)

Short answer

For Y = (A-B+C)*(E+F/G), postfix is A B - C + E F G / + *. Three-address code uses 5 instructions (SUB, ADD, DIV, ADD, MUL). Two-address requires 9 instructions with MOVs. One-address accumulator needs 9 instructions using temporary T1. Zero-address stack machine uses 12 instructions following postfix with PUSH/POP. Three-address is most compact; zero-address has shortest instructions but most instructions overall.

Write codes using 3, 2, 1 and 0 address instruction formats to perform the operation Y = (A-B+C)*(E+F/G).

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.

Fully bracketed: Y = ((A-B)+C)*(E+(F/G)). Postfix (reverse Polish) form: A B - C + E F G / + *.

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

SUB R1, A, B   ; R1 = A - B
ADD R2, R1, C  ; R2 = R1 + C
DIV R3, F, G   ; R3 = F / G
ADD R4, E, R3  ; R4 = E + R3
MUL Y, R2, R4  ; Y = R2 * R4

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

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

MOV R1, A   ; R1 = A
SUB R1, B   ; R1 = R1 - B
ADD R1, C   ; R1 = R1 + C
MOV R2, E   ; R2 = E
MOV R3, F   ; R3 = F
DIV R3, G   ; R3 = R3 / G
ADD R2, R3  ; R2 = R2 + R3
MUL R1, R2  ; R1 = R1 * R2
MOV Y, R1   ; Y = R1

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

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

LOAD A    ; AC = A
SUB B     ; AC = AC - B
ADD C     ; AC = AC + C
STORE T1  ; T1 = AC
LOAD F    ; AC = F
DIV G     ; AC = AC / G
ADD E     ; AC = AC + E
MUL T1    ; AC = AC * T1
STORE Y   ; Y = AC

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

Zero-address instructions (stack machine)

PUSH A  ; push A
PUSH B  ; push B
SUB     ; pop two, compute, push result
PUSH C  ; push C
ADD     ; pop two, compute, push result
PUSH E  ; push E
PUSH F  ; push F
PUSH G  ; push G
DIV     ; pop two, compute, push result
ADD     ; pop two, compute, push result
MUL     ; pop two, compute, push result
POP Y   ; Y = top of stack

Number of instructions: 12. 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 = 5, 2-address = 9, 1-address = 9, 0-address = 12 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 →