CompOrg Spring 2005

C Programming

Due Date: 2/13 (by 11:55PM)
Submit using WebCT: assignment HW1

Late Penalty: 25% per day. No submissions will be accepted after 2/17 at 11:55PM.

You may not share code with anyone else, you must work alone!
Feel free to discuss the project with anyone, just make sure you don't share code in any form! You are expected to write all the code you submit (do not submit code you find on the WWW!!!).

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.


Generating binary (ASCII encoded) from a char: print_binary_char

void 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.


Generating binary (ASCII encoded) from an int: print_binary_int

void 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!


Converting an ASCII encoded binary string to an int: get_int_from_binary

int 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).


Sample Test Code

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).

#include <stdio.h>              /* printf, etc */
#include <stdlib.h>             /* atoi */
#include <string.h>             /* strcmp */
 
/* Function prototypes */
 
void print_binary_char(char x);
void print_binary_int(int x);
int get_int_from_binary(char *s);
 
 
int main(int argc, char **argv) {
 
  char c;
  int i;
 
  if (argc<3) {
        printf("Invalid command line\n");
        printf("Usage: [-c decimalnum] | [ -i decimalnum ] | [ -b
        binarynum ]\n");
        exit(1);
  }
  if (strcmp(argv[1],"-c")==0) {
        c = atoi(argv[2]);
        printf("%d (0x%02x) : ",c,c);
        print_binary_char(c);
        printf("\n");
  } else if (strcmp(argv[1],"-i")==0) {
        i = atoi(argv[2]);
        printf("%d (0x%08x) : ",i,i);
        print_binary_int(i);
        printf("\n");
  } else if (strcmp(argv[1],"-b")==0) {
        i = get_int_from_binary(argv[2]);
        printf("%s: %d (%08x)\n",argv[2],i,i);
  } else {
        printf("Invalid command line\n");
        printf("Usage: [-b decimalnum] | [ -i decimalnum ] | [ -c
        binarynum ]\n");
        exit(1);
  }
  return(0);
}
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: