CS360 Midterm -- March 14, 2002. Question 1

Convert the program on the right into assembly code. Do not perform optimization -- just do a straightforward conversion:
int fp(int **graph, int from, int to)
{
  int *ptr;

  ptr = graph[from];

  while(*ptr > 0) {
    if (*ptr == to) return 1;
    if (fp(graph, *ptr, to) == 1) return 1;
    ptr++;
  }
  return 0;
}

Question 2

One hack that C compilers let you do is insert "asm" statements into your code. This inserts assembly code into the code at that point. It is horrible and unportable, but it makes this question easier. Suppose the program from question 1 were turned into the program on the right.

Explain exactly what the "asm" statements are doing, and how they change the execution of the program. Include why one would be tempted to do this in one's code.

int *hackvar;

int fp(int **graph, int from, int to)
{ 
  int *ptr;

  asm("st %fp -> [fp]);

  hackvar = ptr;
  return fprec(graph, from, to);
}

int fprec(int **graph, int from, int to)
{
  int *ptr;

  ptr = graph[from];

  while(*ptr > 0) {
    if (*ptr == to) {
      ptr = hackvar;
      asm("ld [fp] -> %fp);
      return 1;
    }
    (void) fprec(graph, *ptr, to);
    ptr++;
  }
  return 0;
}