Question:

Consider the program below which uses six temporary variables a, b, c, d, e and f.
a = 10
b = 20
c = 30
d = a + c
e = b + d
f = c + c
b = c + e
e = b + f
d = 5 + e
return d + f
Assuming that all the above operations take their operands from registers, the minimum number of registers needed to execute this program without spilling is:

Show Hint

When determining the minimum number of registers, consider reusing registers as variables are overwritten during computation.
Updated On: May 12, 2025
  • 5
  • 6
  • 3
  • 4
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Analyze the number of variables in use. We have six variables: \(a\), \(b\), \(c\), \(d\), \(e\), and \(f\), but we are aiming to reuse registers as much as possible. 
Step 2: Track the operations and register usage. Initially, we need 3 registers to hold `a`, `b`, and `c`.
During the program execution, we reuse the registers to store intermediate results:
`d = a + c` can reuse the register of `a` or `c`.
`e = b + d` can reuse the register of `b` or `d`.
`f = c + c` can reuse the register of `c`.
`b = c + e` can reuse the register of `b`.
`e = b + f` can reuse the register of `e`.
`d = 5 + e` can reuse the register of `d`.
By reusing the registers, we only need 3 registers to execute the program. 
Conclusion: Thus, the minimum number of registers required is: \[ \boxed{3}. \]

Was this answer helpful?
0
0