Download CS61c Midterm Exam I - EECS, UC Berkeley and more Exams Computer Science in PDF only on Docsity!
University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences
CS61c B. Harvey Fall 2003 J. Wawrzynek
Midterm Exam I
This is a open-book exam. You are allowed to use any books and notes that you wish.
No calculators or electronic devices of any kind, please. You have 2 hours. Each
question is marked with its number of points.
This exam booklet should have 9 printed pages, plus 5 blank pages at the end. Check to
make sure that you have all the pages. Put your name and login neatly on each page.
Show your answers in the space provided for them. Write neatly and be well organized.
If you need extra space to work out your answers, you may use the back of previous
questions or the blank sheets attached to the back of your exam booklet. However, only
the answers appearing in the proper answer space will be graded.
Good luck!
problem maximum score
1 (integers) 6 pts
2 (C short answer) 6 pts
3 (MIPS short answer) 5 pts
4 (floating point) 4 pts
5 (performance) 2 pts
6 (malloc) 2 pts
7 (MIPS coding) 5 pts
total 30 pts
1) [6 pts] Number Representation.
a)[½ pt each] Given the following choice of representations for signed numbers:
a.1’s complement
b.2’s complement
c.sign and magnitude
d.none of the above
If numbers are 8 bits wide, for each of the bit patterns shown below, write the letter
(a, b, c, or d) corresponding to the representation in which each is interpreted as –23 ten
11101001_______
10010111_______
11101000_______
11101010_______
b) [1 pt] What is the value in decimal of the most negative 8-bit 2’s complement
integer?
c)[1 pt] What is the value in decimal of the most positive 8-bit unsigned integer?
d)[1 pt] Write out in hex the 16-bit result of adding the following 2’s complement
numbers: 0xFA25 + 0xB705:
e)[1 pt] Does the add operation in (d) result in overflow?
Question 2 continues on next page.
b) [2 pts] Given the following declarations:
char a[14] = "pointers in c"; char c = 'b'; char *p1 = &c, **p2 = &p1;
Cross out any of the following statements that are not correct C:
p1 = a + 5;
&p1 = &a[0];
p2 = a;
*(a + 10) = 't';
*p2 = &c;
c) [1 pt] The C language allocates call frames on the stack instead of on the heap because
doing so
1)simplifies allocation and freeing of frames,
2)speeds up access to local variables, or
3)both?
Question 3 continues on next page.
d) [2 pts] Given the declaration for a struct point:
struct point { union { int i; double d; } x; union { int i; double d; } y; } p[10];
Consider the following C code:
int i;
for(i=0; i<10; i++) { p[i].x.i = 0; p[i].y.i = 0; }
Fill in the blanks in the following translation of this C code into MIPS:
la $8, p # $8 points to p[0]
addi $9, $8, ______ # $9 points to p[10]
L1: bge $8, $9, L2 # done if past end of array
sw ____, 0($8) # p[i].x.i = 0
sw ____, ____($8) # p[i].y.i = 0
addi $8, $8, ______ # i++ b L
L2:
5) [2 pts] Performance.
a)[1 pt] A 500MHz machine takes 3 clock cycles for every instruction. You wish to
make sure that your program completes in less than 15 microseconds. How many
instructions should you limit your program to?
b)[1 pt] Consider a floating point processor. The typical instruction stream
contains 30% FMUL, 60% FADD, 10% FDIV operations. Each of these instructions
takes the following number of cycles to execute:
FMUL: 2
FADD: 1
FDIV: 10
What is the average cycles per instruction (CPI)?
6)[2 pts] Memory allocation:
Suppose that you are writing the memory allocator for an application in which only one
size of memory block is ever requested or allocated.
For each of the following concepts, enter YES if the concept is still relevant even in this
situation (all blocks the same size), or NO if the concept is not relevant in this situation:
a) fragmentation
b) list of free blocks
c) coalescing free blocks
d) garbage collection
7)[5 pts] MIPS coding:
Here is some C code to sort a list using the mergesort algorithm:
struct node { int value; struct node *next; };
struct node *mergesort(struct node *list) { if (list == 0 || list->next == 0) return list; return merge(mergesort(evens(list)), mergesort(odds(list))); }
Translate this procedure into MIPS assembly language, following our standard
conventions for register use (arguments in registers, not stack, whenever possible). You
don’t have to write the helper procedures evens, odds, or merge.