| CompOrg Fall 2004 |
|   CompOrg Home   |   Assignment   |   Examples   |   How to submit   |   Grading   |   Hints   |   HW1 FAQ |
| Assignment |
This assignment involves the creation of C functions which can be compiled under Linux. We will test your code using gcc as the compiler.
You are to write C functions described below. It is essential that your C functions follow the names and arguments described below (we will write code that calls your functions!).
Your function definitions will be linked with our main
code, so you just need to write the code for the two functions listed
below. Feel free to write other functions as well, the only
requirement is that you include the functions listed below.
print_binary_charvoid print_binary_char(char x);
You need to write the function print_binary_char that
generates output to stdout that displays the binary representation of
8 bit character x. For example, if print_binary_char(0x35) is
called, the function should print the string "00110101".
Your function must work with any char value x! The easiest way to do this is to use binary logic operations to examine the bits of x one at a time. You might also find the shift operators will be useful.
print_binary_intvoid print_binary_int(int x);
You need to write the function print_binary_int that
generates output to stdout that displays the binary representation of
32 bit integer x. For example, if print_binary_int(0x01234567) is
called, the function should print the string:
00000001001000110100010101100111
Your function must work with any int value x! The easiest way to
do this is to use your print_binary_char function on each
of the bytes that make up the integer x. Be careful about byte ordering!
get_int_from_binaryint get_int_from_binary(char *s);
You need to write the function get_int_from_binary
that looks at the string s (which is an ASCII string with
exactly 32 characters each of which is either '1' or '0') and converts
it to a 32 bit C int (assume the data type int is 32 bits).
For example, if your function was called like this:
int x = get_int_from_binary("00000001001000110100010101100111");
The function should return the value 0x01234567 (which is 19088743 base 10).
We will be using our own main to test your functions,
so you should not include a main in the C file you
submit. Below is a sample main that you can use for
testing, this code can test each of your functions (by getting parameter
value from the command line).
|
| Examples |
A few examples of how to use the above main are shown
below, '>' is the shell prompt.
> ./sampmain -c 100 100 (0x64) : 01100100 > ./sampmain -c -100 -100 (0xffffff9c) : 10011100 > ./sampmain -i 1000 1000 (0x000003e8) : 00000000000000000000001111101000 > ./sampmain -i -1000 -1000 (0xfffffc18) : 11111111111111111111110000011000 > ./sampmain -b 11111111111111111111110000011000 11111111111111111111110000011000: -1000 (fffffc18) > ./sampmain -b 00000000000000000000001111101000 00000000000000000000001111101000: 1000 (000003e8) > |
| How to submit |
Submission of your homework is done using WebCT (webct.rpi.edu). Once you log in to WebCT and access the CompOrg site, you should click on assignments, then select HW1. Upload your hw1.c file to webct. Make sure your browser is supported before you attempt to upload a file (there is a link to "Check Browser"). Dave will demonstrate submission using WebCT in class.
For this assignment, the file hw1.c is all you need to submit. For future assignments may need to submit multiple files...
Don't send compiled code, only send your C program!
Multiple Submissions: You can resubmit as many times as you want, WebCTwill make sure we get the last file you submit.
| Grading |
Grades will be determined by testing your functions with various
parameter values. We will not test get_int_from_binary with
anything other than a 32 character ASCII encoded binary strings (your code can assume
that you are passed strings that contain the characters '0' and '1'
only, and are the appropriate length).
Each function is worth 33% of the grade.
| HINTS: |
The simplest approach is to simply manipulate bits (and groups of bits), rather than trying to use arithmetic.
You should assume that your code is to be run on a machine that uses a small-endian byte ordering. If you do something like this:
int i; char *s = (char *)&i;
You must remember that s[0] is not the MS byte in the int, (s[3] would actually hold MS byte).