| CompOrg Spring 2005 Lab |
|   CompOrg Home |
This lab involves C programming and playing with pointers. You should start by getting a copy of the C program showbytes.c (this is the same file we used last lab). Here is a brief description of this program:
The #include <stdio.h> line tells the
preprocessor to include in this program all the function prototypes
and defines found in the stdio include file. This file is actualy
located at /usr/include/stdio.h - go ahead and look at
the file (it just contains some C code!).
The typedef tells the compiler about a new datatype -
the name of this data type is byte_pointer, it is
basically a new name for "a pointer to an unsigned char".
The function show_bytes will display the values of
len memory locations starting at the address
start. This function treats each of these memory
locations simply as a byte of memory, it displays each byte as 2 hex
digits.
The functions show_int, show_float and
show_pointer each call show_bytes with a pointer to some
memory and the appropriate length for each data type.
The main function right now simply tells
show_int to display the value of the integer 1.
You should make the following modifications to the program (test each modification to make sure it works!):
Change main so that it is passed argc
and argv - this way you can add code to get at command
line arguments.
To start - assume that there is a single command line argument
that is an integer, and that main should call
show_int and pass it the integer found on the command
line. We expect the program to be run like this: ./showbytes
17,
and we now want showbytes to show us the hex representation of the
integer value 17.
NOTE: Command line arguments are all strings, but
we need an int. You can use the function atoi to convert
from a string to an int (try "man atoi" for more information,
including what include file you should add to your progam when using
atoi). If you use atoi, you need to put #include
<stdlib.h> at the top of your program! Basic usage of
atoi will look something like this: i = atoi(argv[1]);
ANOTHER NOTE: Recall that argc and
argv include the name of the program. Basically
argc will always be at least 1 (the name of the program
is a command line argument) and that argv[0] points to
the name of the program. The first command line option will show up in
argv[1] - in this case this is where the integer
specified on the command line will be.
Now extend the program so that it can handle integers or
floating point numbers. You should use 2 command line arguments for
this,
if the first one is the string "-i" you should treat the second
command line argument as an integer. If the first command line
argument is the string "-f", treat the second argument as a floating
point value. Check out atof to convert from a string to
a floating point value, and call show_float once you
have a float value. IMPORTANT: If you use atof,
you must #include <stdlib.h> or it won't do what
you expect!
Once this is done you should be able to run showbytes like this:
./showbytes -i 17 ./showbytes -i 1234 ./showbytes -f 3.141593
NOTE: You need to look at argv[1] to
determine whether to show an int or a float. There are a number of
ways you can do this:
You could look at the character argv[1][1] (the second character in argv[1], which should be either 'i' or 'f'):
if (argv[1][1]=='i') ...
You could look at argv as a string and use strcmp
to compare it to "-i" and "-f". If you use strcmp you
must add #include <string.h> to the top of your
program, and you should read the man page (man strcmp)
to find out how it works:
if (strcmp(argv[1],"-i")==0) ...
Here is a simple program that shows the right and the wrong way to compare strings cmp.c
Support showing the value a pointer by supporting the "-p" command line option indicating that the second argument is a string. Remember that in C, a string is actually represented as an address of some ASCII characters terminated with a null byte (a byte whose value is 0).
Now you should be able to try ./showbytes -p "Hello
There"
You should notice that the last part (supporting the "-p" argument)
didn't actually show the ASCII values that make up the string (instead
we see the hex representation of the address of the string). Write a
function named show_string(char *) that will show the hex values
of each byte that makes up the string. Add support for this by
handling a "-s" option on the command line. You should now be able to
do something like this:
./showbytes -i 17 11 00 00 00 ./showbytes -f 3.141593 dc 0f 49 40 ./showbytes -p "Hello" 36 fb ff bf ./showbytes -s "Hello" 48 65 6c 6c 6f
You may find the function strlen useful (returns the
length of a string), if you use it make sure you
#include <string.h>