CS 31: Weekly Lab: Week 1

When working on the Macs in the ITS lab, you will need to remotely log into the CS system. To do this, first start X11 and then start an xterm or terminal. At the Unix shell prompt, ssh into your CS account:
$ ssh -Y your_user_name@cs.swarthmore.edu
You can open other windows on the CS machine by running xterm & or by running terminal or an X11 shell on the ITS machine and then ssh into the CS system as above.

See Connecting to the CS Department from Mac OS X for more details, and remotely connecting to CS. for more general information about remotely connecting to CS.

All work you do and software you run from the ssh session will be on a CS machine from within your CS account.

Once logged into our system, start by creating a cs31/weeklylab/week01 subdirectory and copy over some files from my public/cs31/week01/ directory into your subdirectory:

    cd
    mkdir cs31
    cd cs31
    mkdir weeklylab
    cd weeklylab
    mkdir week01
    cd week01
    pwd
    cp ~newhall/public/cs31/week01/* .
    ls

Goals for this week:

  1. Learn some basic C syntax and types
  2. Learn how to compile and run a C program
  3. Learn how to examine a running program's variable values using gdb
  4. Discover the parts of a computer

C basics

GDB intro

gdb is the gnu debugger for C and C++ programs. Over the course of the semester will will use gdb and learn more and more features. Today, we will learn just a few basics.

To use the debugger, we usually want to compile the program with the -g flag to add debugging information to the a.out file (this allows gdb to map machine code to C program code that the programmer understands).

$ gcc -g testprog.c
Next, we will run the a.out file inside the gdb debugger:
$ gdb ./a.out
The first thing we get is the gdb prompt (our program has not yet started). Typically we will set a break point at main. A breakpoint tells gdb to grab control at a certain point in the execution, in this case right before the first instruction in main is executed:
(gdb) break main
Next, we will enter the run command at the gdb prompt to tell gdb to start running our program:
(gdb) run
There are three other main gdb commands we will learn today, the first is next, which tells gdb to execute the next instruction and then grab control again:
(gdb) next  # this will execute the instruction x = 10
The second is list, which will list the code around where we are in the execution
(gdb) list
The final one is print, which lets us examine the state of program variables (and more generally any C expression we pass to the print command):
(gdb) print x    # print out the value of the variable x
10
(gdb) print (x - 4) # print out the value of the expression (x - 4)

try entering next a few more times, you can re-run from the beginning by entering the run command again, and then step through statement execution using next, and printing out runtime state using print.

When you are all done, type the command quit to exit gdb and get back the Unix prompt.

We will talk more about C and gdb over the course of the semester, and more about C types and gdb print this week in class.
Off my "Unix and CS Info Pages and Links" page are some C programming and gdb references that will be useful this semester:

The Computer

For the rest of lab, we are going to split you into small groups. Each group is assigned a computer that you can take apart. The goal is to find as many parts of a computer as you can. We have tools available to remove parts from your computer, and here are a few links that may be helpful:

Try to identify some of the following:

and then see what else you can find