The Gateway to Computer Science Excellence
0 votes
143 views
The following program uses six different variables p, q, r, s, t and u. The code is:

p=6

q=7

t=p*q

s=t+p

u=8

u=s*p

s=p+u

r=r*q

t=t+p

return t

Assume that all operations take their operands from registers, the minimum number of registers needed to execute this program without spilling are _____________?

 

Given answer is 5, but my answer is 4.

I think that the step u=8 can be skipped since 'u' is being reinitialized in the next step.
in Compiler Design by (253 points)
edited by | 143 views
0
i am getting 5 as answer.

1 Answer

+1 vote
Best answer

Here all the registers defined above the line... has future reference to the statements that are below the line and will be used in future. so we cannot use them to store other values before the line

 i have represented them in ovals with same color. so its 5 registers.

by Boss (12.2k points)
selected by
0
hey u=8 is dead why you assign register its only 4 ?..someone verify pls
0
@minal yes u are right. But the system won't know that u=8 is a dead code... It can be seen only by the user. And moreover if system gets it as dead code it would be after entire scan... So 5 registers needed by system.
0

as i know it will check its live or not ... can you give some standard reference for it 

as i read in dac https://www.cse.iitm.ac.in/~krishna/cs3300/pm-lecture3.pdf

pls conform once.

check this one also , 7 is correct na given ans 6

https://gateoverflow.in/233999/made-easy-test

0
There is a difference between these two question... It has asked for minimum number of registers... That has asked for minimum number of variables..
0
ha these are different .. i just want to know 7 is correct na
+1
yes. its correct
0

why bottom up approach is not followed here???thats the standard approach right??

i was getting 3 as answer!

BOTTOM TO TOP APPROACH IS USED!!

p=6

q=7              {p}

t=p*q            {p,q}

s=t+p           {p,q,t}

u=8              {p,q,s}

u=s*p          {p,q,s}

s=p+u       {p,q,u}

r=r*q          {p,q}

t=t+p          {p}

return t       {t}

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,392 answers
198,593 comments
105,444 users