in Compiler Design edited by
19,326 views
56 votes
56 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 _____________ .

in Compiler Design edited by
by
19.3k views

14 Comments

14
14
here i think the order of evaluation of the expression is done in such a way that the code generated is small and the number of registers is less hence evaluating (a-1) first will give the answer 3 and evaluating last gives the answer 2 but the answer is 2 as it is least with minimum number of registers
5
5
4
4

https://gateoverflow.in/2138/gate2011-36

The instructions produce result only in a register. If no intermediate results can be stored in memory, 

 (without any register spill) 

Does this statements means the same ??

 

0
0
What does mean without any register spill?
0
0
Bracket mismatch here.
0
0

Register Spilling: 

In most register allocators, each variable is assigned to either a CPU
register or to main memory. The advantage of using a register is
speed. Computers have a limited number of registers, so not all
variables can be assigned to registers. A “spilled variable” is a
variable in main memory rather than in a CPU register. The operation
of moving a variable from a register to memory is called spilling,
while the reverse operation of moving a variable from memory to a
register is called filling. For example, a 32-bit variable spilled to
memory gets 32 bits of stack space allocated and all references to the
variable are then to that memory. Such a variable has a much slower
processing speed than a variable in a register. When deciding which
variables to spill, multiple factors are considered: execution time,
code space, data space.
 
So to solve the given question without the register spilling means we have to use registers only and not move the register content to memory.
9
9

The labelling or Sethi Ullman algorithm works here perfectly, check out this https://www.cse.iitb.ac.in/~uday/courses/cs324-08/code-generation.pdf

3
3

@ No, we can’t apply Sethi-Ullman algorithm bcz, acc to the algorithm:

But in given question, its explicitly mention that both operands must be in register or an immediate operands.

“arithmetic instructions can have only register or immediate operands”.

 

4
4
0
0
what difference does it make whether result stored in memory or not stored in memory
0
0

@svas7246 

The main advantage of storing the result  as register is they are stored in CPU memory which is accessed very fast compared to RAM. This makes the program to get executed faster.

1
1

@svas7246

If intermediate result is stored in memory then we don’t need an additional register to store it and it minimizes the registers used.

2
2
edited by

Good Read

what the question want to tell by these 2 lines –

only load and store instructions can have memory operands  -> It means that during load and store instruction we can use variable name(memory operands) on RHS (Just to understand in easy language )

arithmetic instructions can have only register or immediate operands->.means whenever we perform arithmetic operation then we need to use only register(the register on  which the memory operands are stored or loaded ) as a operand OR Immediate Operands(constants) as a operand .

1
1

3 Answers

89 votes
89 votes
Best answer
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

4 Comments

1
1
what if there was another variable in place of 1. Then would we require 3 registers?
0
0
edited by

@sanketnagrale ...yes 3 registers would be required...assign 1 in place of leaf node having 1…But ..I think this is correct tree according to  Sethi-Ullman algo for given question ….We have to assign 1 to variables and zero if constants…

@Amit Puri You have assigned zero to c variable which is wrong i think…....correct me if wrong..

  1. For every non-constant leaf node, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.). For every constant leaf node (RHS of an operation – literals, values), assign a 0

 

3
3
26 votes
26 votes

solution..

1 comment

correct answer will be 2
1
1
2 votes
2 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

Answer:

Related questions