CSC213 · 2080 · 5 marks
Write the program for following statement by using three, single, zero address instructions. X = (A+B*C-D)/(E*F+G)
Write the program for following statement by using three, single, zero address instructions. $X = (A+BC-D)/(EF+G)$ [5]
X = (A + BC - D) / (EF + G)
Using Three-Address, One-Address (Accumulator), and Zero-Address (Stack) instructions.
Given Data
- Expression: $X = \dfrac{A + B \cdot C - D}{E \cdot F + G}$
- Operands: $A, B, C, D, E, F, G$
- Operations: multiply, add, subtract, divide
Three-Address Instructions
Format: OP dest, src1, src2 meaning dest = src1 OP src2
MUL T1, B, C ; T1 = B * C
ADD T2, A, T1 ; T2 = A + B*C
SUB T3, T2, D ; T3 = A + B*C - D (numerator)
MUL T4, E, F ; T4 = E * F
ADD T5, T4, G ; T5 = E*F + G (denominator)
DIV X, T3, T5 ; X = T3 / T5
Total: 6 instructions
One-Address Instructions (Accumulator based)
Format: OP M meaning AC = AC OP M
LOAD B ; AC = B
MUL C ; AC = B*C
ADD A ; AC = A + B*C
SUB D ; AC = A + B*C - D
STORE T1 ; T1 = numerator
LOAD E ; AC = E
MUL F ; AC = E*F
ADD G ; AC = E*F + G
STORE T2 ; T2 = denominator
LOAD T1 ; AC = numerator
DIV T2 ; AC = numerator / denominator
STORE X ; X = result
Total: 12 instructions
Zero-Address Instructions (Stack based)
Format: PUSH/POP M for memory access; arithmetic ops act on top two stack elements.
PUSH A ; [A]
PUSH B ; [A, B]
PUSH C ; [A, B, C]
MUL ; [A, B*C]
ADD ; [A+B*C]
PUSH D ; [A+B*C, D]
SUB ; [A+B*C-D] (numerator)
PUSH E ; [num, E]
PUSH F ; [num, E, F]
MUL ; [num, E*F]
PUSH G ; [num, E*F, G]
ADD ; [num, E*F+G] (denominator)
DIV ; [num/den]
POP X ; X = result
Total: 14 instructions
Summary
| Instruction Type | Count |
|---|---|
| Three-Address | 6 |
| One-Address | 12 |
| Zero-Address | 14 |
As the number of addresses per instruction decreases, more instructions are required, but each instruction becomes simpler and shorter.
All three programs evaluate the expression correctly while respecting operator precedence (multiplication before addition and subtraction, numerator and denominator computed before division).
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.