CS360 Midterm Exam: October 8, 2003. Question 1

Match the following four pieces of C code to their proper assembler, as converted by an unoptimized compiler:

Procedure 1
int f(int a1, int *a2)
{
  int i;

  i = a1 + *a2;
  return i-1;
}
Procedure 2
int f(int *a1, int *a2)
{
  int i;

  *a1 = i + *a2;
  return *a1-1;
}
Procedure 3
int f(int *a1, int *a2)
{
  int i;

  i = a1[1] + a2[i];
  return i-1;
}
Procedure 4
int f(int a1, int *a2)
{
  int i;

  return g(a2, h(&a1))-1;
}

Answer A
f: push #4
   ld [fp] -> %r0
   ld [fp+16] -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp+12]
   ld [fp+12] -> %r0
   add %r0, %gm1 -> %r0
   ret
Answer B
f: push #4
  ld [fp+12] -> %r0
  ld [fp+16] -> %r1
  ld [r1] -> %r1
  add %r0, %r1 -> %r0
  st %r0 -> [fp]
  ld [fp] -> %r0
  add %r0, %gm1 -> %r0
  ret
Answer C
f: push #4
   ld [fp+12] -> %r0
   st %r0 -> [sp]--
   jsr h
   pop #4
   st %r0 -> [sp]--
   ld [fp+16] -> %r0
   st %r0 -> [sp]--
   jsr g
   pop #8
   add %r0, %gm1 -> %r0
   ret
Answer D
f: push #4
   mov #12 -> %r0 
   add %r0, %fp -> %r0
   st %r0 -> [sp]--
   jsr h
   pop #4
   st %r0 -> [sp]--
   ld [fp+16] -> %r0
   st %r0 -> [sp]--
   jsr g
   pop #8
   add %r0, %gm1 -> %r0
   ret
Answer E
f: push #4
  ld [fp+12] -> %r0
  ld [fp+16] -> %r1
  ld [r1] -> %r1
  add %r0, %r1 -> %r0
  st %r0 -> [fp]
  add %r0, %gm1 -> %r0
  ret
Answer F
f: push #4
  ld [fp+12] -> %r0
  ld [r0] -> %r0
  ld [fp+16] -> %r1
  add %r0, %r1 -> %r0
  st %r0 -> [fp]
  ld [fp] -> %r0
  add %r0, %gm1 -> %r0
  ret
Answer G
f: push #4
   ld [fp] -> %r0
   ld [fp+16] -> %r1
   ld [r1] -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp+12]
   ld [fp+12] -> %r0
   add %r0, %gm1 -> %r0
   ret
Answer H
f: push #4
  ld [fp+12] -> %r0
  ld [fp+16] -> %r1
  add %r0, %r1 -> %r0
  st %r0 -> [fp]
  ld [fp] -> %r0
  add %r0, %gm1 -> %r0
  ret
Answer I
f: ld [fp+12] -> %r0
   st %r0 -> [sp]--
   jsr h
   st %r0 -> [sp]--
   ld [fp+16] -> %r0
   st %r0 -> [sp]--
   jsr g
   add %r0, %gm1 -> %r0
   ret
Answer J
f: push #4
   st %r2 -> [sp]--
   ld [fp+12] -> %r0
   mov #4 -> %r1
   add %r0, %r1 -> %r0
   ld [r0] -> %r0
   ld [fp] -> %r1
   mov #4 -> %r2
   mul %r1, %r2 -> %r1
   ld [fp+16] -> %r2
   add %r1, %r2 -> %r1
   ld [r1] -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp]
   ld [fp] -> %r0
   add %r0, %gm1 -> %r0
   ld ++[sp] -> %r2
   ret
Answer K
f: st %r2 -> [sp]--
   push #4
   ld [fp+12] -> %r0
   mov #4 -> %r1
   add %r0, %r1 -> %r0
   ld [r0] -> %r0
   ld [fp] -> %r1
   mov #4 -> %r2
   mul %r1, %r2 -> %r1
   ld [fp+16] -> %r2
   add %r1, %r2 -> %r1
   ld [r1] -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp]
   ld [fp] -> %r0
   add %r0, %gm1 -> %r0
   ld ++[sp] -> %r2
   ret
Answer L
f: push #4
   st %r2 -> [sp]--
   ld [fp+12] -> %r0
   mov #4 -> %r1
   add %r0, %r1 -> %r0
   ld [fp] -> %r1
   mov #4 -> %r2
   mul %r1, %r2 -> %r1
   ld [fp+16] -> %r2
   add %r1, %r2 -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp]
   ld [fp] -> %r0
   add %r0, %gm1 -> %r0
   ld ++[sp] -> %r2
   ret
Answer M
f: push #4
   st %g0 -> [fp]
   ld [fp] -> %r0
   ld [fp+16] -> %r1
   ld [r1] -> %r1
   add %r0, %r1 -> %r0
   ld [fp+12] -> %r1
   st %r0 -> [r1]
   ld [fp+12] -> %r0
   ld [r0] -> %r0
   add %r0, %gm1 -> %r0
   ret
Answer N
f: mov #12 -> %r0 
   add %r0, %fp -> %r0
   st %r0 -> [sp]--
   jsr h
   st %r0 -> [sp]--
   ld [fp+16] -> %r0
   st %r0 -> [sp]--
   jsr g
   add %r0, %gm1 -> %r0
   ret
Answer O
f: st %r2 -> [sp]--
   push #4
   ld [fp+12] -> %r0
   mov #4 -> %r1
   add %r0, %r1 -> %r0
   ld [fp] -> %r1
   mov #4 -> %r2
   mul %r1, %r2 -> %r1
   ld [fp+16] -> %r2
   add %r1, %r2 -> %r1
   add %r0, %r1 -> %r0
   st %r0 -> [fp]
   ld [fp] -> %r0
   add %r0, %gm1 -> %r0
   ld [sp] -> 5r2
   ret
Answer P
f: push #4
   ld [fp] -> %r0
   ld [fp+16] -> %r1
   ld [r1] -> %r1
   add %r0, %r1 -> %r0
   ld [fp+12] -> %r1
   st %r0 -> [r1]
   ld [fp+12] -> %r0
   ld [r0] -> %r0
   add %r0, %gm1 -> %r0
   ret