U.C. Berkeley Spring 2007, CS 61C

Lab 01: Simple C and Number Representation

Background

Goals

These lab exercises are intended to show you how to run a C program on the EECS instructional computers, to introduce you to the gdb debugger, and to get you thinking about the internal representations of numbers.

Reading

Accounts

You need to have an EECS instructional account (cs61c-*) in order to do your lab work. Most importantly, you will need to log in to this account in order to enroll in the class.

Don't forget to change your password and account information (gecos) once you have logged in. You can do this by SSH'ing to another computer with the following command:

  % ssh update

Note that the % represents the UNIX prompt.

Also, please setup your EECS instructional account email and newsgroup program, pine, by running the following command:

  % /share/b/adm/bin/instmail.pl

You can then run pine by typing the following at the prompt.

  % pine 

If you use pine as your email and your newsgroup client, please make sure that your name is not "Class Account" in the configuration of pine (accessed through Setup->Config).

You may also access your EECS instructional account email at Imail and the newsgroups at Webnews. Your login and password at these two sites are your EECS instructional account login and password.

Exercises

Setup

Copy the contents of ~cs61c/labs/01 to a suitable location in your home directory.

$ mkdir ~/lab
$ cp -r ~cs61c/labs/01/ ~/lab

Exercise 1: Simple C program

Fill in the blank in the following C program, also in output0.c, so that its output is a line containing "0" (zero). Don't change anything else in the program.

#include <stdio.h>
int main ( ) {
  int n;
  n = _____;
  printf ("%c\n", n);
  return 0;
}

To verify your solution, compile it and run the resulting binary:

$ gcc -o output0 output0.c
$ ./output0
0

Checkoff

Show your TA that output0 does output zero. 

Exercise 2: Debugger

Compile your solution to exercise 1 with the "-g" option. This causes gcc to store information in the executable program for gdb to make sense of it.

$ gcc -g -o output0 output0.c
$ gdb output0
Then single-step through the whole program by:

  1. setting a breakpoint at main
  2. giving gdb's run command
  3. using gdb's single-step command

Type help from within gdb to find out the commands to do these things.

Checkoff

Set the breakpoint at main, and show your TA how you run upto that breakpoint. 
Show your TA how you single step a program. 

Exercise 3: Octal Dump

The program mysteryout apparently produces a blank line as output when it is executed. Find out what it really prints using the od (octal dump) command. Running "man od" will give you information on how it works. Print the output of mysteryout (not the program mysteryout) as hexadecimal numbers (hex). You will learn more about hex numbers in next lecture. Hint: The output is a sequence of 5 bytes. Therefore, it is best to look at the output byte by byte using -t switch of od.

Checkoff

Show how to save the output of mysteryout to a file. 
Show the output of mysteryout in hexadecimal numbers. 
Show the output of mysteryout in decimal numbers. 

Exercise 4: The Biggest Integer

In Friday's class, we will discuss number representation. In particular, we are going to discuss unsigned integers and two's complement, the almost ubiquitous format for signed integers. Look at biggestInt.c. You may wish to read through the comments but at this point it is not critical that you understand exactly how the program works. Basically, it is a C program that will tell you some useful information about certain C data types. It does this by exploiting the fact that C does not check for overflow and wrap around conditions. Compile and run the program and answer the following questions:

  1. The first piece of information output by the program tells you the value of the most significant bit (MSB) of an unsigned int on your terminal. Based on this information, How many bits does the C data type unsigned int have?
  2. The second piece of information tells you the value of the largest positive signed long on your terminal. Based on this information, how many bits does the C data type long have?
  3. The third piece of information tells you the value of the most negative signed int on your machine. Based on this information, do the unsigned int and signed int have the same number of bits?
  4. 2's Complement number representations have one more negative number than they do positive numbers. That is, the most negative number does not have a positive counter-part. The final piece of information printed is the signed value of what you get when you try to negate the most negative value. Can you explain why this happens?

Checkoff

Answer to question 1  
Answer to question 2  
Answer to question 3  
Answer to question 4