Project ForgeSign up free
Compiler Design
Code Generation & Optimization
GATE 2007
Expert

In a simplified computer, the instructions are: OP Rj,Ri (performs Rj OP Ri, stores in Ri); OP m,Ri (performs val(m) OP Ri, stores in Ri, where val denotes the content of memory location m); MOV m,Ri (moves content of memory location m to register Ri); MOV Ri,m (moves content of register Ri to memory location m). The computer has only two registers, and OP is either ADD or SUB. Consider the basic block: t1=a+b; t2=c+d; t3=e-t2; t4=t1-t3. Assume all operands are initially in memory, and the final value must end up in memory. What is the minimum number of MOV instructions in the generated code?

  • A2
  • 3
  • C5
  • D6

Explanation

MOV a,R1; ADD b,R1 (R1=a+b=t1, no MOV needed for b since ADD can take a memory operand directly). MOV c,R2; ADD d,R2 (R2=c+d=t2). SUB e,R2 (R2 = e−t2 = t3, again no MOV needed for e). SUB R1,R2 (R2 = R1−R2 = t1−t3 = t4, using two registers directly, no MOV). Finally MOV R2,(result address) to store t4. Total MOVs: a, c, and the final store = 3.

Source: GATE CS 2007 (2007)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free