Intermediate Code Generation · Compiler Design
32 questions · asked in 16 of 27 years (2000–2026)
MCQ 26 · MSQ 1 · NAT 51-mark 13 · 2-mark 19
2000Questions per year2026
Questions 31–32 of 32
In a simplified computer the instructions are:
The computer has only two registers, and OP is either ADD or SUB. Consider the following basic block:
Assume that all operands are initially in memory. The final value of the computation should be in memory. What is the minimum number of MOV instructions in the code generated for this basic block?
| OP Rj, Ri | - Performs Rj OP Ri and stores the result in register Ri. |
| OP m,Ri | - Performs val OP Ri and stores the result in Ri. val denotes the content of memory location m. |
| MOV m, Ri | - Moves the content of memory location m to register Ri. |
| MOV Ri, m | - Moves the content of register Ri to memory location m. |
The computer has only two registers, and OP is either ADD or SUB. Consider the following basic block:
Assume that all operands are initially in memory. The final value of the computation should be in memory. What is the minimum number of MOV instructions in the code generated for this basic block?
Answer: (B)
Meaning of the instructions: computes , with the memory value on the left. Only MOV moves data between memory and registers.
Code (the block is computed as written, with to as separate steps):
Why fewer is not possible: is computed first and is still needed at the last step, so it must stay in one register. To build and then , the other register must be loaded from memory with a MOV (OP works only on a register and a memory value, so a value cannot start in a register without a load). That is 2 loads. The result must end in memory, and only MOV can store it, so that is a third MOV. So at least 3.
Note: if algebraic regrouping were allowed, could be built in one register with only 2 MOVs (MOV ; SUB ; ADD ; ADD ; ADD ; MOV ). The key (B) assumes the block is computed as written.
Answer: (B)
Code (the block is computed as written, with to as separate steps):
- MOV
- ADD ()
- MOV
- ADD ()
- SUB (, because the memory value is on the left)
- SUB ()
- MOV (store the result)
Why fewer is not possible: is computed first and is still needed at the last step, so it must stay in one register. To build and then , the other register must be loaded from memory with a MOV (OP works only on a register and a memory value, so a value cannot start in a register without a load). That is 2 loads. The result must end in memory, and only MOV can store it, so that is a third MOV. So at least 3.
Note: if algebraic regrouping were allowed, could be built in one register with only 2 MOVs (MOV ; SUB ; ADD ; ADD ; ADD ; MOV ). The key (B) assumes the block is computed as written.
Answer: (B)
Consider the following C code segment.
for (i = 0; i < n; i++)
{
for (j=0; j < n; j++)
{
if (i%2)
{
x += (4*j + 5*i);
y += (7 + 4*j);
}
}
}
Which one of the following is false?Answer: (D)
(A)
(B)
(C) Multiplication can be replaced by addition. In the inner loop,
(D) Every statement can be reached (the
The false statement is (D).
Answer: (D)
i%2 and 5*i do not change inside the inner loop, so they are loop-invariant. True.(B)
4*j appears in two statements, so it can be computed once and reused (common subexpression elimination). True.(C) Multiplication can be replaced by addition. In the inner loop,
4*j can be kept in a variable t that is set to 0 before the inner loop and gets t += 4 in each pass. Similarly, 5*i can be kept in a variable p that is set to 0 before the outer loop and gets p += 5 at the end of each outer pass. True.(D) Every statement can be reached (the
if body runs for odd i), so there is no dead code to remove. False.The false statement is (D).
Answer: (D)
Keep practising
Topics in Compiler Design
Lexical AnalysisParsingSyntax-directed TranslationRuntime EnvironmentAssemblers, Linkers and Loaders
Intermediate Code GenerationYou are here
Matching