Homework 2

Due in class Wednesday November 16th

  1. Using the C compiler (gcc) installed on the lab machines, compile some small test functions into assembly language (gcc -S). Turn on optimizations (gcc -O -S). Then evaluate the compiled programs by these criteria:
    1. Are local variables kept in registers?
    2. If local variable b is live across more that one procedure call, is it kept in a callee-save register? Explain how doing this would speed up the following program:
      void g(void);
      void h(int);
      int f(int a) {
        int b = a+1;
        g();
        h(b);
        return b+2;
      }
      
    3. If local variable x is never live across a procedure call, is it properly kept in a caller-save register? Explain how doing this would speed up the following program:
      void f(int);
      void h(int y) {
        int x = y+1;
        f(y);
        f(2);
      }
      
  2. Generate assembly language for this program:
    void h(int, int);
    void m(int x, int y) {
      h(y,y);
      h(x,x);
    }
    
    Clearly, if arguments to m(x,y) arrive in registers r1 and r2, and arguments to h must be passed in r1 and r2, then x cannot stay in r1 during the marshaling of arguments to h(y,y). Explain when and how the C compiler moves x out of r1 so as to call h(y,y).
  3. For each of the variables a, b, c, d, e in the following C program, say whether the compiler allocates it in memory or a register, and why.
    int g(int, int *);
    int f(int a, int b) {
      int c[3], d, e;
      d = a+1;
      e = g(c, &b);
      return e+c[1]+b;
    }
    
  4. Compile the following program to assembly language:
    int leaf(int a, int b, int c, int d, int e, int f, int g, int h) {
      return a+b+c+d+e+f+g+h;
    }
    int foo(void);
    int nonleaf (int a, int b, int c, int d, int e, int f, int g, int h) {
        int x = foo();
        return a + b + c + d + e + f + g + h + x;
    }
    
    Then identify all the components of the calling sequence and explain what each line of assembly language does (including the pseudo-instructions).