Here are the new partnerships. The course webpage has a section on expectations for working with partners.
Both you and your partner should:
cd ~/cs31/labs
git clone [your_Lab5_URL]
Makefile sum.s sumtester.c QUESTIONNAIRE sum_C_goto_version swap.s compare.s comparetester.s swaptester.cFor more detailed instructions see the the using git guide.
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.
esp ----> # sum's stack frame: ebp-0x8 # space for one local variable (for res or for i) ebp-0x4 # space for one local variable (for the other one) ebp ----> # main's stack frame: ebp+0x8: # space for the parameter (x)Note: 8 and 0x8 are the same value. But be careful about the number representation you are using for literal values--be sure that you are using the value you think you are (e.g. 10 and 0x10 are not the same value).
movl 0x8(%ebp), %eax # load x into R[%eax] addl $1, %eax # x + 1
Tips:
# re-cast address as an int pointer (int *), then dereference the pointer * (gdb) print *(int *)(0x1234)Or:
# examine the value at 0x1234 as a decimal number (gdb) x/d 0x1234
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.
esp ----> # compare's stack frame: ebp ----> # main's stack frame: ebp+0x8: first parameter value (x) ebp+0xc: second parameter value (y)
(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.
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.