CS61C Midterm - UC Berkeley - Electrical Engineering and Computer Sciences - Prof. D. D. G, Exams of Computer Science

Materials for the cs61c midterm exam held at the university of california, berkeley, department of electrical engineering and computer sciences in fall 2006. Instructions, sample questions, and answer keys for various topics such as jump statements, old-school quarter arcade, recursive mips code, and memory allocation. Students are expected to write higher-level code, perform arithmetic operations with quarter precision floats, translate recursive c code into mips assembly, and analyze the performance of memory allocation algorithms.

Typology: Exams

2010/2011

Uploaded on 05/10/2011

koofers-user-vrl-1
koofers-user-vrl-1 🇺🇸

10 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of California, BerkeleyCollege of Engineering
Department of Electrical Engineering and Computer Sciences
Fall 2006 Instructor: Dr. Dan Garcia 2006-10-16
CS61C Midterm
Last Name
First Name
Student ID Number
Login
cs61c-
Login First Letter (please circle)
a b c d e f g h i j k l m
Login Second Letter (please circle)
a b c d e f g h i j k l m
n o p q r s t u v w x y z
The name of your SECTION TA (please circle)
Scott Aaron David P. Sameer David J.
Name of the person to your Left
Name of the person to your Right
All the work is my own. I had no prior knowledge of the exam
contents nor will I share the contents with othe rs in CS61C
who have not taken it yet. (please sign)
Instructions (Read Me!)
Don’t Panic!
This booklet contains 8 numbered pages including the cover page. Put all answers on these pages;
don’t hand in any stray pieces of paper.
Please turn off all pagers, cell phones & beepers. Remove all hats & headphones. Place your
backpacks, laptops and jackets at the front. Sit in every ot her seat. Nothing may be placed in the “no fly
zone” spare seat/desk between students.
Question 0 (1 point) involves filling in the front of this page and putting your name & login on every front
sheet of paper.
You have 180 minutes to complete this exam. The exam is closed book, no computers, PDAs or
calculators. You may use one page (US Letter, front and back) of notes and the green sheet.
There may be partial credit for incomplete answers; write as much of the solution as you can. We will
deduct points if your solution is far more complicated than necessary. When we provide a blank, please
fit your answer within the space provided. You have 3 hours...relax.
Problem
0
1
2
3
5
6
Total
Minutes
0
20
30
40
30
30
180
Points
1
11
12
15
12
12
75
Score
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download CS61C Midterm - UC Berkeley - Electrical Engineering and Computer Sciences - Prof. D. D. G and more Exams Computer Science in PDF only on Docsity!

University of California, Berkeley – College of Engineering

Department of Electrical Engineering and Computer Sciences

Fall 2006 Instructor: Dr. Dan Garcia 2006 - 10 - 16

CS61C Midterm

Last Name First Name Student ID Number Login cs61c- Login First Letter (please circle) a b c d e f g h i j k l m Login Second Letter (please circle) a b c d e f g h i j k l m n o p q r s t u v w x y z The name of your SECTION TA (please circle) Scott Aaron David P. Sameer David J. Name of the person to your Left Name of the person to your Right All the work is my own. I had no prior knowledge of the exam contents nor will I share the contents with others in CS61C

who have not taken it yet. (please sign)

Instructions (Read Me!)

  • Don’t Panic!
  • This booklet contains 8 numbered pages including the cover page. Put all answers on these pages; don’t hand in any stray pieces of paper.
  • Please turn off all pagers, cell phones & beepers. Remove all hats & headphones. Place your backpacks, laptops and jackets at the front. Sit in every other seat. Nothing may be placed in the “no fly zone” spare seat/desk between students.
  • Question 0 (1 point) involves filling in the front of this page and putting your name & login on every front sheet of paper.
  • You have 180 minutes to complete this exam. The exam is closed book, no computers, PDAs or calculators. You may use one page (US Letter, front and back) of notes and the green sheet.
  • There may be partial credit for incomplete answers; write as much of the solution as you can. We will deduct points if your solution is far more complicated than necessary. When we provide a blank, please fit your answer within the space provided. You have 3 hours...relax. Problem 0 1 2 3 4 5 6 Total Minutes 0 20 30 40 30 30 30 180 Points 1 11 12 15 12 12 12 75 Score

Question 1: Warm up jog and stretch (11 pts, 20 min.)

You’re coming straight from a wild pre-midterm toga party and are not quite focused on the exam.

Fear not, this question will get you warmed up and ready to rock, 61C style.

