Question

Consider the expression (a−1)∗(((b+c)/3)+d)(a - 1) * (((b + c) / 3) + d). Let XX be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture, in which (i) only load and store instructions can have memory operands and (ii) arithmetic instructions can have only register or immediate operands. The value of XX is ______.

Official answer and solution
Official answer
2
From
Official GATE 2017 answer key

Solution

Construct the optimal instruction sequence for evaluating the expression tree without register spilling on a load/store architecture.

We are given the expression (a−1)∗(((b+c)/3)+d)(a - 1) * (((b + c) / 3) + d) on a load/store architecture where memory operands must be loaded first, and arithmetic operations can use registers or immediate constants.

Let us determine if 2 registers (R1, R2) are sufficient without spilling:

  1. Evaluate (b+c)/3(b + c) / 3:
    • LOAD R1, b (R1 holds bb)
    • LOAD R2, c (R2 holds cc)
    • ADD R1, R1, R2 (R1 holds b+cb + c; R2 is now free)
    • DIV R1, R1, #3 (R1 holds (b+c)/3(b + c) / 3 using immediate 3)
  1. Add dd:
    • LOAD R2, d (R2 holds dd)
    • ADD R1, R1, R2 (R1 holds ((b+c)/3)+d((b + c) / 3) + d; R2 is free again)
  1. Evaluate (a−1)(a - 1) while holding the previous subexpression in R1:
    • LOAD R2, a (R2 holds aa)
    • SUB R2, R2, #1 (R2 holds a−1a - 1 using immediate 1)
  1. Final multiplication:
    • MUL R1, R1, R2 (R1 holds the final result)
      At every instruction, at most 2 registers (R1 and R2) are required simultaneously, and no register spill to memory is needed. Hence, the minimum number of registers is 2.
Final answer

2.0

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 1
Question
52 of 65, printed as CS Q.52
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.