Direction: The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and one destination operand. Assume that all variables are dead after this code segment.
c = a + b;
d = c * a;
e = c + a;
x = c * c;
if (x > a) {
y = a * a;
}
else {
d = d * d;
e = e * e;
}
-
What is the minimum number of registers needed in the instruction set architecture of the processor to compile this code segment without any spill to memory? Do not apply any optimization other than optimizing register allocation.
-
- 3
- 4
- 5
- 6
- 3
Correct Option: B
Assuming that R1 and R2 holds the value of a and b respectively. The machine code is equivalent to
R2 ← R1 + R2 (c = a + b) (R2 holds c)
R3 ← R2 *R1 (d = c * a)
R4 ← R2 + R1 (e = c + a)
R2 ← R2 *R2 (x = c * c)
(Removing c from R2 as c is not used after this statements R2 holds x)
If (R2 > R1) {{If (x > a)}
If (R2 > R1)
{
R2 ← R1 × R1
R2 ← R1 * R1 (y = a* a)
}
else {
R3 ← R3 * R3 {d =d *d)
R4 ← R4 * R4 (e = e * e)
}
We have used R1, R2, R3 ,R4 registers So answer is 4.