Skip to main content

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

Short answer

Using postfix A B F * - C * D E / +, the operation needs 5 three-address, 9 two-address, 11 one-address and 12 zero-address instructions.

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

Assumptions: the operands are memory locations, R1, R2, ... are CPU registers, T1, T2, ... are temporary memory locations, and X receives the result. Multiplication and division are done before addition and subtraction, and equal-priority operators go left to right.

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

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

MUL R1, B, F   ; R1 = B * F
SUB R2, A, R1  ; R2 = A - R1
MUL R3, R2, C  ; R3 = R2 * C
DIV R4, D, E   ; R4 = D / E
ADD X, R3, R4  ; X = 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
MOV R2, B   ; R2 = B
MUL R2, F   ; R2 = R2 * F
SUB R1, R2  ; R1 = R1 - R2
MUL R1, C   ; R1 = R1 * C
MOV R2, D   ; R2 = D
DIV R2, E   ; R2 = R2 / E
ADD R1, R2  ; R1 = R1 + R2
MOV X, R1   ; X = 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 B    ; AC = B
MUL F     ; AC = AC * F
STORE T1  ; T1 = AC
LOAD A    ; AC = A
SUB T1    ; AC = AC - T1
MUL C     ; AC = AC * C
STORE T2  ; T2 = AC
LOAD D    ; AC = D
DIV E     ; AC = AC / E
ADD T2    ; AC = AC + T2
STORE X   ; X = 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
PUSH F  ; push F
MUL     ; pop two, compute, push result
SUB     ; pop two, compute, push result
PUSH C  ; push C
MUL     ; pop two, compute, push result
PUSH D  ; push D
PUSH E  ; push E
DIV     ; pop two, compute, push result
ADD     ; pop two, compute, push result
POP X   ; X = 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 →