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? (A) 2 (B) 3 (C) 4 (D) 6

My attempt :

enter image description here

But, people are using register R3 for variable C.

I've applied register re-usability without spilling, so answer is only 2.

My question is :

Whether, can we apply register re-usability or not !?

  • 1
    $\begingroup$ Any series of strictly additions where only the requested sum needs to be returned can always be implemented using just two registers: an accumulator and a loading register. But that's not the same as allocating registers for a specific program. The problem isn't very well-defined: sometimes you're allowed to still reorder operations if the result doesn't change, sometimes you're not. $\endgroup$ – orlp Mar 1 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.