GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 54

Register allocation and MOV count in a basic block

Compiler Design · Code Generation & Register Allocation · Full GATE CSE 2007 paper

GATE 2007, Q54 2 marks · MCQ
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 54, GATE 2007: A computer has only two registers. Instructions OP Rj,Ri store Rj OP Ri in Ri; OP m,Ri store M[m] OP Ri in Ri; OP is ADD or SUB. MOV m,Ri loads memory into a register and MOV Ri,m stores a register to memory. Generate code for t1=a+b; t2=c+d; t3=e−t2; t4=t1−t3. All operands start in memory and the final result must be in memory. What minimum MOV count is intended?
Options:
(A) 2
(B) 3
(C) 5
(D) 6
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. Track the unusual operand order: SUB e,R2 computes e−R2, not R2−e. Under expression-preserving generation, retain t1 in one register while computing t2 and t3 in the other. The scope of permitted algebraic rewrites affects the literal minimum.

2. Derivation and interpretation. For expression-preserving code generation, three MOVs suffice: MOV a,R1; ADD b,R1; MOV c,R2; ADD d,R2; SUB e,R2; SUB R1,R2; MOV R2,t4. The specified source-minus-destination semantics gives R1=t1, R2=t2 then t3=e−t2 then t4=t1−t3. There are two loads to initialize the two independently computed subexpressions and one store, yielding the intended option B (3). This minimum presumes the usual task of generating code for the given expression structure. If algebraic reassociation is allowed, two MOVs suffice: MOV c,R1; ADD d,R1; SUB e,R1; SUB a,R1; ADD b,R1; MOV R1,t4. This computes t4=a−(e−(c+d))+b. Furthermore, if arbitrary arithmetic instructions may initialize a register, SUB R1,R1 makes zero; replacing the initial MOV c,R1 by SUB R1,R1; ADD c,R1 leaves only the final MOV. Hence the literal unrestricted minimum is not 3, and the scope assumption must be disclosed; B is an intended, conditional answer rather than an unconditional theorem.

Original paper, page 13