| CompOrg Fall 2002 Homework #2 |
|   Course Syllabus   |   CompOrg Home   |   Assignment   |   Examples   |   How to submit   |   Grading   |   Hints   |   HW2 FAQ |
| Assignment |
This assignment involves the creation of a C program which can be compiled and run under Unix (any of Linux, FreeBSD, Cygwin are fine, we will test with the compiler gcc).
You are to write a program that is capable of printing out either the hex or binary representation of various numeric values. Command line parameters will be used to tell your program what representation should be used (hex or binary), and what value to display. Your submission must include a single C file named "hw2.c", and it must be written to work with the command line parameters described below. We will use an automated system to run your program, so if you don't follow the instructions below we won't be able to grade your program.
We will compile your program with the command line "gcc -o
hw2 hw2.c" (the resulting executable program will be named
hw2). The command line parameters are listed below:
The first command line parameter will be a single character
that indicates what representation should be used to display the
value. The only possible characters used will be 'b' (for
binary) and 'x' (for hex).
The second command line parameter will indicate whether the
the value is an integer ('i') or a floating point number ('f').
The third command line parameter is the actual value
(specified as a decimal number) that we want you to print
out. Integer values should be treated as signed int (32 bit 2s
complement integers). Floating point values should be treated
as the C type float, which is IEEE single precision
(32 bits long).
For example, the command line ./hw2 x i 13 means
"print the hex representation of the int with the value 13".
The output format for the hex representation of either an integer
or a float (which is single precision IEEE) should simply be an 8
character hex string followed by a newline (\n). Use
capital letters for the hex digits (use 'A', 'B', not 'a', 'b').
The %X format for printf will do all the work!
The output format for the binary representation of integers should be a string of 32 binary digits (bits). You need to actually print a '1' or a '0' for each bit. There is no binary format for printf, so you need to write code to print a sequence of '1's or '0's to match the bits in value you are given.
For floating point numbers, you need to print 32 bits, but you
should put a single space after the 1st bit (which represents the
sign bit in IEEE format), then print the 8 bit exponent followed by
a space, then the 23 bits of the significand. So the output of the
floating point value 7 would be: 0 10000001
11000000000000000000000
| Examples |
A few examples of the output expected for various command line
parameter values are shown below. '>' is the shell prompt.
|
|
|
|
|
| How to submit |
Submission of your homework is via email, the general idea is to send an email message with your file as an attachment. There is an automated email submission system that will respond to your submission right away, so you will have a record that we got your file.
All projects must be submitted via email to comporg-submit@cs.rpi.edu. The subject line of the submission message should contain a single number '2' indicating the HW number. You must include your C program named hw2.c as an attachment.
IMPORTANT! Make sure your email message includes your full name, we can't record your grade unless we know your name!
Don't send compiled code!
You can expect a return email indicating receipt of your project submission immediately. This receipt will include a list of all the files that were successfully extracted by the submission script - please look over the receipt carefully to make sure your submission worked.
Multiple Submissions: You can resubmit up to 10 times for each project, we will always grade the last submission received unless you tell us otherwise.
| Grading |
Grades will be determined by testing your program with various valid command lines (we will not test with invalid options). Each combination of data type and output representation format will be tested with a few values, each combination counts for 25% of the grade.
| HINTS: |
You need to be able to handle command line parameters in C. Here is some sample code that prints out all the command line parameters received (to remind you how argc and argv work):
#include <stdio.h> /* for printf */
int main(int argc, char **argv) {
int i;
/* first print the number of command line arguments */
printf("There are %d arguments\n",argc-1);
/* go through all the args received and print each
IMPORTANT: all of these are strings (always!)
if you expect an integer - you have to convert from
a string to an integer
*/
for (i=0;i<argc;i++) {
printf("argv[%d] is \"%s\"\n",i,argv[i]);
}
}
You need to convert the last command line argument from a string to
either an int or a float. You can use atoi and
atof to accomplish this - check the man pages for
the details.
You may bump in to some problems when trying to print a float.
For example, if you try to print a float using %X with
printf - the compiler will convert your 32-bit float to a 64 bit
double before giving it to printf, and the result will not be the
right output. You need to find a way to give printf the 32 bits
that represent your float value, but give it to printf as if it
was an int (or unsigned).
Example of the problem: the following code:
float x = 5.0.;
printf("%X\n",x);
will generate the output:00000000 which is not
correct. The problem is that the compiler will convert x to a double
(double precision IEEE) before giving it to printf.
There are ways to actually send printf exactly the bits you want to send, and you can always write your own code to generate the hex output.
Printing binary representations requires some work! One idea is determine the value of the MS bit of a word, if it's a one, print the character '1', otherwise print a '0'. Then shift your word to the left one place and do the same thing (check the MS bit of the result of the shift operation). Do this 32 times.