CS31 Lab 4:
C Pointers and IA32

Due before 11:59pm Tuesday, Oct 6

This lab should be done with your assigned partner:
lab A partner list
lab B partner list

This is also the last lab for which we will assign partners. For all remaining labs, you will work with a partner of your own choosing.

Your partner for lab 5 and beyond must be in the same lab section as you. If you have a partner, both you and your future partner should send email to your lab instructor (Tia for Lab A and Bryce for Lab B) telling us who your lab 5 and beyond partner is. If you would like our help finding a partner, please email your lab instructor and we will help you find a partner before the lab 5 assignment.

Important: Send us your lab5 and beyond partner choice by Sunday noon so that we can help singles find partners.


Lab 4 Goals:

There are three parts to this lab assignment:

  1. Part 1: C program with pointers and dynamic memory allocation
  2. Part 2: IA32 Loop
  3. Part 3: IA32 C-style pass-by-reference
  4. Extra Challange: An Extra Challenge (this is NOT a required part of lab 4, and you should not try it until you have completed the first three required parts)

Lab 4 Starting point code:

Both you and your partner should do:

  1. Get your Lab04 ssh-URL from the GitHub server for our class: CS31-f15
  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_Lab04_URL]
    
    Then cd into your Lab04-you-partner subdirectory.
If all was successful, you should see the following files when you run ls:
Makefile    grades.c     grades2.txt          mainCompareA.c  readfile.h
README.md   grades0.txt  loop.s               mainloop.c
compareA.s  grades1.txt  loop_C_goto_version  readfile.c
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. C Pointers
Educators often want statistical analysis of a set of exam scores. These types of analyses could be done for an exam from a single class or an exam from an entire school district. A useful tool would be a program that computes statistical results for any size data set (i.e. it would work for ten data values or for thousands without re-compilation).

You will implement the program started in grades.c that takes a single command line argument, which is the name of a file of grade values (floats, one per line), and computes and prints out a set of statistics about the data values and prints out a historgram of the grade distribution.

The starting point code comes with three input files that you can use to test your solution.

This program includes the readfile library code that it links in as well as linking in the math library: use the makefile to compile. You can see how the executable is built from a .c, a .o, and explicitly linking in the math library (-lm), by reading the Makefile.

In grades.c is the starting point for your program. It contains a prototype for the getvalues function that you need to implement, and it has some code in main that copies the filename command line into a string local variable for you.

Your program should do the following:

  1. Make a call to getvalues, passing in the filename of the file containing the data values, and passing in two values by reference: the address of an int variable to store the size of the array (number of values read in); and the address of an int variable to store the total capacity of the array.

    getvalues returns an array of float values initialized to the values read in from the file, or returns NULL on error (like if malloc fails or if the file cannot be opened). It dynamically allocates the array it returns and uses a doubling re-allocation algorithm as it needs more space (see the Requirements section for details about this algorithm).

  2. It then computes the max and min grades, the mean (average), the median (the middle value), and the standard deviation of the set of grade values. It prints out the total number of grades and each of these computed statistics.

  3. It creates a histogram from the set of grade values, and prints out the grade histogram.

    A note on histogramming values: Each histogram bucket counts the number of exam scores in a particular range: 0-9, 10-19, 20-29, etc. Exam grades that have a fractional component (e.g. 89.75) should be counted as being in a histogram bucket range based on their whole number part only (e.g. 89.75 is a grade in the 80's not a grade in the 90's).

  4. It prints out information about the amount of unused capacity in the array storing the grade values.

Statistic Functions

The statistics you need to compute on the set of values are the following:
  1. mean: the average of the set of values. For example, if the set is: 5, 6, 4, 2, 7, the mean is 4.8 (24.0/5).
  2. median: the middle value in the set of values. For example, if the set is: 5, 6, 4, 2, 7, the median value is 5 (2 and 4 are smaller and 6 and 7 are larger). If the number of values is even, just use the num/2 value as the median.
  3. stddev: is given by the following formula:


    Where N is the number of data values, X_i is the ith data value, and X-hat is the mean (average) value.

Sample Output

Here is Sample Output from a working program run on the three input files that were included with the starting point code. You should also test your program on other input files that you create.

Requirements and Hints



Part 2. 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



Part 3. Writing a Pass-by-Reference Function in IA32
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 changeA.s finish the IA32 implementation of the changeA function that changes the value of the int pointed to by x to the value of y.
void changeA(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. After the call, if the value of the fist argument is greater than the value of the second argument, it should be set to the second. Otherwise, it is unchanged by the call. A call to changeA would look like (see mainchangeA.c for another example):
int a, b;
a = 10;
b = 8;
printf("%d %d\n", a, b);  // prints: 10 8 
changeA(&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 
changeA(&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 changeA.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 "change A" operation.

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

Hints and Requirements



Extra Challenge to Part 1

This is NOT a required part of the Lab 4 assignment. DO NOT try this until you have finished all regular parts of this lab (Parts 1-3). Extra Challenge parts are just a way to try something with an added challenge for fun. We will award a nominal amount of extra credit points to your lab score for successful completion of these (and it is all or nothing grading), but they will really have little to no impact on your lab grade: it is much much better to finish the required parts completely and to finish them well, than to try the extra challenge and not submit complete required parts.

Lab 4 Extra Challenge Part

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/Lab04-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:
git add grades.c
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.