• edited by
30,292 views
75 75 votes

Consider the expression $(a-1) * (((b+c)/3)+d)$. Let $X$ be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture, in which 

  1.  only load and store instructions can have memory operands and 
  2.  arithmetic instructions can have only register or immediate operands. 

​​​​​​​The value of $X$ is _____________ .

4 Answers

Best answer
118 118 votes
Load $R1,b$

Load $R2,c$

ADD $R1,R2$

Div $R1,3$

Load $R2,d$

Add $R1,R2$

Load $R2,a$

Sub $R2,1$

Mul $R2,R1$

hence minimum $2$ registers required
• edited by
10 10 votes

Answer: 2 Registers (minimum)


(adding this answer to visually see what’s happening really)

Let’s Construct an Expression tree – an easy way to visualize 

now come from bottom to top, numbered 1 to 9


(note: immediate values can be specified by CPU, hence is no need to load them into Reg and here “ $R_{i}$ ← a “ is load operation [abusive notation] )

we try to conserve as many registers as possible, we can come from anyway, left first or right first in the tree but make sure the register is free to use at that point (i.e, not holding any temporary value that we want to use in the future)

 

Answer

0 0 votes
After converting this expression to three address code we get:

t1=b+c

t2=t1/3

t3=t2+d

t4=a-1

t5=t3*t4

Now after doing liveness analysis of each and every statement we get:

Live(statement1)={a,b,c,d}

Live(statement 2)={a,d,t1}

Live(statement 3)={a,d,t2}

Live(statement 4)={t3,a}

Live(statement 5)={t3,t4}

Register allocation:

r1<-b (Allocate r1 to b)

r2<-c (Allocate r2 to c)

Since outlive of first statement is {a,d,t1}, so we can allocate any of the registers r1 or r2 to t1. Let's allocate r1 to t1.

r1<-r1+r2

Outlive (statement 2)={a,d,t2} , so we either can allocate r2 to t2 or put the result back in r1 after doing the division.

r2<-r1/3

Outlive(statement 3)={t3,a} and t2 is being used in this statement, so we can first allocate r1 to d and then finally allocate r1 to t3.

r1<-r2+r1

Outlive(statement 4)={t3,t4}, so first allocate r2 to a and put the final result back in r2

r2<-r2-1

Finally we can put the final result in either r1 or r2 after doing the multiplication. So minimum no of registers needed=2
Answer:
Position:
Show:

Related questions

85 85 votes
14 answers 14 answers
35.1k
35.1k views
Arjun asked Feb 14, 2017
35,056 views
Consider the following grammar:stmt $\rightarrow$ if expr then expr else expr; stmt | $Ò$expr $\rightarrow$ term relop term | termterm $\rightarrow$ id | numberid $\right...
48 48 votes
5 answers 5 answers
17.7k
17.7k views
Arjun asked Feb 14, 2017
17,668 views
Consider the following intermediate program in three address codep = a - b q = p * c p = u * v q = p + qWhich one of the following corresponds to a static single assignme...
89 89 votes
12 answers 12 answers
28.9k
28.9k views
Arjun asked Feb 14, 2017
28,875 views
A cache memory unit with capacity of $N$ words and block size of $B$ words is to be designed. If it is designed as a direct mapped cache, the length of the $\textsf{TAG}$...
145 145 votes
11 answers 11 answers
61.6k
61.6k views
Arjun asked Feb 14, 2017
61,623 views
Consider a $2$-way set associative cache with $256$ blocks and uses $\text{LRU}$ replacement. Initially the cache is empty. Conflict misses are those misses which occur d...