CS 270 Programming Assignment #1

Due Tuesday September 9th (by 11:59pm)

Introduction:

Programming assignments and the associated homework questions are to be done individually. The homework questions listed in this assignment description should be answered in the README file.

For this assignment you will be a number detective. The goal is to learn how numbers are represented in the machine(s) you are using.

The Assignment

Implement the following functions, each in a separate file named based on the function (e.g. printHexForInt.c).
void printHexForInt( int x )    
    Print hex for a given integer to standard out

void printHexForFloat( float x )
    Print hex for a given float to standard out.  Hint 1: %x only works for
    integer types.  Hint 2: see printBinForFloat implementation that will be
    given in recitation.

void printBinForInt( int x )    
    Prints an integer to a string of bits.  Group the bits into sets of size
    four.  It is ok to assume that integers are represented with 32-bits, but
    that assumption should be tested with an assert.
    
void printBinForFloat( float x )
    Print a float to a string of bits broken into sign, exponent, and fraction.
The prototypes for all of the functions should be in a pa1.h file, similar to the pa0.h file you were given for PA0. Write a unit test for each function. Within each unit test, test that the function returns the same result as you calculate by hand for the conversions in the Homework Questions (see below). Since the functions in this assignment all print to standard out, you will need to visually inspect that the results are as expected. Each unit test should contain at least four different test cases written by you including the relevant examples in the below homework questions. Read the chapter on number representation to find some examples.

When you are finished your PA1 subdirectory will contain the following files:
    Makefile
    cs270.doxygen.config
    pa1.guideline

    pa1.h

    printBinForInt.c
    test_printBinForInt.c

    printHexForFloat.c
    test_printHexForFloat.c

    printHexForInt.c
    test_printHexForInt.c
    
    printBinForFloat.c
    test_printBinForFloat.c
Keep reading this writeup for instructions on how to get started.

README and Homework Questions

For this assignment the README file should contain your name, userid, and answers to the the following questions. Copy the question into the file and then type in the answer directly after the copied question.

If you answer the first four questions with respect to two or more machines, then make sure to indicate which answer corresponds to which machine.
  1. Are you doing your assignments on the school machines or at home? If at school what is the name of the machine you are using to answer these questions?
  2. Type gcc --version on the command line. What is the output?
  3. Does the machine you are on use the little endian or big endian representation? How did you determine that?
  4. Is the machine you are using 64 bit or 32 bit? How did you determine that?
  5. What are the ASCII codes for the characters in your family name?
  6. Show the representation of -305 (base 10) in the following representation schemes (assume 16-bit words): (a) sign magnitude, (b) one's complement, and (c) two's complement
  7. Show the representation of 284 (base 10) in the following representation schemes (assume 16-bit words): (a) two's complement and (b) hexadecimal.
  8. Show the representation of 101.5 (base 10) in the following representation schemes (assume 32-bit words): (a) binary float and (b) hexadecimal float.
  9. What is the range of base 10 integers that can be represented with 8 bits for each of the following representations? (a) sign magnitude, (b) one's complement, and (c) two's complement

Getting Started

The start of a Makefile, a doxygen configuration file (optional, try command make docs and open html/index.html), and the start of one of the assigned functions and its unit test have been provided in the public directory (~cs270/public/PA1-start/). Let a copy of the PA1-start subdirectory become your initial PA1 subdirectory. To accomplish this do the following at the command prompt:
    % cd ~
    (Just to make sure you are in your home directory.)
    % cp -r ~cs270/public/PA1-start ~/CS270/PA1


Now set up a subversion repository for PA1. See SVN Notes for directions for setting up a subversion repository and working directory.

Look carefully at the provided files. Type make inside of your PA1/ working directory to see the following output:
Compiling each C source file separately ...
gcc -c -g -std=c99 -Wall -O0 printHexForInt.c

Compiling unit test test_printHexForInt
gcc -g -std=c99 -Wall -O0 test_printHexForInt.c printHexForInt.o  -o test_printHexForInt
Executing unit test test_printHexForInt
./test_printHexForInt
===== Testing printHexForInt =====
42 in hex should be
	0x2A
	 output from printHexForInt = 
	IMPLEMENT ME!

-42 (as a 32-bit integer) in hex should be
	0xFFFFFFD6
	 output from printHexForInt = 
	IMPLEMENT ME!

Done with unit test test_printHexForInt

You will have to add file names to the C_SRCS, C_OBJS, and TESTS lists in the Makefile as you implement more functions and their corresponding unit tests.

Submitting your Assignment

Late Policy

Late assignments will be accepted up to 24 hours past the due date and time for a deduction of 20% and will not accepted past this period. One minute late is still late.


mstrout@cs.colostate.edu .... September 9, 2008