




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this exam paper are: Pseudo-Instructions, Complement Bytes, Mips Instruction, Binary Number, Automatically Optimized, Variables Names, Allocation Increases, Memory Leak, Relocation Tables, Buddy System
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





CS 61C, Fall, 2006, Midterm 1, Garcia
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
Question 0: Name: Login: SID: Section TA: Person to Left: Person to Right: Signature:
Question 1: Warm up jog and Stretch (11 pts, 20 min.)
a) How many different things can we represent with N bits?
b) Given the number 0x811F00FE, what is it interpreted as:
…a binary number? …an octal (base 8) number? …four unsigned bytes? …four two’s complement bytes? …a MIPS instruction? Use register names (e.g., $a0)
c) During which phase of the process from coding to execution do each of the following things happen? Place the most appropriate letter to 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
Question 3: You must be kidding! (groan) (15 pts, 40 min) We have a simple linked list that consists of kids’ names (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:
typdef 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 temp; / 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 */
ret }
kid Kim Paula Juan
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 kid_array
Paula
Kim Juan
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 kid_array
Paula
Kim Juan
Question 3: You must be kidding! (groan) …continued… (15 pts, 40min)
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(kit_t kid_array[]){ int i; for (i = 0; i<= MAXGRADE; i++){ free_kid_list(kid_array[i]); } / Clean up if necessary */
}
void free_kid_list(kid_t *kid) {
if (kid == NULL) return;
/* 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 the 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 place din register_ rd .) Compiles 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, $v0, $t 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?
c) How would rube change if we swapped the srl with sra? Examples might be:
rube:
jr $ra
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. Request 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 rows are filled 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) Let’s 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 maximum number of requests Slab can satisfy before failure? _________
What is the maximum number of requests Buddy can satisfy successfully? _________
What is the maximum 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 would 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.