This page does not represent the most current semester of this course; it is present merely as an archive.
The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You’ll do this by solving a series of programming “puzzles.” Many of these puzzles are quite artificial, but you’ll find yourself thinking much more about bits in working your way through them.
Have trouble getting it to work on your machine? Try the following:
linux> sudo apt-get install libc6-dev-i386 gcc-multilib
Follow the instructions of your lab TAs. In general, you may work together however you wish as long as they do not say otherwise.
After this lab is over you will be given a homework assignment which is much like the lab (the same structure, but with different puzzles). The homework assignment you must do individually (no group work), so if you do work with others during lab make sure you understand everything your group does.
You should have received an email with your lab unix login in the past 8 days. If you think you did not, check your spam folders.
There is a problem with the password changing program yppasswd
in the current linux build in the labs. To work around it,
gnome-terminal
→ Enter)ssh labunix04.cs.virginia.edu
yppasswd
and follow the instructionsexit
to exit labunix and return to your computer.Start by copying datalab-handout.tar to a (protected) directory on a Linux machine in which you plan to do your work. Then give the command
linux> tar xvf datalab-handout.tar
(note: the linux>
represents the command prompt; do not type it when entering commands)
This will cause a number of files to be unpacked in the directory. The only file you will be modifying and turning in is bits.c.
Next make sure it works. Do
linux> cd datalab-handout linux> make
If that give you an error about inability to find
gcc
orpow
then they still haven't fixed a bug with the lab linux boxes. Trylinux> sed -i 's/gcc/gcc-4.8/g' Makefile
as a workaround, then try
make
again.
The bits.c file contains a skeleton for each of the programming puzzles. Your assignment is to complete each function skeleton using only straightline code for the integer puzzles (i.e., no loops or conditionals) and a limited number of C arithmetic and logical operators. The exact set of operators varies by puzzle.
Each puzzle is describes in comments in the bits.c file. Read the comments. All of them.
-
operator (which you may not use but is not hard to emulate).This lab is designed to look like the homework that will follow it. However, lab participation is more important than lab success, and lab work will be graded generously. This section describes how the homework will be graded.
perl driver.pl
program, which is described in the next section. You will get full credit for a puzzle if it passes all of the tests performed by driver.pl
, and no credit otherwise.
We have included some autograding tools in the handout directory — btest
, dlc
, and driver.pl
— to help you check the correctness of your work.
driver.pl
This is a driver program that uses btest
and dlc
to compute the correctness and performance points for your solution. It takes no arguments:
linux> ./driver.pl
Your instructors will use driver.pl
to evaluate your solution. If it doesn't work for you, it won't work for us either.
btest
This program checks the functional correctness of the functions in bits.c. To build and use it, either use driver.pl
or type the following two commands:
linux> make
linux> ./btest
Notice that you must rebuild btest
each time you modify your bits.c file.
You’ll find it helpful to work through the functions one at a time, testing each one as you go. You can use the -f
flag to instruct btest
to test only a single function:
linux> ./btest -f bitXor
You can feed it specific function arguments using the option flags -1
, -2
, and -3
:
linux> ./btest -f bitXor -1 7 -2 0xf
Check the file README for documentation on running the btest
program.
dlc
This is a modified version of an ANSI C compiler from the MIT CILK group that you can use to check for compliance with the coding rules for each puzzle. The typical usage is either to use driver.pl
or to invoke it directly:
linux> ./dlc bits.c
The program runs silently unless it detects a problem, such as an illegal operator, too many operators, or non-straightline code in the integer puzzles. Running with the -e
switch:
linux> ./dlc -e bits.c
causes dlc
to print counts of the number of operators used by each function. Type ./dlc -help
for a list of command line options.
The C code a ? b : c
can also be written ((-!a) & (c)) | ((-!!a) & (b))
(why?)
Don’t include the <stdio.h>
header file in your bits.c file, as it confuses dlc
and results in some non-intuitive error messages. You will still be able to use printf in your bits.c file for debugging without including the <stdio.h>
header, although gcc
will print a warning that you can ignore.
Use /*
and */
for comments, not //
The dlc
program enforces a stricter form of C declarations than is the case for C++ or that is enforced by gcc
. In particular, any declaration must appear in a block (what you enclose in curly braces) before any statement that is not a declaration. For example, it will complain about the following code:
int foo(int x)
{
int a = x;
a *= 3; /* Statement that is not a declaration */
int b = a; /* ERROR: Declaration not allowed here */
}
The included program fshow
helps you understand the structure of floating point numbers. To compile fshow
, switch to the handout directory and type:
linux> make
You can use fshow
to see what an arbitrary pattern represents as a floating-point number:
linux> ./fshow 2080374784
Floating point value 2.658455992e+36
Bit Representation 0x7c000000, sign = 0, exponent = f8, fraction = 000000
Normalized. 1.0000000000 X 2^(121)
You can also give fshow
hexadecimal and floating point values, and it will decipher their bit structure.
Have trouble getting it to work on your machine? Try the following:
linux> sudo apt-get install libc6-dev-i386 gcc-multilib
You should turn in your finished
bits.c on the submissions page. Remember, the lab is graded on effort so when time ends you are welcome to declare yourself finished and submit what you have.
Remember to replace <Please put your name and userid here>
with your name and computing id. If you worked with others or consulted sources other than the course website and textbook, list those there too. For example,
/*
* CS:APP Data Lab
*
* Luther Tychonievich (lat7h)
* Used http://dx.doi.org/10.1109%2FIEEESTD.2008.4610935
* Worked with Mystery Theater (mst3k)
* Tom Jefferson (tj1a) helped me understand what ~ does
Even if you work as a pair on developing just one bits.c, each partner should submit the file with their own name as the author and the others as collaborators.
For fun, we’re offering an optional “Beat the Prof” contest that allows you to compete with other students and the instructor to develop the most efficient puzzles. The goal is to solve each Data Lab puzzle using the fewest number of operators. Students who match or beat the instructor’s operator count for each puzzle are winners!
To submit your entry to the contest, type:
linux> ./driver.pl -u "Your Nickname"
Nicknames are limited to 35 characters and can contain alphanumerics, apostrophes, commas, periods, dashes, underscores, and ampersands. You can submit as often as you like. Your most recent submission will appear on a real-time scoreboard, identified only by your nickname. You can view the scoreboard by pointing your browser at
Wilkes is just some computer we have, and it might not be up all the time. If you can't see it, try again an hour later.
Note that you'll still need to submit to the submissions page even if you also submit to the “Beat the Prof” contest.