• edited by
21,267 views
63 63 votes

Consider the grammar rule $E \rightarrow E1 – E2$ for arith­metic expressions. The code generated is targeted to a CPU having a single user register. The sub­traction operation requires the first operand to be in the register. If $E1$ and $E2$ do not have any com­mon sub expression, in order to get the shortest possible code

  1. $E1$ should be evaluated first
  2. $E2$ should be evaluated first
  3. Evaluation of $E1$ and $E2$ should necessarily be interleaved
  4. Order of evaluation of $E1$ and $E2$ is of no consequence

5 Answers

Best answer
89 89 votes
$E2$ should be evaluated first

After evaluating $E2$ first and then $E1$, we will have $E1$ in the register and thus we can simply do SUB operation with $E2$ which will be in memory (as we have only a single register). If we do $E1$ first and then $E2$, we must move $E2$ to memory and $E1$ back to register before doing SUB, which will increase the code size.

for more expalianation see this discussion https://gateoverflow.in/4069/gate-cse-2004-question-10?show=100621#c100621
• edited by
16 16 votes

Suppose that E1 is stored at memory location 1005 and E2 is stored at memory location 1010.

We want to perform E1 – E2.. and we have 1 Register named R1.

So for Substraction we first move E1 into ALU followed by E2.

Suppose we first evaluate E1.

  1. Move E1 to R1
  2. Assign value 5 to E1 =>> E1 = 5
  3. Move E1 back at Memory location 1005.
  4. Move E2 to R1.
  5. Assign value 10 to E2 =>> E2=10
  6. Move E2 back at memory location 1010.
  7. Move E1=5 at R1 again.
  8. Move R1 in to accumulator.(or in ALU)
  9. Move E2=10 at R1 again.
  10. Move R2 in to accumulator.(or in ALU)

Now we first evaluate E2.

  1. Move E2 to R1.
  2. Assign value 10 to E2 =>> E2=10
  3. Move E2 back to Memory location 1010.
  4. Move E1 to R1.
  5. Assign value 5 to E1 =>> E1=5
  6. Now move or load R1 in accumulator.(or in ALU)
  7. Move E2=10 at R1 again.
  8. Move R2 in to accumulator(or in ALU)

So evaluating E2 first takes less steps.

12 12 votes

 E -> E1 - E2

Given that E1 and E2 don't share any sub expression, most optimized usage of single user register for evaluation of this production rule would come only when E2 is evaluated before E1.

This is because when we will have E1  evaluated in the register, E2 would have been already computed and stored at some memory location. Hence we could just use subtraction operation to take the user register as first operand, i.e. E1 and E2 value from its   memory location referenced using some index register or some other form according to the instruction. Hence correct answer should be (B) E2 should be evaluated first. 

0 0 votes
@arjun sir

If they said that spilling is not allowed and we have only one register then is this operation possible with any way of code

Is it necessary to move the E1 without moving to register
0 0 votes

We need to do E1-E2, but E1,E2 are arithmetic expressions themselves, so we need to evaluate them before we do the subtraction. 
( ignoring load operation of the frst expressions)

What if we evaluate E1 first?
1. Evaluate E1 --> result ends up in register
2. Store E1 (since we also need to evaluate E2 using same reg.)
3. Load E2
4. Evaluate E2 ----> result in register
5. Store E2 ( since the operation requires the first operand to be in the register, we need to put E1 in register)
6. Load E1
7. SuB

now if we evaluate E2 first
1. Evaluate E2
2. Store E2
3. Load E1
4. Evaluate E1 ( First operand in register as intended)
5  Sub

               When we evaluate E1 first, we need extra operation for storing E1, one extra load operation after the E2 is evaluated and stored. 
So E2 evaluation should be done first. 


" E1 and E2 do not have any com­mon sub expression" - if they had common subexpressions, interleaved evaluation might have been an optimal choice(not necessarily by length of code), but we don't necessarily know the pattern of the common subexpression - so we can't really say anything about the length of code. Some codes might be smaller for interleaved evaluation, while others might be bigger. 

 

 

Answer:
Position:
Show:

Related questions

33 33 votes
3 answers 3 answers
13.2k
13.2k views
Kathleen asked Sep 18, 2014
13,190 views
Consider the following grammar G:$S \rightarrow bS \mid aA \mid b$$A \rightarrow bA \mid aB$$B \rightarrow bB \mid aS \mid a$Let $N_a(w)$ and $N_b(w)$ denote the number o...
42 42 votes
2 answers 2 answers
17.1k
17.1k views
Kathleen asked Sep 18, 2014
17,063 views
Consider the grammar with the following translation rules and $E$ as the start symbol$$\begin{array}{lll}E \rightarrow E_ 1\# \: T & \qquad\left\{E.value = E_1.value * ...
31 31 votes
3 answers 3 answers
16.5k
16.5k views
Kathleen asked Sep 18, 2014
16,536 views
Which of the following grammar rules violate the requirements of an operator grammar? $P, Q, R$ are nonterminals, and $r, s, t$ are terminals.$P \rightarrow Q R$$P \right...
77 77 votes
4 answers 4 answers
30.6k
30.6k views
Arjun asked Feb 14, 2017
30,586 views
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 ...