This lab is to be done alone (no partners). The assignment includes a
written part that you will hand in at the beginning of class on
Thursday and a programming part that you will submit electronically by
Thursday evening (detailed submission instructions below).
Lab 1 Goals:
- To understand binary data representation of different types:
int, unsigned, float, char
- To be able to convert between decimal, binary and hexadecimal
representations
- To understand Binary arithmetic and apply it
- To understand bit-wise operations, and to use C bit-wise operators
to manipulate binary data
- Practice with basic C programing and gdb
Contents:
Getting Your Lab 1 Starting Point Code
For CS31 lab assignments, you will use
git
for managing lab starting point code distribution, for submitting
your lab solutions, and in subsequent labs for sharing code with your partner.
For each lab assignment you will do the following:
- Get the ssh-URL to your Lab git repository from the
GitHub server for our class:
CS31-F17
- Using your URL, and from your cs31/labs/ subdirecory, run git clone to
clone a local copy of your repo:
cd ~you/cs31/labs/
git clone [your-ssh-URL]
We are going to go through these above general steps in detail in lab this week:
- If you have not already done so,
create a cs31/labs subdirectory into which you will
clone your git repo containing your Lab 1 starting point code:
cd
cd cs31
mkdir labs
cd labs
pwd # should be /home/you/cs31/labs/
- Next, clone your Lab1 repo into your labs subdirectory.
Follow the directions off the git Help page and
grab the ssh-URL from your repo to clone:
git clone [your-ssh-URL]
You should have already completed the
one-time set-up steps for Lab 0, but if not do that first:
First-time set-up and generating ssh keys
- At this point you can cd into your local repo and start writing, compiling, and testing your lab 1 solution code:
cd ~you/cs31/labs/Lab1
ls
Makefile README.md QUESTIONNAIRE.md lab1.c
vim README.md
vim lab1.c
make
./lab1
Read over the
Git Commands section of the
git Help page, for a quick overview of commands you will use to submit your lab 1 solution (to add, commit
and push your code changes to the master repo):
git add lab1.c
git commit
git push
Part 1. Written Assignment
This part of the lab is a written assignment.
You can either write-up your solutions by hand, or using some word processing
software, or write them up in a file on our system using vim (or emacs or atom)
and print the resulting file to one of our printers.
To print a file created by vim (or emacs) on a CS lab printer
use either enscript or lpr:
$ lpr filename
$ enscript -2rG filename
If you write up your answers in vim (or emacs) make sure to not have
lines longer than 80 characters (explicitly hit the Enter key to
start a new line). If you have lines longer than 80 characters they
will either be cut off by the printer or wrapped strangely. One way
to ensure this is to work in a terminal 80 characters wide when you
run vim so you can see when the currently line is too long and starts
to wrap around. Typing in CNTRL-L in your file will print a page break:
hello1 # this will print on the first page
^L # this is typed in using CNTRL-L
hello2 # this will print on the second page
For these problems you are graded on showing how you applied the operation
or conversion method we described in class:
you must show your work or explain how you got the result to receive
credit. Check your
answers for correctness by either writing a C program to do some of the
computation and printing result values or by using gdb's print command.
See the weekly lab page for details on using gdb.
Answer the questions below showing your work and/or explaining your
answer.
For these questions, if a question specifies a number of bits, your
answer should be in a corresponding number of digits. For example, if
the question asks to add 4 bit values together your answer should be a
4 bit value not a 64 bit one. Also, assume that for signed values,
the high-order bit is the sign bit. For example, 1000 should be interpreted
as negative as a 4-bit signed value, but positive as an 8-bit signed
value (00001000).
- What is the largest positive value that can be represented with
a 2's complement 8 bit number? Explain.
- What is the largest positive value that can be represented with
an unsigned 8 bit number? Explain.
- convert the unsigned 8 bit binary value 10100110 to decimal. Show
your work.
- convert the signed 8 bit binary value 10100110 to decimal. Show
your work.
- For the following 8 bit binary values (show your work):
value 1: 01011101
value 2: 01100101
- What is the binary representation of the result of adding them together as unsigned 8 bit values? Does this operation result in overflow? If so, when?
- What is the decimal representation of the resulting addition if
the two values are unsigned 8 bit values?
- What is the decimal representation of the resulting addition if
the two values are signed 8 bit values?
- What is the binary representation of the result of subtracting the
second from the first as unsigned 8 bit values? Does this operation result in overflow? If so, when?
- Convert the following 2 byte binary numbers to hexadecimal, indicating
how each part is converted
(the binary values are shown with spaces between each 4 digits just to
make them easier to read):
- 0000 0110 0001 1111
- 1100 0101 1110 0101
- 1010 0111 1101 0110
- Convert the following hexadecimal numbers to binary, indicating how
you converted each digit:
- 0x23
- 0x852
- 0xc1a6
- 0xefab
- Convert the following decimal values to 8 bit binary and
to hexadecimal. Show your work:
- 12
- -36
- 123
- -123
-
Given the following 4 bit binary values, show the results of
each bit-wise operation, showing both the binary and decimal result
value for each (list the unsigned decimal value):
- 0110 | ~(1010)
- ~(0110 | 1010)
- 0111 & ~(1001)
- (1010 | 0000) & 1111
- 0011 ^ 1110
- 0111 << 2
- 0111 >> 2
Part 2. C Programming
You will write a single C program that when run, prints out answers
to each of the questions below.
For each question, print out a string that is your answer to the
question, and then print out some expressions and their results that
support your answer to the question. For example, the beginning of
a run of your program might look like this:
$ ./lab1
Question 1: my answer to question 1 is ...
This can be verified by examining the result of the expression ...
when x is the int value ... and y is ... the expression is ...
when x is ... and y is ... the expression is ...
Question 2: my answer to question 2 is ...
This can be verified by ...
Each answer should include printing out the value(s) of COMPUTATION(s)
that demonstrates your answer's correctness.
DON'T just print something like this:
printf("The answer to question 1, what 0x2 + 0x6, is 0x8\n");
Instead, have C code that computes the answer to show or to prove
that your answer is correct:
unsigned x, y, z;
x = 0x2; y = 0x6;
z = x+y;
printf("The answer to question 1, what %x + %x is %x\n", x, y, z);
For some questions, the code proving your answer correct may be as
simple as the example above. For others, however, you will have to
think about how to construct some arithmetic expressions that will
demonstrate the correctness of your answer.
You may want to try printing some values and expressions in gdb
in binary, hexadecimal, and decimal to help you figure out good values
to test in your C program to ensure you are considering all cases.
Answer these questions by writing a C program that prints out the
answer and prints out example expression(s) that support your answer
- Given the following variable declaration and initialization, how
many ways can printf display the value of x? Show all of them.
unsigned int x;
x = 97;
- What is the maximum positive value that can be stored in a
C int variable? (write code that both gives the answer and
proves/demonstrates your answer is correct)
- What is the maximum value that can be stored in a
C unsigned int variable?
- What arithmetic operation is equivalent to left shifting an
unsigned int value by 1?
- What arithmetic operation is equivalent to left shifting an
unsigned int value by 2?
- What arithmetic operation is equivalent to right shifting an
unsigned int value by 1?
Hints and Requirements for this part
- The answer to each question should be implemented as a separate function
called by main. For example, main might look like:
int main() {
question1(); // call the question1 function
question2();
...
And, you are welcome to add additional helper functions.
- Each answer should contain enough examples to support it.
If your answer is "false", this usually means just providing a
single counterexample that proves the statement false. If your
answer is "true", then find a few different cases that
demonstrate your claim. Do not, for example, enumerate every possible
int value. For most questions, it should be enough to have 4 or 5
examples to support your answer. Do not have more than 10 for any one
question.
- Examples in support of your answer must be computed by the C program.
For example, don't just print out the string "3 + 6 = 9". Instead write C code
that computes the expression and prints out its result,
like this:
int x, y;
x = 3;
y = 6;
printf ("%d + %d = %d\n", x, y, (x+y));
- Your C program, when run, should print out the answer to each question
in order, with supporting examples, in an easy to read format.
Use formatted printf statements, and do not print out lines that
are longer than 80 characters (break long output up into multiple
lines). Look at printf examples and documentation to use nice formatting
for any tabular output your program might produce.
- Remember that type is important in C, and that if you use different
formatting strings to printf, you can print out the same value as different
types (for example, if you print out a value using %u you may
see a very different value that if you print it out using %d).
If you are not seeing the values that you expect to see, check your
printf format string and use gdb to examine your running program.
- You should use good C programming style. See my
C Style Guide for examples of commenting code and avoiding line-wrapping.
- For C programming help, refer to the weekly lab examples and see the C programming help/links page.
- Use the Makefile to build your program.
Lab Questionnaire
With every lab assignment is a file named
QUESTIONNAIRE for you to fill out and submit with your
lab solution. In this file you will answer some questions about the lab assignment. You should fill this out
and submit it with your lab solution.
Submitting your Solution to Part 2
First make sure you have filled out the lab QUESTIONNAIRE.
To submit your Lab solutions, you must
run three git commands before the due date.
To submit your code, commit your changes locally using git add
and git commit. Then run git push to push your
local committed version to the remote master:
git add lab1.c
git add QUESTIONNAIRE
git commit -m "lab1 is done"
git push
You can run these any number of times before the due date to update
your solution answers in the remote master. In fact, it is good practice
to solve some of the lab, do an add, commit, and push, then solve more, ...
At the due date I will pull your solutions from the remote master git
repo and grade those. I can only see and grade changes to your Lab1 Git
Repo that you push. Git will only push changes you commit.
Git will only commit things you add.
It is also good practice to not modify your Lab source code (lab.c)
after the due date. If something goes wrong, I'll use the modification
date of the file in your local repo to determine which version of
your solution you submitted before the due date.
Some other information about add, commit, push
You can see if you have any changes from your local latest commit by
running
git status (this will tell you if you need to add and
commit something, and likely push too). For example, in this run
of
git status, the "Changes not staged for commit" output tells
me that I have made changes to lab1.c since my last add and commit, and
I need to add, commit, and push them to the repo:
git status
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: lab1.c
Also, please never add, commit or push files that can be created
from running gcc (i.e. executable files). If you run
make clean before running git
commands, this will help ensure that you do not by mistake add an
executable file to your git repo.
For more git help see: the git Help page