Skip to main content

Address-format code for Y=AB+(F/G)+CD

Short answer

The postfix expression is A B * F G / + C D * +. Three-address code uses 5 instructions, two-address 9, one-address 11, zero-address 12. Fewer addresses per instruction results in more instructions overall.

Write codes using 3, 2, 1 and 0 address instruction formats to perform the operation Y = AB+(F/G)+CD.

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. Adjacent letters with no operator, or a letter before a bracket, are read as multiplication. So AB is A × B and CD is C × D.

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

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

MUL R1, A, B    ; R1 = A * B
DIV R2, F, G    ; R2 = F / G
ADD R3, R1, R2  ; R3 = R1 + R2
MUL R4, C, D    ; R4 = C * D
ADD Y, R3, R4   ; Y = R3 + 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
MUL R1, B   ; R1 = R1 * B
MOV R2, F   ; R2 = F
DIV R2, G   ; R2 = R2 / G
ADD R1, R2  ; R1 = R1 + R2
MOV R2, C   ; R2 = C
MUL R2, D   ; R2 = R2 * D
ADD 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
MUL B     ; AC = AC * B
STORE T1  ; T1 = AC
LOAD F    ; AC = F
DIV G     ; AC = AC / G
ADD T1    ; AC = AC + T1
STORE T2  ; T2 = AC
LOAD C    ; AC = C
MUL D     ; AC = AC * D
ADD T2    ; AC = AC + T2
STORE Y   ; Y = AC

Number of instructions: 11. 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
MUL     ; pop two, compute, push result
PUSH F  ; push F
PUSH G  ; push G
DIV     ; pop two, compute, push result
ADD     ; pop two, compute, push result
PUSH C  ; push C
PUSH D  ; push D
MUL     ; pop two, compute, push result
ADD     ; 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 = 11, 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 →