CS31 Lab 5: IA32

Due before 11:59pm Tuesday, Oct. 24 (after fall break)

This lab should be done with your partner:

Here are the Lab 5 partner lists.

Expectations for Working with Partners


Lab 5 Goals:

Contents:


Lab 5 Starting point code:

Both you and your partner should do:

  1. Get your Lab05 ssh-URL from the GitHub server for our class: CS31-F17
  2. On the CS system, cd into your cs31/labs subdirectory
    cd ~/cs31/labs
    
  3. Clone a local copy of your shared repo in your private cs31/labs subdirectory:
    git clone [your_Lab05_URL]
    
    Then cd into your Lab05-you-partner subdirectory.
If all was successful, you should see the following files when you run ls:

If this didn't work, or for more detailed instructions see the the Using Git page.

As you and your partner work on your joint solution, you will want to push and pull changes from the master into your local repos frequently.



Part 1. Writing a Loop in IA32

For this part, you will act like a compiler and generate IA32 instructions for part of a C program. In the file named loop.s finish the implementation of the following function in IA32:

// 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 loop(int x) {
  int res, i;
  res = 0;
  for(i=1; i <= x; i++) {
    res = res + i;
  }
  return res;
}
In mainloop.c is a main program that makes a call to this function. If you run make, you can build an executable, mainloop, that links in your loop.s solution. Use this to test your solution for correctness.

Hints and Requirements for Part 1
Part 2. IA32 Function with Pass-by-Reference Parameter

For this problem you will also act like a compiler and generate IA32 code translation for part of a C function. In the file named compareA.s, finish the IA32 implementation of the compareA function that conditionally changes the value of the int pointed to by x to the value of y.

void compareA(int *x, int y);
This function takes one int, x, passed by reference (the parameter points to the storage location of its argument), and one int, y, passed by value. 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 compareA would look like (see mainCompareA.c for another example):
int a, b;
a = 10;
b = 8;
printf("%d %d\n", a, b);  // prints: 10 8
compareA(&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
compareA(&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 compareA.s file already contains some IA32 instructions that set up up and tear down the stack frame associated with the function. You will add IA32 instructions to the body of this IA32 function to perform the compareA operation.

In mainCompareA.c is code to test your implementation. Run make to build an executable, mainCompareA, that links in your compareA.s solution and calls it. Use this to test your solution for correctness.

Hints and Requirements for Part 2
Extra Problem: Writing a Swap Function in IA32

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, you can try implementing a swap function in IA32:

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

In the swap.s file implement your function, and look at the swaptester.c program for how it calls to your swap function. Your solution to Part 2 should help you with this.

Lab Questionnaire

With every lab assignment is a file named QUESTIONNAIRE for you to fill out and submit with your lab solution. In this file you will answer some questions about the lab assignment. You should fill this out and submit it with your lab solution.

Submit

Before the Due Date

Only one of you or your partner needs to push your solution from one of your local repos to the GitHub remote repo. (it doesn't hurt if you both push, but the last pushed version before the due date is the one we will grade, so be careful that you are pushing the version you want to submit for grading):

From one of your local repos (in your ~you/cs31/labs/Lab05-partner1-partner2 subdirectory)

git push

Troubleshooting

If git push fails, then there are likely local changes you haven't committed. Commit those first, then try pushing again:
make clean
git add * 
git commit
git push
Another likely source of a failed push is that your partner pushed, and you have not pulled their changes. Do a git pull. Compile and test that your code still works. Then you can add, commit, and push.

If that doesn't work, take a look at the "Troubleshooting" section of the Using git page. You may need to pull and merge some changes from master into your local. If so, this indicates that your partner pushed changes that you have not yet merged into your local. Anytime you pull into your local, you need to check that the result is that your code still compiles and runs before submitting.