Unformatted text preview:

University of California, Berkeley – College of Engineering Department of Electrical Engineering and Computer Sciences Summer 2018 Instructors: Steven Ho, Nick Riasanovsky 2018-07-03 CS61C MIDTERM 1 Last Name (Please print clearly) First Name (Please print clearly) Student ID Number Circle the name of your Lab TA Damon Jonathan Sean Sruthi Emaan Suvansh Sukrit Name of the person to your: Left | Right All my work is my own. I had no prior knowledge of the exam contents nor will I share the contents with others in CS61C who haven’t taken it yet. (please sign) Instructions ● This booklet contains 9 pages including this cover page. The back of each page is blank and can be used for scratch work, but will not be graded (i.e. not even scanned into Gradescope). ● Please turn off all cell phones, smartwatches, and other mobile devices. Remove all hats, headphones, and watches. Place everything except your writing utensil(s), cheat sheet, and beverage underneath your seat. ● You have 80 minutes to complete this exam. The exam is closed book: no computers, tablets, cell phones, wearable devices, or calculators. You are allowed one page (US Letter, double-sided) of handwritten notes. ● There may be partial credit for incomplete answers; write as much of the solution as you can. ● Please write your answers within the boxes and blanks provided within each problem! Question 1 2 3 4 5 Total Possible Points 12 16 20 20 12 80 If you have the time, feel free to doodle on the front page! 1SID: ________________ This page is intentionally blank. Draw here if you are bored. 2SID: ________________ Question 1: Number Representation and Floating Point (12 pts) Given the following bit string 0b1111 1100, answer the following questions: 1) What is this bitstring’s value if it was interpreted as an unsigned number? _________________ 2) What is this bitstring’s value if it was interpreted in two’s complement? _________________ 3) Suppose the bit string was represented as fixed point where the bits following the dot (.) represent the base (2) to the power of a negative exponent. What number does the bitstring 0b1111.1100 represent? _________________ 4) Now let’s devise a scheme for interpreting fixed point numbers as positive or negative values. Complete the following sentence: Given an 8-bit fixed point bitstring 0bXXXX.XXXX with a value of Y, in order to compute -Y, we must flip all the bits of Y and add: _________________ 5) What is the value of 0b1111.1100 given the two’s complement fixed point representation described above? _________________ 6) You are given the following field breakdown and specifications of an 8-bit floating point, which follows the same rules as standard 32-bit IEEE floats, except with different field lengths: Sign: 1 bit Exponent: 3 bits Significand: 4 bits Exponent Value Significand Value Floating Point Value Smallest Zero, Non-Zero ±0, Denormalized Largest Zero, Non-zero ±Infinity, NaN What is the floating point value of 0b1111 1100: _________________ 7) We now modify the floating point description in part 6 so that the exponent field is now in two’s complement instead of in bias notation. Compute the floating point value of 0b1111 1100. _________________ 3SID: ________________ Question 2: C Memory Management (16 pts) char *mood; char *copy_message (char *msg) { char *x = malloc (sizeof (char) * (strlen (msg) + 1)); strncpy (x, msg, strlen (msg)); x[strlen (x)] = ‘/0’; / **** 6 ****/ return x; } void print_int (int *p) { printf (“%d\n”, *p); /**** 7 ****/ } void print_msg (char *str) { char *cpy = calloc (strlen (str) + 1, 1); strncpy (cpy, str, strlen (str)); printf (“%s\n”, cpy); /**** 8 ****/ } char *a () { char res[7] = “ rules”; return res; } char *b () { char *var = “cs 61c”; return var; } void c () { printf (“%s\n”, a ()); /**** 9 ****/ printf (“%s\n”, b ()); /**** 10 ****/ } int main () { int y; mood = malloc (3); strcpy (mood, “hi”); copy_message (mood); print_int (&y); print_msg (mood); c (); } 4SID: ________________ Each of the following values below evaluates to an address in the C code on the previous page. Select the region of memory that the address points to (notice each function is called exactly once). 1. mood Ⓐ Code Ⓑ Static Ⓒ Stack Ⓓ Heap 2. &mood Ⓐ Code Ⓑ Static Ⓒ Stack Ⓓ Heap 3. var Ⓐ Code Ⓑ Static Ⓒ Stack Ⓓ Heap 4. res Ⓐ Code Ⓑ Static Ⓒ Stack Ⓓ Heap 5. print_int Ⓐ Code Ⓑ Static Ⓒ Stack Ⓓ Heap On the previous page there are comments on lines with numbers from 7-11. Each of these refers to a line of code that requires a dereference of a pointer to be performed. What we want to do is characterize if these memory accesses are legal c. We will use the following terminology Legal: All addresses dereferenced are addresses that the program is allowed to read. Initialized: Is there actual meaningful data in contents (data at each address) or is it garbage. Always Illegal: This line will always dereference an address the program doesn’t have explicit access to Possibly Legal: The operation could result in only dereferences of legal addresses but it’s also possible that in other runs on the program illegal accesses occur. For each of lines that have the numbered comment select the best answer from ● A. Legal and Initialized ● B. Legal and Uninitialized ● C. Possibly Legal ● D. Illegal For example for question 6 you should answer about the line with the /**** 6 ****/ comment from when the program runs. 6. Ⓐ Ⓑ Ⓒ Ⓓ 7. Ⓐ Ⓑ Ⓒ Ⓓ 8. Ⓐ Ⓑ Ⓒ Ⓓ 9. Ⓐ Ⓑ Ⓒ Ⓓ 10. Ⓐ Ⓑ Ⓒ Ⓓ 5SID: ________________ Question 3: RISC-V Coding (20 pts) 1. Fill in the following RISC-V code so that it properly follows convention. Assume that all labels not currently in the code are external functions. You may not need all the lines provided. Pro: __________________________ __________________________ __________________________ __________________________ __________________________ __________________________ __________________________ Body: mv s1 a0 jal ra foo mv s2 a0 addi a0 x0 6 Loop: beq a0 x0 Epi addi a0 a0 -1 mv s3 a0 jal ra foo


View Full Document
Download Midterm
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?