CSE 410 20wi Homework 3 - Compiling

Out: Saturday, 22 January
Due: Friday, 28 January, 11:59 pm
Turnin: Gradescope - Please turn in as a pdf file

Introduction

Have a look at the following program, written in C. The first two lines simply provide the compiler with the types of functions printInt and fib and do not cause any assembler code to be emitted. (We have to declare the types of functions in C before any calls to them.) The function fib() is included in this file, but after the call to it. The function printInt is implemented only in assembler, so there is no C function with that name.

Here's the assembler code produced by compiling the C program above using the CSE 410 tools. In CSE410, the assembly implementation of printInt() is automatically appended to the assembler produced by the compiler before assembling to machine code, and another assembly language function, prologue(), is prepended. When run, execution actually starts in prologue.

The assembler code is formatted so you can print this assignment and write your answers to question in the blank spaces provided. Please turn in as a pdf file.

Questions

  1. For each instruction in the assembly code, indicate which line of of the C source program caused that line of assembler to be generated.
  2. Where is variable F, declared in the C program on line 3, stored at run time?
  3. In lines 40 and 41of the assembly program, what are the names in the C program of the variables referred to as -20(s0) and -24(s0)?
  4. How much stack space is used when this program runs? That is, given the value in register sp just before line 1 of the assembler code is executed, how much lower will the sp point before the program returns from main?
  5. [OPTIONAL] The recursion in fib() ends if n equals either 0 or 1. That allows fib() to behave reasonably if some user calls it with n==0. (In all other cases, the check for n==0 is never true.) What code might you add to the implementation of fib to make it even more resilient to things some calling code might do, either intentionally or by error?

Appendix A

In case you're interested, the C program is available to you at klaatu:/courses/cse410/22wi/hw3/h3-fib.c. On klaatu, you can copy it to your current working directory with:

    $ cp /courses/cse410/22wi/hw3-fib.c .
  
You can compile it to RISC-V assembler with:
    $CtoAsm hw3-fib.c
  
That will produce file hw3-fib.asm.

You can also copy the assembler code file, which is at klaatu:/courses/cse410/22wi/hw3/hw3-fib.asm. You can run the simulator on that code if you'd like.

Appendix B

The assembler code in the assignment was created by asking the compiler to turn off all optimizations when compiling. That causes it to produce the most easily understood code, which is generated basically by it looking at one source line at a time.

At the other extreme, the compiler is capable of compiling by looking at all the code available to it and finding optimizations. Below is the result of compiling the same program with optimizations on. In case you're interested, here's what it produces. It's pretty incredible what the compiler manages to do. (Remember that the compiler code must be sure that what it produces "always works" -- always gets the same result as the unoptimized code.)