CS611
Fall 2002
Programming Assignment 4
Due Sunday October 27


Write a C function that will divide two 40-bit 2's complement values. The routine should also compute the remainder of the division.

The function should be called divide40. The function returns void and takes four void* arguments. The first argument is an input argument and points to the dividend. The second argument is an input argument and points to the divisor. The third argument is an output argument and should be set to the division result. The fourth argument is an output argument and should be set to the remainder result.

Each input argument points to the first (low address) byte of five consecutive bytes storing the 40-bit value in Little Endian format. The output arguments will point to five-byte buffers where the output values should be stored in Little Endian format.

You do not need to error check the arguments. You may simply assume they contain valid pointers.

You also do not need to do anything special for division by zero. If the divisor is 0, then anything can be returned for the division and remainder results.

The results from dividing a by b are defined by this formula:
(((a / b) * b) + (a % b)) == a

So,
5 % 3 == 2 and 5 / 3 == 1
5 % -3 == 2 and 5 / -3 == -1
-5 % 3 == -2 and -5 / 3 == -1
-5 % -3 == -2 and -5 / -3 == 1

You must implement this function using explicit bit manipulation:

  1. Pack the 40-bits of the dividend argument into the significand bits of an IEEE double-precision floating-point value. Normalize the value. Set the exponent and sign bits.

  2. Pack the 40-bits of the divisor argument into the significand bits of an IEEE double-precision floating-point value. Normalize the value. Set the exponent and sign bits.

  3. Divide the dividend by the divisor using the C division operator for doubles.

  4. Discard the fractional part of the division result.

  5. Compute the remainder using C operations on doubles.

  6. Extract the 40-bit integer result from the floating-point division result.

  7. Extract the 40-bit integer result from the floating-point remainder result.

To test your function construct a "test rigging" that will allow you to enter values, call divide40 to compute the division and the remainder, and then print the results. This can be a simple main program, similar in spirit to what we used for Program 1.

Grading will be based upon completed, correct functionality:

Your program will be graded primarily by testing it for correct functionality. However, you may lose points if your program is not properly structured or adequately documented. At the top of your "test rigging" file be sure to include a comment that explains how you use it.

Put your C code in a file called divide.c. Put your "test rigging" in a file called test.c.

Your assignment should be submitted for grading from a CIS Linux machine (e.g. turing.unh.edu). To turn in this assignment, type:
~cs611/bin/submit prog4 divide.c test.c

Do not turn in any other files!

Submissions can be checked from a CIS Linux machine by typing:
~cs611/bin/scheck prog4

To receive full credit for the assignment, you must turn in your files prior to 8am on Monday October 28. Late submissions will be accepted at the penalty of 5 points per day up to one week late.

Remember: as always you are expected to do your own work on this assignment. Copying code from another student or from sites on the internet is explicitly forbidden!

If you developed your code on a DOS/Windows system, be sure to appropriately transfer your files to a CIS Linux system before submitting them. You need to convert the DOS ASCII file format to UNIX format. If you need help with this, please see me.


Last modified on October 16, 2002.

Comments and questions should be directed to hatcher@unh.edu