With this assignment you will build a toy program that manipulates pointers to integers. You will develop the same main program fragment in both C and Assembler. Thus, you'll get two homeworks from this exercise, HW3-A and HW3-B.
The main ideas in these assigments are to understand the array-pointer duality in C and understand the relationship between pointers and addressing in assembly.
Getting Started
Here is a template C program to get you started. You'll need to fill in the blanks to complete.
#include
#define SZ 7
int* a[SZ];
int x, y, z;
void populate() {
x = 1;
y = 2;
z = 3;
a[0] = &x;
a[1] = &y;
a[2] = &z;
a[3] = a[0];
a[4] = a[1];
a[5] = a[2];
a[6] = a[3];
}
void printall() {
int i;
for (i = 0; i < SZ; i++) {
printf("a[%1d]=%1d, ", i, *(a[i]));
}
printf("x=%1d, y=%1d, z=%1d\n", x, y, z);
}
void add1each() {
// FILL THIS IN
// For HW4-A use C
// For HW4-B use GAS assembly.
}
int main(int argc, char* argv[]) {
populate();
printall();
add1each();
printall();
return 0;
}
Running
When this program runs, main() calls populate() to initialize the values for global variables, x, y, z, and the pointers in the array, a. Here is a diagram (that you'll need to complete) of those global variables.
HW4-A Instructions
1. Complete the diagram by filling in the lines representing pointer values.
2. Implement the body of the function add1each() in C. The function should add one to each referenced element of the array. Note that some elements are shared so will be incremented more than once.
3. Compile and run your program using the following, ?% script
4. Script started, file is typescript
5. % ./hw4a
6. a[0]=1, a[1]=...
7. a[0]=...
8. % exit
9. Script done, file is typescript
10. ??When you are done, the file typescript will contain a transcript of everything you typed with computer's responses.
Submit
For HW4-A, submit three things:
- Your diagram with pointers. This can be a PNG file--take a picture of your drawing, or an MS Word document or an SVG document from a drawing tool like Inkscape.
- Your C program file.
- Your typescript file.
- Do not submit ".o" files, nor the executable file
- Do not submit any other write-ups as MS word documents or any other write-ups, as the grader will just throw these away. If you need comments put them into your source code.
HW4-B Instructions
Implement the function add1each() using GAS in-line Assembly. You will want to pay particular attention to addressing modes used for operands.
The implementation of add1each() using GAS should perform exacly the same pointer dereferencing as your C program. Thus, when run, your GAS program should produce exactly the same results as with the C program. To verify this, save the session from the C program, run the GAS program and "diff" the results.
% cp typescript typescript-a
% script
Script started, file is typescript
% ./hw4b
a[0]=1, a[1]=...
a[0]=...
% exit
Script done, file is typescript
% diff typescript typescript-a
If no differences are reported then the output of the two programs exactly matches.
Submit
For HW4-B, One file containing:
- Your C program file with in-line assembly implementation of add1each().
- Do not submit ".o" files, nor the executable file
- Do not submit any other MS word documents or any other write-ups, as the grader will just throw these away. If you need comments put them into your source code.