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:
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:
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.
Comments and questions should be directed to hatcher@unh.edu