CS 270: Program assignment 1

The purpose of this assignment is to introduce you to systems programming in C, which is the language that we will be using throughout the semester. You must complete this program in ANSI C, not C99, C++ or C#. Your program must compile and run on your virtual machine, although you may develop it on any machine you like, such as on the MultiLab.


You will be graded on exact adherence to these specifications.

  1. Your C program must be called prog1.c, and the executable program must be called prog1.
  2. The program may be invoked on the command line like this:
    prog1 file.data
    where file.data can be any valid Unix file name.
  3. The file contains n+1 data chunks, each 4 bytes long. This file is in binary format, so you should not read it using string-based methods. The first chunk is a 4-byte signed integer (in little-endian order, which is the native order for your virtual machine) representing n, the number of remaining chunks. The file may contain an arbitrary amount of ignorable data past the last chunk.
  4. Your program should read all the chunks into an array of 4-byte unsigned integers. In C on your virtual machines, int is 4 bytes. The elements of the array are numbered starting at 0.
  5. Your program should then read commands from standard input (which could be the keyboard or a file, depending on how you invoke prog1), one command per input line. All values that your program prints should be directed to standard output (which could be the screen or a file, again depending on how you invoke prog1), in hexadecimal (8 characters, lower case), one result per line, unless indicated otherwise. The following list shows the valid commands and their meaning. Here, n and m are non-negative base-10 integers representing indices into your array of chunks, and b represents a non-negative base-10 integer representing a shift amount. Each command ends with a newline (\n).
    d n print element n in decimal (suppress leading zeros)
    x n print element n in hex
    o n print element n in octal (suppress leading zeros)
    0 n print the lowest order (first) byte of element n in 2 hex characters
    1 n print the second byte of element n in 2 hex characters.
    2 n print the third byte of element n in 2 hex characters.
    3 n print the fourth byte of element n in 2 hex characters.
    L n print the low 16 bits of element n in 4 hex characters.
    H n print the high 16 bits of element n in 4 hex characters.
    + n m print the sum of elements n and m, ignoring overflow
    * n m print the product of elements n and m, ignoring overflow
    & n m print the bitwise and of elements n and m
    | n m print the bitwise or of elements n and m
    ^ n m print the bitwise xor of elements n and m
    > n b print the result of right-shifting element n by b bits
    < n b print the result of left-shifting element n by b bits
    q quit with status 0; do not print anything
    There is one space separating operators (like 3 or |) and parameters.
  6. The program must not print anything other than what is specified above, except if it encounters an error. If there is an error, the program should print a helpful one-line message to standard error and exit with status 1.
  7. You must have a Makefile. The Makefile must be able to compile your program when you run
    make prog1
    You should start with the Makefile that you can get this way:
    wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/Makefile
    You may modify this file.
  8. The program must compile without any warnings with this command:
    gcc -Wall -g prog1.c -o prog1
    The Makefile we provide invokes the compiler exactly this way.
  9. The program must not assume that the data file exists, that it is well-formed, that the number of chunks is within any particular bound, or that the commands are well-formed. The program may terminate with an error message (and exit status 1) if it encounters an error. The program must terminate with exit status 0 if it finishes normally. It must not terminate with a fault.

You can get a sample data file, a working executable, and a program that creates data:

wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/testData
wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/workingProgram
wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/createData.pl
chmod +x workingProgram createData.pl

You might want to use the od ("octal dump") program to inspect the test data:

od -t d4 -t x4 testData

Useful information

The man program can tell you about useful programs and functions in the C library. For example, run this program to find out about the strtok() function:

man 3 strtok
The 3 means "section 3 of the manual", which is where the C library is documented. You can leave off the "3" and find all references to strtok(). You might find the following functions useful:
open fopen 
read fread
close fclose 
printf fprintf
scanf fscanf sscanf

Writing programs

For programs, correctness is an important criterion, but is by no means the whole story. Here are some suggestions for programming in general.

What to submit

Submit prog1.c, Makefile, and a README file. Do not submit other files. To create your submission, tar and compress all files that you are submitting. There is a recipe in the Makefile that we provide:

make bundle

The README file must be a text file, not a file in MS Word, PostScript, PDF, or any other non-text format. README should include at least your name, a list of the files you are submitting, a short description of the project, any known bugs, and a short description of your testing.

Upload your submittal via https://www.cs.uky.edu/csportal/. You may upload it as many times as you like; we grade only the last submission. Once we have graded a submission, we ignore later submissions.