This lab should be done with your partner:
Here are the Lab 5 partner lists.
Expectations for Working with Partners
Both you and your partner should do:
cd ~/cs31/labs
git clone [your_Lab05_URL]Then cd into your Lab05-you-partner subdirectory.
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.
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.
esp ----> # loop'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
Help:
# re-cast address as an int pointer (int *), then dereference the pointer * (gdb) *(int *)(0x1234)Look at the week 6 Wednesday lab page and in-lab examples for how to use gdb to examing code at the IA32 instruction level.
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.
esp ----> # compareA's stack frame: ebp ----> # main's stack frame: ebp+0x8: first parameter value (&a) ebp+0xc: second parameter value (b)
movl 0x8(%ebp), %eax # load x into R[%eax] addl $1, %eax # x + 1
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.
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.
From one of your local repos (in your ~you/cs31/labs/Lab05-partner1-partner2 subdirectory)
git push
make clean git add * git commit git pushAnother 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.