c) During which phase of the process from coding to execution do each of the following things

happen? Place the most appropriate letter of the answer next to each statement. Some letters

may never be used; others may be used more than once.

The stack allocation increases A) Never

jr $ra B) During loading

You give your variables names C) During garbage collection

Your code is automatically optimized D) While writing higher-level code

Jump statements are resolved E) During the compilation

Pseudo-instructions are replaced F) During assembly

A memory leak occurs G) During linking

A jal instruction is executed H) When malloc is called

Symbol and relocation tables are created I) When free is called

The “Buddy System” might be used J) When a function is called

Machine code is copied from disk into memory K) When a function returns

Storage in C-originated-code is garbage collected L) When registers are spilled

MAL is produced M) During mark and sweep

N) When there are no more

references to allocated memory

a) How many different things can

we represent with N bits?

b) Given the number 0x811F00FE , what is it interpreted as:

...a binary number? 0b

...an octal (base 8) number?^0

...four unsigned bytes?

...four two’s complement bytes?

...a MIPS instruction?

Use register names (e.g,. $a0 ).

Question 3: You must be kid ding! (groan) (15 pts, 40 min)

We have a simple linked list that consists of kids’ name s (a standard C string) and the grade they are

in – an integer between 0 (Kindergarten) and 12. The structure appears as follows, with an example:

**typedef struct kid_node { int grade; char name; struct kid_node next; } kid_t;

For “administrative reasons”, we’d like to categorize our kids by grade.

We copy the kids’ information into an array of linked lists indexed by the grade.

Note that changing ANY of the data

in these structures here should NOT

affect the original list above.

Fill in the blanks in the below code:

a) The create_kid_array function will return a pointer to the new array. Remember, the

range of grades is 0-12, inclusive, and the original list MUST remain unchanged.

#define MAXGRADE 12

kid_t **create_kid_array(kid_t *kid) {

int i; /* in case you need an int */

kid_t tmp; / or kid_t ptr somewhere */

kid_t **kid_array = (kid_t **) malloc ( __________________________ );

if (kid_array == NULL) return NULL; /* malloc has no space! */

/* Additional initializing – add some code below */

while (_____________________!= NULL) { /* populate the array */

tmp = entry_list->next;

return kid_array;

Kim Paula Juan

kid

Index: 0 1 2 3

kid_array

Paula Juan Kim

Question 3: You must be kid ding! (groan) ...continued... (15 pts, 40 min)

**typedef struct kid_node { int grade; char name; struct kid_node next; } kid_t;

b) For every Yin, there is a Yang. Now that we have a function for creating kid arrays, we must

create a function that frees all memory associated with the structure. Fill in the following

functions. free_kid_array calls the recursive function free_kid_list which frees a

single kid list.

void free_kid_array(kid_t *kid_array[]){

int i;

for (i = 0; i <= MAXGRADE; i++){

free_kid_list(kid_array[i]);

/* Clean up if necessary */

__________________________________________________________________

/* Remember, this has to be a RECURSIVE function. */

void free_kid_list(kid_t *kid) {

if (kid == NULL)

return;

Index: 0 1 2 3

kid_array

Paula Juan Kim

/* Declare temp variables */

Question 5: A little MIPS to C action... (12 pts, 30 min)

You may find this definition handy: sllv (Shift Left Logical Variable): sllv rd, rt, rs

(The contents of general register rt are shifted left by the number of bits specified by the low order

five bits contained as contents of general register rs , inserting zero into the low order bits of rt. The

result is placed in register rd .) Compilers translate z = x << y into sllv zReg, xReg, yReg

rube: li $t0, 0

loop: andi $t1, $a0, 1

beq $t1, $zero, done

srl $a0, $a0, 1

addiu $t0, $t0, 1

j loop

done: li $v0, 0

li $t2, 32

beq $t2, $t0, home

ori $v0, $a0, 1

sllv $v0, $v0, $t

home: jr $ra

a) In the box, fill in the C code for rube.

b) rube can be rewritten as two TAL instructions!

We’ve provided the second; what’s the first?

rube:

jr $ra

c) How would rube change if we swapped the srl with sra? Examples might be:

  • rube doesn’t change
  • rube now crashes on all input
  • rube is the same, except rube(5) now overflows the stack
  • rube now returns - 3 always
  • etc…

int rube (unsigned int x) {

int i = 0; /* i is $t0 */

Question 6: Memory, all-ocate in the moonlight... (12 pts, 30 min.)

a) Fill in the following table according to the given memory allocation scheme. Show the changes

that are made to the memory, if any. Requests for memory are in the left column. If a request can’t

be satisfied, the memory and internal state (e.g., where next-fit will start) shouldn’t change.

Likewise, if there is no prior allocation made for a given free call, ignore the action for the given

scheme. Assume that if best-fit has multiple choices, it will take the first valid one starting from the

left. The first few rows are filled in as an example.

Memory action First-Fit Next-Fit Best-Fit

A = malloc(1) A A A

B = malloc(2) A B B A B B A B B

free(A) B B B B B B

C = malloc(1)

D = malloc(1)

E = malloc(2)

free(B)

free(C)

free(E)

F = malloc(2)

G = malloc(2)

b) Lets compare the performance of a Slab Allocator and Buddy System for 128 bytes of memory (It

will be fun!). The Slab Allocator has 2 32-byte blocks, 7 8-byte blocks, and 4 2-byte blocks. All

