1. Goals for this week:

  1. Practice Writing and Compiling Assembly Code

  2. Reminder of Debugging C programs using gdb and valgrind.

  3. Introduction to Part 2 of Lab 4.

2. Starting Point Code

Start by creating a week06 in your cs31/weeklylabs subdirectory and copying over some files:

$ cd ~/cs31/weeklylabs
$ mkdir week06
$ cd week06
$ pwd
/home/you/cs31/weeklylabs/week06
$ cp ~newhall/public/cs31/week06/* ./
$ ls
Makefile  README  dosomething.c  dosomething.s	prog.c

3. Writing IA32 Assembly

Together, we are going to write some IA32 assembly code, and then compile and test it out.

As we go, let’s refer to the IA32 IA32 instruction reference sheet.

First, open the prog.c file. You will see that it reads in an int value from the user and then makes a call to the int dosomething(int n) function that returns the result of some arithmetic operation on its parameter value. We are going to implement this function in IA32 assembly code in the dosomething.s file.

Lets look at the start of the dosomething function written in IA32 assembly:

vim dosomething.s

The assembly code in this file doesn’t really do much yet; the function just returns the value 3 (movl $3 %eax). Let’s try compiling and running it. The -m32 flag tells gcc to compile to IA32 code:

gcc -m32 -o prog prog.c dosomething.s
./prog

make    # or just type make to compile and note the -m32 flag

3.1. Write, compile, and run IA32 assembly code

We are going to implement the following function in IA32:

int dosomething(int n) {

  int x, res;

  x = n + 20;
  res = x*3;

  return res;
}

Open dosomething.s in vim and we will add IA32 assembly instructions to implement the body of this function (the stack setup and function return statement are already implemented).

vim dosomething.s

We will implement the body of the dosomething function in a few steps to try out accessing parameter and local variable space on the stack, and compile and run after each step to test out what we have done:

  1. Let’s start by loading the value of the parameter n into a register, let’s pick %edx.

    The parameter n is allocated on the stack at address: 8(%ebp)

  2. Next, see if you can get the function to return n. Compile and test it out.

    The function’s return value needs to be copied into the %eax register before the function returns (currently the function is set up to return the literal value 3, change it to return the value of n).

  3. Next, see if you can set res to 1 and get the function to return the value of res.

    res = 1;   // NOTE: assignment (=) STORES the value 1 in the res variable
    return res;

    There is space on the stack for the two local variables x and res, you can use -4(%ebp) for one and -8(%ebp) for the other.

  4. Now that we know how to access parameters and local variables and return a value from a function, try implementing the body of this function, compile and try it out to see if it works:

    x = n + 20;  // NOTE: assignment (=) STORES a value in the x variable
    res = x*3;   // NOTE: assignment (=) STORES a value in the res variable
    return res;

4. Debugging C programs: gdb and valgrind

You should be using the C debugging tools gdb and valgrind to help you debug Part 1 of Lab 4.

Refer to Weekly Lab 5 for examples on how to use gdb and valgrind, and see references in Section 6 for more information on both.

5. Lab 4 Part 2 Intro

Lets talk through Part 2 of the next Lab 4 Assignment, where you will implement the body of a function in IA32.

6. Handy Resources