CS208 Guest Lecture: Mastering C
Who am I?
I am currently a faculty member in Computer Science at the University of
British Columbia. I just started in September, but next year I'll be teaching
a course strikingly similar to this one. Prior to joining UBC, I was on the
faculty at Harvard for 25 years and the last few years, I taught a course
quite similar to this one
(CS61).
I love teaching undergraduates and particularly getting students excited
about the parts of a system that are hidden from most people.
And, I really love C!
(I also love creating interesting, 100% edible cakes. If you want a different
scavenger hunt, find my cake collection on
my web site.)
I'm also always looking for great graduate students, so if you ever find
yourself contemplating graduate school, I hope you'll think back on this
class and get in touch.
Today's Learning Objectives
- Become familiar with a non-trivial C program you did not write.
- Reverse engineer what a program is doing.
- Use UNIX man pages
- Have fun!
Things I am assuming
- You can read hexidecimal
- You know how things like integers, characters, and floating point are
represented in memory.
In-class Exercise
We're going to send you on a bit of a scavenger hunt -- inside a program!
You can find the program source code here.
And to make things easy to compile, here is a
Makefile. (You should be able to place both these
files in the same directory and and then just type "make" to produce the program
treasure.) If that doesn't work, let us know so we can get you unstuck.
Some handy info about the treasure program
- You run the program from the command line just by typing
./treasure.
- The first argument you pass to treasure indicates which of the treasure
functions to execute.
- Any other arguments you need to pass are for you to figure out.
- I recommend a combination of code reading, experimentation, and maybe
some lldb.
Your Mission
Pick a partner and begin exploring!
Run the program with different parameters to find the items in the list below.
There are two or more different ways to produce most of these results. How many
can you find?
- Sixty-one (this exercise is based on one from a course called CS61).
- An expired cow
- A precious stone
- A golden ratio
- An English word
- A greeting cat
- An infinite loop
- A number that is not a number
- A zero that is less than zero
- A professor
- A Shakespeare play
If you find them all, we will be impressed, but then feel free to tinker
with the program and add new treasures! Trade them with other groups.
Getting Started
- Find “sixty-one” using ./treasure 6. (If you run ./treasure 6 without arguments, it prints “6”. Can you find other arguments so that it prints something like “sixty-one”?)
- Find “sixty-one” using ./treasure 5.
- Find an expired cow using ./treasure 0.
- Find an expired cow using ./treasure 12.
- Find a precious stone using ./treasure 21.
- Find a precious stone using ./treasure 14.
- Find a greeting cat using ./treasure 16.
- Find a greeting cat using ./treasure 2.
Tips
We’ve hidden treasure in the program in many different ways. You’ll
make progress by looking at the code, by reasoning through its
behavior, and, importantly, by running experiments. You can even
change the code to see what happens! (But your answers should work
on the original code.) Also use the Web liberally. Don’t understand
a term? Search for it!
Don’t be afraid to treat the code like a scientist: run experiments!
We often try to understand code by reading it, but that can be
difficult, even for simple loops. If you get stuck, try instead to
get a feel for a function by observing its behavior. So try running
a treasure function with different arguments. What happens? Use the
results to develop a hypothesis about its behavior. Then check your
hypothesis against the code, and confirm or refute it by running
the code with different arguments.
Acknowledgements
Thanks to my brilliant colleague,
Eddie Kohler, for
inventing this assignment and writing the code.