Skip to content
Official PYQ · 2017

GATE CS 2017 Set 1 Q52 — Runtime Environments

Explain with AI

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 

  1.  only load and store instructions can have memory operands and 
  2.  arithmetic instructions can have only register or immediate operands. 

​​​​​​​The value of XX is _____________ .

View worked solution

Answer: 2

To find the minimum number of registers XX required for the expression (a−1)∗(((b+c)/3)+d)(a-1) * (((b+c)/3)+d) on a load/store architecture, we can write an optimal sequence of instructions. In a load/store architecture, memory variables must be loaded into registers before performing operations, while constants can be used as immediate operands.

Let us evaluate the expression using registers R1R_1 and R2R_2:

  1. Evaluate ((b+c)/3)+d((b+c)/3) + d:
    • LD R1, b             (R1 = b)
    • LD R2, c             (R2 = c)
    • ADD R1, R1, R2     (R1 = b + c; R2 is now free)
    • DIV R1, R1, #3     (R1 = (b + c) / 3, using immediate operand 3)
    • LD R2, d             (R2 = d)
    • ADD R1, R1, R2     (R1 = ((b + c) / 3) + d; R2 is now free)
  2. Evaluate (a−1)(a-1):
    • LD R2, a             (R2 = a; R1 still holds the right subexpression)
    • SUB R2, R2, #1     (R2 = a - 1, using immediate operand 1)
  3. Perform the final multiplication:
    • MUL R1, R2, R1     (R1 = (a - 1) * (((b + c) / 3) + d))

Throughout the execution, we use at most 2 registers (R1R_1 and R2R_2). Since adding two memory variables bb and cc requires both to be loaded into registers, we need at least 2 registers. Thus, the minimum number of registers required is 22.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions