This lab assignment includes a written part that you will hand in at the beginning of your lab section on Wednesday and the programming part that you will submit electronically by Tuesday evening via GitHub.
Before starting the lab, first clone your Lab1 repository into ~/cs31/labs.
You can either write-up your solutions by hand or write them up in a text editor and print the resulting file to one of our printers. If you type your answers, 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 will be 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).
value 1: 01011101 value 2: 01100111
For this part, 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. DO NOT 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 constructing 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 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:
int main() { question1(); // call the question1 function question2(); ...You are welcome to add additional helper functions.
int x, y; x = 3; y = 6; printf ("%d + %d = %d\n", x, y, (x+y));
Please remove any debugging output prior to submitting.
To submit your code, simply commit your changes locally using git add and git commit. Then run git push while in your lab directory. For more info, see the section on Using a shared repo on the git help page.
If the exercise in this lab is too easy for you, here's an additional challenge: write a function which takes an unsigned integer as input and prints the binary representation of this number on screen. Do not submit your solution, this is just for fun and to test your understanding of the algorithm seen in class. To solve this, I suggest you take a look at the file array_loop.c from the weekly lab, it contains a construct you may need.