Part 2 (programming part):
due before midnight on Thursday, February 1
This lab is to be done alone (no partners). The assignment includes a written part and a programming part. The written part should be turned in at lecture and the programming part should be submitted via GitHub.
For CS31 lab assignments, we will use git to distribute starting point files, to save our progress towards a solution, and to submit our solutions. On subsequent labs you will use git to share code with your partner.
$ cd $ mkdir cs31 $ cd cs31 $ mkdir labs $ cd labs $ pwd # should be /home/[your-username]/cs31/labs/
$ 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
$ cd Lab1-[your-username] $ ls Makefile README.md lab1.cOpen README.md and lab1.c in your favorite editor (atom, vim, emacs).
$ make $ ./lab1
Answer the questions below showing your work and/or explaining your answer. If a question specifies a number of bits, your answer should be in a corresponding number of bits. For example, if the question asks you to add 4-bit values together your answer should be a 4-bit value not a 64-bit one. 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).
You can check your answers for correctness by writing a C program or by using gdb's print command. See the weekly lab page for details on using gdb.
value 1: 01011101 value 2: 01100101
Write a C program that 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 to support your answer. 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 ...For each question, you should be writing C code that demonstrates your answer is correct. If question 1 asked you to find the sum of 0x2 and 0x6, this would be insufficient:
printf("Question 1\n"); printf("0x2 + 0x6 is 0x8.\n");This would be better:
unsigned x, y, z; x = 0x2; y = 0x6; z = x + y; printf("Question 1\n"); printf("%x + %x is %x.\n", x, y, z);
int val; val = 97;
int main() { question1(); // call the question1 function question2(); ...
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 commit -m "lab1 is done" git push
You should run these commands not just when you are done but whenever you feel you have made progress towards a solution. Choose a commit message ("lab1 is done" in the example above) that describes what you have accomplished.
You can see if you have any uncommitted changes by running git status. 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 addPlease don't add, commit or push the executable files produced when you run make. If you run make clean before running git commands, this will help ensure that you do not do this by mistake...." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: lab1.c
For more git help see: the git Help page