CS31 Lab 4: C Pointers and IA32


Due before 11:59pm Wednesday, Oct 9

This lab should be done with an assigned partner. Here is the lab4 partner list.

First run update31, which will create the directory structure for your lab 3 assignment:

$ update31
$ cd cs31/labs/04
$ pwd
/home/your_user_name/cs31/labs/04

Sharing files with your partner

Later this semester we will learn a better way to share files, but for now, you should either email or scp your shared solution to your partner's directory after a joint editing session. Some suggestions for using scp, email, or cut-and-paste to share code with your partner.

Lab 4 Goals:



Part 1. C Pointers
Experimental scientists collect data from experiments and often want to compute some simple statistical analyses over the set of experimental data. A useful tool would be a program that could compute these statistical results for any size data set (i.e. it would work for 10 data values or for 10 million without re-compilation).

For this program you will implement the program started in stats.c that takes a single command line argument, which is the name of a file of data values (floats, one per line), and computes and prints out a set of statistics about the data values.

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

./stats small.txt
Results:
--------
num values:         16
      mean:      1.812
    median:      2.000
   std dev:      1.031
unused array capacity: 4

./stats large.txt
Results:
--------
num values:         94
      mean:      1.161
    median:      1.000
   std dev:      0.788
unused array capacity: 66
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 stats.c is the starting point into which you will put your code. It contains the prototype for the getvalues function that you need to implement, and has some code to get the filename command line argument.

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 array capacity.

    The function will return an array of float values initialized to the values read in from the file, or NULL on error (like malloc fails or the file cannot be opened).

  2. It will then compute the mean (average), the median (the middle value), the standard deviation of the set of values and print them out.
  3. It will also print out information about the number of values in the data set and the amount of unused capacity in the array storing the 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).
  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 values.

Requirements and Hints



Part 2. Writing a Loop in IA32
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 Swap Function in IA32
In the file named swap.s finish the implementation of the a swap function that swaps two int values:
void swap(int *x, int *y);
This function takes two ints passed by reference (the parameters point to the storage location of their argument variables). After the call, the two int variables passed to swap should have their values swapped. A call to swap would look like (see mainswap.c for another example):
int a, b;
a = 10;
b = 8;
printf("%d %d\n", a, b);  // prints: 10 8 
swap(&a, &b);  // swap the values stored in a and b
printf("%d %d\n", a, b);  // prints: 8 10 
The swap.s file already contains some IA32 instructions that set up up and tear down the stack frame associated with the swap function. You will add IA32 instructions to the body of this IA32 function to perform swapping operation.

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

Hints and Requirements


Submit

Once you are satisfied with your solutions, hand them in by typing handin31 at the unix prompt.

Only one of you or your partner should run handin31 to submit your joint solutions

You may run handin31 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.