requests to memory are at least 1 byte, and are no more than 64 bytes. For ideal requests (of your

choosing), find the limits:

What is the maximum number of requests Slab can satisfy successfully?

What is the minimum number of requests Slab can satisfy before failure?

What is the maximum number of requests Buddy can satisfy successfully?

What is the minimum number of requests Buddy can satisfy before failure?

c) My code segment is SOOO big. (audience: How big is it?) It’s SOOO big that if I added one more

instruction, I wouldn’t be able to jal to it. (Currently I can jal to anywhere in my program).

How big is my code segment? Use IEC language, like 16 KibiBytes, 128 YobiBytes, etc.

Question 1: Warm up jog and stretch (11 pts, 20 min.)

You’re coming straight from a wild pre-midterm toga party and are not quite focused on the exam.

Fear not, this question will get you warmed up and ready to rock, 61C style.

c) During which phase of the process from coding to execution do each of the following things

happen? Place the most appropriate letter of the answer next to each statement. Some letters

may never be used; others may be used more than once.

L The stack allocation increases A) Never

K jr $ra B) During loading

D You give your variables names C) During garbage collection

E Your code is automatically optimized D) While writing higher-level code

G Jump statements are resolved E) During the compilation

F Pseudo-instructions are replaced F) During assembly

N A memory leak occurs G) During linking

J A jal instruction is executed H) When malloc is called

F Symbol and relocation tables are created I) When free is called

H,I The “Buddy System” might be used J) When a function is called

B Machine code is copied from disk into memory K) When a function returns

A Storage in C-originated-code is garbage collected L) When registers are spilled

E MAL is produced M) During mark and sweep

N) When there are no more

references to allocated memory

a) How many different things can

we represent with N bits?

2 N

b) Given the number 0x811F00FE , what is it interpreted as:

...a binary number?^1000 0001 0001 1111 0000 0000 1111

...an octal (base 8) number?^20107600376

...four unsigned bytes?^129 31 0

...four two’s complement bytes? -^127 31 0 -^2

...a MIPS instruction?

Use register names (e.g,. $a0 ).

1000 00|01 000|1 1111 |0000 0000 1111 1110

lb $ra, 254($t0)

;; META

