CS31 Written Homework 1:
IA32 Practice

Due by 12:30 Friday Oct. 2
Hand in a hardcopy or printout of your solution outside Tia's office door by the due date. Please put in appropriate bin based on your Lab section.

Written assignments are not worth as much as lab assignments, but are designed to give you some extra practice on lecture material and on some of the type of questions you may see on course exams.

You are allowed to work with one or two classmates of your choice, but no groups larger than three (just split up if you have a large group). You may also work alone. However, I suggest working together to compare answers and submit a single joint solution.

If you work with a partner(s) only one of you should submit yor joint solution (and make sure all of your names are on it).

Start by copying over a file that you will print out with your solution to question 1:

cd 
cd cs31
mkdir written_hw
cd written_hw
mkdir hw01
cd hw01
cp ~newhall/public/cs31/hw01/* .
ls
README diss.s
If you work with partners you can share solutions by emailing the diss.s file back and forth (or printing out hard copies for each of you).
Problem Details
The README file contains a summary of these instructions.

You will solve 2 problems: the first can be written up in the diss.s file and then printed out, or you can print out diss.s and write your answers on the print out. To print on our system:

  lpr diss.s
You can insert page break characters (CNTL-L) into your ascii file to make good choices for page breaks in your print out. The diss.s has an example page break character between parts a and b.

Questions:

  1. Translate IA32 to C: In the diss.s file is a sequence of IA32 assembly code.
    1. annotate each instruction with what it is doing in terms expressions involving the local variables x,y,z and the registers. Assume that these variables are on the stack at locations:
        x is at R[%ebp]-4
        y is at R[%ebp]-8
        z is at R[%ebp]-12
      
    2. list the full C code translation of this code fragment You cannot use goto in this part, but you could think about a goto version as a first step if you are having trouble getting going on this.
    For this problem you can either print out the diss.s file and write your answers in with pen, or you can add your answers in the diss.s file in vim, and then print out the result.

  2. C to IA32: Convert the following C code fragment to equivalent IA32 assembly code:
            int hamster, bunny, gerbil;
            hamster = 12;
            bunny = 90;
            gerbil = hamster - bunny;
            while(hamster < bunny){
                    hamster *= 2;
                    gerbil += hamster;
            }
    
    You will do this in two steps, and show both steps:
    1. first, translate the loop to its equivalent C goto version
    2. next, translate your C goto version to IA32 For this part, assume that hamster, bunny and gerbil are local variables, located on the stack at the following addresses:
          hamster is at R[%ebp]-4
          bunny   is at R[%ebp]-8
          gerbil  is at R[%ebp]-12
      
      To receive partial credit annotate your IA32 code with comments describing to which part(s) of the C code it corresponds.

    Submit

    Submit a printout/hard-copy of your answers by the due date. Make sure your name(s) are clearly printed on what you hand in, and if you work with a partner(s), please only submit a single joint solution, and please staple all your pages together.

    To print out a file on our system:

    lpr filename