| CompOrg Fall 2003 |
|   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 FreeBSD, we will test your code using gcc as the compiler.
You are to write a program that is capable of displaying the values of individual bytes in memory using various representations (hex, decimal, binary). Command line parameters will be used to tell your program what address to start displaying, and what format should be used to display each memory location. Initially your program should display the address (as 32 bit hex) followed by the value of that memory location. Each time the user presses the Enter key, your program should advance to the next memory location and display the address and value (using the same format). If the user presses 'q' your program should quit.
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 (argv[1]) will indicate the starting address your program should use, this will be provided in hexadecimal.
The second command line parameter will be a single character
that indicates what representation should be used to display the
value of memory locations. The only possible characters used will be 'b' (for
binary), 'x' (for hex) and 'd' for decimal
(unsigned 8 bit integer).
For example, the command line ./hw2 AB3240 x means
"show the value of memory starting at location 0x00AB3240 as 2 hex digits."
For the decimal format, the values must be in the range 0 to 255 (so treat memory as an unsigned 8 bit integer).
For hex format, the values must be exactly 2 hex digits (never only one digit, and never more than 2)!
For binary format, the values must be displayed by exactly 8 characters (each either a '1' or '0'). Note that there is not "binary" format provided by printf - you need to write code to output '1's and '0's yourself.
| Examples |
A few examples of the possible output for various command line
parameter values are shown below. Note that the value of any specific
memory location will likely be different for you!
'>' is the shell prompt.
In the examples below, the user is pressing the Enter key 8 times, then pressing 'q' and hitting Enter (the 'q' in each example is from the user, not the program).
|
|
|
| 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 HW2. Upload your hw2.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 hw2.c is all you need to submit. For future assignments you will 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 program with various valid command lines (we will not test with invalid options). We will not deduct points if your program SEGV's when we give it an invalid address (it's up to us to provide good addresses).
You get up to 25% for supporting each of the 3 formats (hex, decimal and binary) correctly. You get 25% for allowing the user to sequence through as many memory locations as desired (for handling user input and quitting only when 'q' is pressed).
| 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 hex address specified on the command line
from a string to something you can
actually use as an address (a value that can be assigned to a pointer
variable). There are many ways to do this - you can step through the
string one character at a time and basically accumulate the integer
value based on positional notation. You might also look at the
sscanf
Printing binary representations requires some work! One idea is determine the value of the MS bit of a byte, if it's a one, print the character '1', otherwise print a '0'. Then shift your byte to the left one place and do the same thing (check the MS bit of the result of the shift operation). Do this 8 times.