GS = Grading Standard CM = Common Mistakes PC = Partial Credit NPC = No Partial Credit NB = Nota Bene (http://en.wikipedia.org/wiki/Nota_Bene) ;;;;;;;;;;;;; ;; Question 1 ;;;;;;;;;;;;; a) 2^N (This one may be the easiest question we've ever asked.) Each bit is an independent binary choice, so we have 22...2 (n times) = 2^N different possibilities, and each bit pattern maps to a different thing. GS: 1/2 pt, NPC b) 0x811F00FE ...binary Breaking this into binary, we simply map each hex digit into its binary nibble (these answers are on the green sheet!) 0b1000 0001 0001 1111 0000 0000 1111 1110 GS: 1/2 pt, NPC ...octal We simply work from the right, and re-cluster the binary number into threes 1000 0001 0001 1111 0000 0000 1111 1110 (becomes) 10|00 0|001| 000|1 11|11 0|000| 000|0 11|11 1| which when converted to octal digits becomes (with invisible leading 0s) 020107600376 GS: 1/2 pt, full PC if you had binary wrong but binary->octal correct; else NPC ...four unsigned bytes We take each byte (two nibbles) and have four separate calculations: 0x81 0x1F 0x00 0xFE which are simply base- 16 values. I.e., 0xXY = X * 16^1 + Y * 16^0 = 16X+Y 816+1 116+15 016+0 15*16+ 129 31 0 254 But another way is to think in binary, and remember some common bit-patterns. 0b1000 0001 ==> We recall the 8th bit is 128, + 1 = 129 0b0001 1111 ==> This is the biggest 5 - bit value = 2^5- 1 = 31 0b0000 0000 ==> 0, easy 0b1111 1110 ==> This is one less than the biggest unsigned byte = 2^8- 1 - 1 = 254 GS: 1 pt, full PC if you had binary wrong but conversion correct; else NPC NB: All the TAs were able to do these in their heads given the binary patterns, so that's something you might want to shoot for...

Question 2: Old-School Quarter Arcade (12 pts, 30 min)

Early processors had no hardware support for floating point numbers. Suppose you are a game

developer for the original 8-bit Nintendo Entertainment System (NES) and wish to represent fractional

numbers. You and your engineering team decide to create a variant on IEEE floating point numbers

you call a quarter (for quarter precision floats). It has all the properties of IEEE 754 (including

denorms, NaNs and ± ∞) just with different ranges, precision & representations.

A quarter is a single byte split into the following fields (1 sign, 3 exponent, 4 mantissa): SEEEMMMM

The bias of the exponent is 3, and the implicit exponent for denorms is - 2.

a) What is the largest number

smaller than ∞?^01101111

0b01101111 = 1.1111 * 2^3

b) What negative denorm is closest

to 0? (but not - 0)^10000001

10000001 = - 0.0001 * 2-^2

= - 2 -^6 = - 1/

Show your work here

You find it neat how rounding modes affect computation, if at all. You remember that the NES carries

one extra guard bit for computation, so you write the following code to run on your NES. What is the

value of c , d , and e? Please express your answer in decimal, but fractions are ok. E.g., – 5 ¾.

Show your work here

quarter q1, q2, q3, c, d, e;

q1 = - 0.25; /* - 1/4 */

q2 = - 4.0;

q3 = - 0.125; /* - 1/8 */

/* Default rounding mode */

c = q1 + (q2 + q3);

/* Default rounding mode */

d = (q1 + q2) + q3;

SetRoundingMode(TOWARD_ZERO);

e = (q1 + q2) + q3;

c – 4 ¼

d – 4 ½

e - 4 ¼

;; Question 2 ;;;;;;;;;;;;; a) Largest number smaller than infinity... In quarter (floating point), infinity's representation is 0b01110000, where the exponent field is maxed out and the mantissa is 0. The largest number smaller than infinity? Subtract 1! The result: 0b01101111. Now translate to decimal for the second column. 1.1111 * 2 ^ 3 = 1111.1 = 15. b) Negative denorm closest to 0 (but not - 0)... In a quarter, - 0's representation is 0b10000000. The smallest number of higher magnitude than - 0? Add 1! The result: 0b10000001. NB this is still a denorm because the exponent bits are 0. GS (for parts a and b): 1 pt for correct bit pattern (NPC for first column). 2 pt for correct decimal representation. 1 pt for decimal representation that is correct with respect to an incorrect bit pattern. BOTTOM HALF: q1 = - 0. q2 = - 4. q3 = - 0. For these three problems, q3 will always trump the other values when adding them together because it is so large. What numbers can be represented in a number that has the same exponent field as - 4.0? Well, - 4.0 is 2^2, so our exponent is

  1. Thus, - 4.0's representation is 0b11010000. What is it's least significant bit's value? 2^2 * 2^- 4 = 2^- 2 = 0.25. Thus, adding - 0.25 is no problem for a quarter like this, since it fits without a problem in the LSB of the quarter. It's only when we try to add - 0.125 that we have to deal with rounding. NB: Adding - 0.125 will put you exactly half-way between two numbers with exponent

c) Default rounding mode == unbiased == round toward even/ q2 + q3 happens first. - 4.0 + - 0.125 puts me exactly half-way between - 4.0 and - 4.25, numbers which can be represented in a quarter with exponent 2! So, which way do we round? - 4.0 = 0b - 4.125 falls right in between - 4.25 = 0b Round toward even tells us that we should round toward the quarter with a 0 in the LSB, so we round to - 4.0. Finally, add q1, and we end up with - 4. Answer: - 4. d) q1 + q2 = - 4.25. No problems there add on q3 (-0.125) puts us exactly half-way between - 4.25 and - 4.50. Which way do we round using the default rounding mode? - 4.25 = 0b - 4.375 falls right in between - 4.50 = 0b

