CS31 Lab 5: Assembly Code

Due before midnight Tuesday, March 6

Here are the new partnerships. The course webpage has a section on expectations for working with partners.


Lab 5 Goals:

Contents:


Getting Starting Point Code

Both you and your partner should:

  1. On the CS system, cd into your cs31/labs subdirectory
    cd ~/cs31/labs
    
  2. Get your Lab5 ssh-URL from the GitHub server for our class: CS31-s18
  3. Clone a local copy of your shared repo in your private cs31/labs subdirectory:
    git clone [your_Lab5_URL]
    
You should see the following files in your repo:
Makefile       sum.s                sumtester.c
QUESTIONNAIRE  sum_C_goto_version   swap.s
compare.s      comparetester.s      swaptester.c
For more detailed instructions see the the using git guide.

Part 1. Writing a Loop in IA32

For part 1 you will act like a compiler and generate IA32 instructions for part of a C program. In the file named sum.s finish the IA32 assembly code implementation of the function with this C implementation:

// this function computes the sum of the first x positive int values
// x: the top of the range of values to sum
// returns: the sum of the values 0 to x inclusive
int sum(int x) {
  int res, i;
  res = 0;
  for (i=1; i <= x; i++) {
    res = res + i;
  }
  return res;
}
sumtester.c makes use of the sum function. When you run make, it will build an executable, sumtester, that links in the assembly code you wrote in sum.s. Use this executable to test your solution for correctness.

Hints and Requirements for Part 1
Part 2. IA32 Pointer Parameters

For this problem you will again act like a compiler and translate a C function into IA32 assembly code. In compare.s you should finish implementing the compare function, which conditionally changes the value of the int pointed to by x to the value of y.

void compare(int *x, int y);
This function takes one int pointer, x, and one int, y. If the int pointed to by x is greater than y, it should be set to y. Otherwise, it should be unchanged by the call. A call to compare might look like this:
int a, b;
a = 10;
b = 8;
printf("%d %d\n", a, b);  // prints: 10 8
compare(&a, b);           // changes a's value to b's if a > b
printf("%d %d\n", a, b);  // prints: 8 8  (a's value was changed to b's)
a = 3;
b = 11;
printf("%d %d\n", a, b);  // prints: 3 11
compare(&a, b);           // changes a's value to b's if a > b
printf("%d %d\n", a, b);  // prints: 3 11  (a's value wasn't changed to b's)
The compare.s file already contains IA32 instructions that set up and tear down the function's stack frame.

testcompare.c contains code to test your implementation. Run make to build an executable, testcompare, that links in your compare.s solution and calls it. Use this to test your solution for correctness.

Hints and Requirements for Part 2
Extra Problem: Swap

(This is not a required part of the lab assignment. Do not attempt this until you have successfully completed the other two parts.)

As extra practice with pointer parameters, try implementing swap in IA32:

/*
 *  swap: exchange the values pointed to by x and y
 *     x: a pointer to one int value
 *     y: a pointer to another int value
 */
void swap(int *x, int *y);

Implement the function in swap.s. Look at swaptester.c to see how it might be used.

Submitting

There is a QUESTIONNAIRE file for you to fill out and submit with your lab solution. Your answers to the questionnaire will not affect your grade.

Only one member of your partnership needs to git push your solution before the deadline. We will grade sum.s, sum_C_goto_version, and compare.s. If you do the extra challenge we will also grade swap.s.