The program below uses six temporary variables a, b, c, d, e, f. a = 1 b = 10…

2025

The program below uses six temporary variables a, b, c, d, e, f.

a = 1
b = 10
c = 20

d = a + b
e = c + d
f = c + e

b = c + e
e = b + f

d = 5 + e

return d + f

Assuming that all operations take their operands from registers, what is the minimum number of registers needed to execute this program without spilling?

  1. A.

    2

  2. B.

    3

  3. C.

    4

  4. D.

    6

Attempted by 16 students.

Show answer & explanation

Correct answer: B

To find the minimum registers, track variable lifetimes. At step e = c + d, variables c, d, and e are live simultaneously (3 registers). When f = c + e is computed, c and e remain live while d becomes dead. The maximum number of simultaneously live variables determines the minimum registers needed, which is 3.

Explore the full course: Niacl Ao It Specialist