Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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. 2
    2. 3
    3. 4
    4. 6
Correct Option: B

Let AX, BX, CX be three registers used to store results of temporary variables a, b, c, d, e, f.
a(AX) = 1
b(BX) = 10
c(CX) = 20
d(AX) = a(AX) + b(BX)
e(BX) = c(CX) + d(AX)
f(AX) = c(CX) + e(BX)
b(BX) = c(CX) + e(BX)
e(BX) = b(BX) + f(AX)
d(CX) = 5 + e(BX)
return d(CX) + f(AX)
Thus, only 3 registers can be used without any overwriting of data.



Your comments will be displayed only after manual approval.