Question 3: You must be kid ding! (groan) (15 pts, 40 min)

We have a simple linked list that consists of kids’ name s (a standard C string) and the grade they are

in – an integer between 0 (Kindergarten) and 12. The structure appears as follows, with an example:

**typedef struct kid_node { int grade; char name; struct kid_node next; } kid_t;

For “administrative reasons”, we’d like to categorize our kids by grade.

We copy the kids’ information into an array of linked lists indexed by the grade.

Note that changing ANY of the data

in these structures here should NOT

affect the original list above.

Fill in the blanks in the below code:

a) The create_kid_array function will return a pointer to the new array. Remember, the

range of grades is 0-12, inclusive, and the original list MUST remain unchanged.

#define MAXGRADE 12

kid_t **create_kid_array(kid_t *kid) {

int i; /* in case you need an int */

kid_t tmp; / or kid_t ptr somewhere */

kid_t **kid_array = (kid_t **) malloc ( __________________________ );

if (kid_array == NULL) return NULL; /* malloc has no space! */

/* Additional initializing – add some code below */

tmp = entry_list->next;

return kid_array;

Kim Paula Juan

kid

Index: 0 1 2 3

kid_array

Paula Juan Kim

tmp = (kid_t *) malloc (sizeof(kid_t));

tmp->grade = kid->grade;

*tmp->name = (char ) malloc (sizeof(char) * (strlen(kid->name)+1));

strcpy(tmp->name, kid->name);

tmp->next = kid_array[kid->grade];

kid_array[kid->grade] = tmp;

kid = kid->next;

(MAXGRADE+1)*sizeof(kid_t *)

Question 3: You must be kid ding! (groan) ...continued... (15 pts, 40 min)

**typedef struct kid_node { int grade; char name; struct kid_node next; } kid_t;

b) For every Yin, there is a Yang. Now that we have a function for creating kid arrays, we must

create a function that frees all memory associated with the structure. Fill in the following

functions. free_kid_array calls the recursive function free_kid_list which frees a

single kid list.

void free_kid_array(kid_t *kid_array[]){

int i;

for (i = 0; i <= MAXGRADE; i++){

free_kid_list(kid_array[i]);

/* Clean up if necessary */

__________________________________________________________________

/* Remember, this has to be a RECURSIVE function. */

void free_kid_list(kid_t *kid) {

Index: 0 1 2 3

kid_array

Paula Juan Kim

/* Declare temp variables */

free(kid_array);

NB: Do not forget, just because you freed the linked lists that the array contains, you can't assume that the array itself was freed. BLANK 2: Optional (use if you need an addiitonal variable) BLANK 3: GS: cannot lose more than 6 pts

  • 2 pts if missing the recursive call (free_kid_list(kid->next))
  • 2 pts if missing the free of the kid
  • 2 pts if missing the free of the kid's name (we only took off 1 pt if name wasn't malloc-ed)
  • 3 pts if all components are there but the order is wrong (NB: Cannot dereference freed memory, so one mustn't free the kid before his name, or the kid before the rest of the list, unless the pointers were saved before freeing)
  • 1 pts for other small errors GS: First page was out of 8, Second page was out of 7

Question 4: That’s sum grade you got there, kid! (12 pts, 30 min)

We wish to find out how many cumulative years of schooling our kids have had. Conveniently, we can

calculate that by simply summing all the grade fields from our new linked list of kid s.

Translate the following recursive C code into recursive MAL-level MIPS:

**typedef struct kid_node { int grade; char name; struct kid_node next; } kid_t;

int grade_sum(kid_t *kid) {

if (kid == NULL)

return 0;

else

return kid->grade + grade_sum(kid->next);

We started you off; Fill in the blanks. You may not add any lines; you may leave lines blank.

grade_sum: ### Feel free to write comments below

beq _____, _____, NULL_CASE beq $a0, $0, NULL_CASE

_______________________ addi $sp, $sp, - 8

_______________________ sw $ra, 4($sp)

_______________________ sw $a0, 0($sp)

_______________________ lw $a0, 8($a0)

jal grade_sum

lw $a0, 0($sp)

_______________________ lw $t0, 0($a0)

_______________________ lw $ra, 4($sp)

_______________________ addu $v0, $v0, $t

_______________________ addi $sp, $sp, 8

_______________________ jr $ra

NULL_CASE:

_______________________ move $v0, $zero

_______________________ jr $ra