CS360 Midterm Exam. March 14, 2013. Page Two


Question 3 -- Spilling

Part A: Define what spilling is. You may assume the machine model and assembly code that we use in class.

Part B: Explain why the following statement requires spilling: "k = (3 * i) + (4 * j);"

Part C: Explain why the following statement requires spilling: "k = a(5) + b(6);"

Part D: Do you spill before or after allocating local variables. Why?


Question 4 -- Assembler

To the right is a program in C. You do not have to convert this program to assembler. Instead, what I want you to do is show me the stack at the point where a() returns. You need to label every word on the stack, as I did in the lecture notes and as you have seen from the study material. You should make the following assumptions:
  • We are using the machine model and assembly defined in class.
  • When main() is called, the fp and sp both equal 0xfff4a0.
  • The jsr call in main() is at address 0x110c in the code.
  • The jsr b call in b() is at address 0x120c in the code.
  • The jsr a call in b() is at address 0x11f0 in the code.
  • You can compile this code without having to do any spilling.
I have an answer sheet with addresses drawn to help you.
int a(int i, int *jp)
{
  int k;

  k = i + *jp;
  return k + 1;
}

int b(int i)
{
  int j;

  j = 3;
  if (i > 10) {
    j += (a(i, &i) + 1);
    return j;
  }
  return b(i+5);
}

int main()
{
  int i;

  i = printf("a.out %d %d", b(8), 15);
}

Question 5 -- Recursive Directory Traversal

Your good friend Luigi has written a program to remove all of the a.out files in all of his directories. It is pictured to the right. When he runs it, it generates a segmentation violation. Since Luigi is one of those people who would rather have interpersonal interactions than actually debug his code, he shows it to you so that you can help him.

You look at Luigi's code and ask him if he's taken CS360. He reports that he didn't go to UT, so, "no." Figures. I want you to tell me at least five things that are wrong with Luigi's program. For each of these five things, describe what it is, how it manifests with an error, and how you'd fix it. You don't have to write code.

/* 01 */  #include <stdio.h>
/* 02 */  #include <stdlib.h>
/* 03 */  #include <dirent.h>
/* 04 */  #include <sys/stat.h>
/* 05 */  
/* 06 */  void rm_a_dot_out(char *dir)
/* 07 */  {
/* 08 */    DIR *d;
/* 09 */    struct dirent *de;
/* 10 */    struct stat buf;
/* 11 */  
/* 12 */    d = opendir(dir);
/* 13 */   
/* 14 */    for (de = readdir(d); de != NULL; de = readdir(d)) {
/* 15 */      if (strcmp(de->d_name, "a.out") == 0) remove(de->d_name);
/* 16 */      stat(de->d_name, &buf);
/* 17 */      if (S_ISDIR(buf.st_mode)) rm_a_dot_out(de->d_name);
/* 18 */    }
/* 19 */    closedir(d);
/* 20 */  }
/* 21 */  
/* 22 */  main()
/* 23 */  {
/* 24 */    rm_a_dot_out(".");
/* 25 */  }