CompOrg Fall 2004 Lab

Lab #2 - C Programming.

9/15/2004

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!):


You should notice that the last part (supporting the "-s" 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.

You may find the function strlen useful (returns the length of a string), if you use it make sure you #include <string.h>


Links