Skip to main content

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

Short answer

For Y = (A+B)*(C+D)+G/E*F, postfix A B + C D + * G E / F * + gives 6 three-address, 10 two-address, 12 one-address and 14 zero-address instructions.

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

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. G/E*F is evaluated left to right as (G/E)*F.

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

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

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

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

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

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

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

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

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

Number of instructions: 12. 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
ADD     ; pop two, compute, push result
PUSH C  ; push C
PUSH D  ; push D
ADD     ; pop two, compute, push result
MUL     ; pop two, compute, push result
PUSH G  ; push G
PUSH E  ; push E
DIV     ; pop two, compute, push result
PUSH F  ; push F
MUL     ; pop two, compute, push result
ADD     ; pop two, compute, push result
POP Y   ; Y = top of stack

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