Download Register Size - Machine Structures - Solved Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!
University of California, Berkeley – College of Engineering
Department of Electrical Engineering and Computer Sciences
Summer 2010 Instructor: Paul Pearce 2010-07-
CS61C Midterm
After the exam, indicate on the line above where you fall in the emotion spectrum between “sad” & “smiley”...
Last Name Answer Key
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 LAB TA (please circle) Eric Tom Noah Alex
Name of the person to your Left
Name of the person to your Right
All the work is my own and I have collaborated with no one. 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)
a) Instructions (Read Me!)
Don’t Panic!
This booklet contains 12 numbered pages including the cover page and MIPS reference guide. 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. 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 login on every sheet of paper.
You have 180 minutes to complete this exam. The exam is closed book, no computers, PDAs,
calculators. You are allowed 1 page of notes, front and back.
A MIPS reference sheet has been provided as the last page of this handout. You should rip it off.
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.
Question 0 1 2 3 4 5 6 Total
Minutes 1 15 20 25 15 20 25 (+59 to review) = 180 Points 1 39 20 50 20 25 35 190
Score 1 39 20 50 20 25 35 190
Login: cs61c-__ Answers __
Question 1: Where’s the kitchen sink? ……………….……………… (39 pts, 15 min)
Part 1: CS61C trivia
True/False: Circle the correct answer in the right-hand column.
a) You must use the addu instruction to add unsigned numbers. T F
b) You do not need to save volatile registers if your code doesn’t call any
subfunctions.
T F
c) You do not need to save volatile registers if they won’t be modified by any
subfunctions.
T F
d) We add a bias to floating point exponents to increase the range of values we
can represent.
T F
e) The instructions srl and sra behave identically on positive (2’s complement)
numbers.
T F
f) There are situations where using first-fit will cause less fragmentation than
best-fit.
T F
g) The size of a structure that contains only 2 ints and 1 char will be 9 bytes. T F
Fill in the blank: neatly write your answer in the right-hand column
h) How many things can you represent with N bits? 2 N
i) Suppose you are given N bits. How many more bits would you need if
we wished to triple the number of things we wanted to represent?
j) Assuming the following C code declaration:
char str[] = “Hello World”;
What will sizeof(str) return?
k) Assuming the following C code declaration:
char *str = “Hello World”;
What will sizeof(str) return?
l) Assuming the following C code declaration:
char str[] = “Hello\0World”;
What will strlen(str) return?
(Continued on next page)
Login: cs61c-__ Answers __
Question 2: Did somebody say “Free Lunch”?! ……………………… (20 pts, 20 min)
Consider the following 10-bit floating-point format. It contains the same fields (sign, exponent,
significand) and follows the same general rules as the 32-bit IEEE standard (denorms, biased exponent,
non-numeric values, etc.). It simply allocates its bits differently. Please answer the following questions,
and show all your work in the space provided. We went ahead and got you started.
Number represented by 0x00 : ______ __0
# Bits in the Mantissa: ____________6__________
a) Exponent Bias: ____ 3
b) Implicit exponent for denormalized #’s: ____ -
c) # of Numbers between (2 ≤ n < 8): ____ 128
d) Largest number x such that x + .5 = .5: ____ 2 -8^ = 1/
(Continued on next page)
S EEE MMMMMM
Login: cs61c-____
Question 2: Did somebody say “Free Lunch”?! (Continued) ………. (20 pts, 20 min)
(Repeated so you don’t need to flip back and forth)
e) Difference between the two smallest positive values: ____ 2 -8^ = 1/256 _
f) Difference between the two largest non- positive values: ____ 2 -3^ = 1/
g) Number of NaN’s: ____ 126
h) Using the above format, what is the bit pattern for the floating-point
number closest to 14.4? _____0b0110110011______
S EEE MMMMMM
Login: cs61c-____
void free_list(struct node **head_h) {
struct node *curr = head_h;
while (curr != NULL) {
free(curr);
curr = curr->next;
void free_list(struct node **head_h) {
struct node *curr = *head_h ;
while ( curr != NULL ) {
*free(curr->datum); struct node tmp = curr->next; free(curr); curr = tmp;
*head_h = NULL;
Question 3: Don’t lose your head (Continued) ……………………….. (50 pts, 25 min)
b) The following free_list function takes a pointer to head (recall head points to the first element in
the doubly linked list, and head_h is a handle to the head) and frees all memory that was allocated for
the list. Once the list is freed, free_list must set head to NULL. This function is BUGGY. Assume
the memory for both the nodes and the datums was allocated from the heap. You should also assume
free_list is correctly passed the address of head and all datums are non-NULL.
Describe all the bugs in the given free_list
function in the space below. Number each
bug.
1) *head_h
2) Free then deref
3) No datum free
4) No head update
c) Implement a correct iterative version of free_list which corrects the bugs found in part b. Please
re-read the function requirements from part b. You may not need all the space provided.
Login: cs61c-__ Answers __
Question 4: Who needs a compiler? …………………………………(20 pts, 15 min)
While trying to compile a program for a MIPS processor, the compiler crashes. Before crashing, it
compiled everything but the following lines of code.
// var and i are signed integers
// ptr is a pointer to a sufficiently large array of integers
i = 0;
while (var != 0) {
var = var * 2;
i++;
if (i >= 0) {
var = *ptr;
else {
var = *(ptr+i);
Finish the job of the compiler by translating the preceding C to MIPS code.
var is stored in $t0, i is stored in $t1, and the pointer ptr is in $t2. Ignore register conventions for
this problem. We have given you the first instruction and some labels to help guide you. You must
comment your code! You may not need all the given space. Do not use mult!
addu $t1, $0, $0 _ _ # Set i = 0
while: _______ beq $t0, $0, endWhile____ # While var != 0
________sll $t0, $t0, 1___________ # Var *= 2
________addiu $t1, $t1, 1_________ # i++
________j while___________________ # loop back and retest
endWhile: ________slt $t3, $t1, $0__________ # t3 = 1 if t1 < 0, 0 otherwise
________bne $t3, $0, else_________ # if t1 is not >= 0, goto else
if: ________lw $t0, 0($t2)____________ # var = *ptr;
________j done____________________ # DO NOT execute the else case
__________________________________
__________________________________
else: ________sll $t3, $t1, 2___________ # Get byte offset
________addu $t4, $t2, $t1________ # Add in the offset
________lw $t0, 0($t4)____________ # var = *(ptr + i);
done: __________________________________
Login: cs61c-__ Answers __
Question 6: It’s all MIPS to me…………………………………………… (35 pts, 25 min)
A) You are the assembler. Convert the following MAL MIPS code to TAL. Assume the labels are located
at the addresses specified, and adding additional instructions does not affect these addresses. If a
MAL instruction is already TAL, simply rewrite it in the TAL column.
Address MAL TAL
0x10000000 entry: subiu $sp, $sp, 4 addiu $sp, $sp, -
0x10000004 lbu $t0, 6($sp) lbu $t0, 6($sp)
0x10000008 move $v0, $a add $v0,$0, $a
0x1000000C ble $v0, $0, entry
blez $v0 entry
0x10000010 j label j label
0x10000014 sltiu $t0, $t1, 0x8000 ori $at, $0, 0x slt $t0, $t1, $at #because sltiu sign extends #0x8000 cannot be represented in #16 bits ... # Some TAL instructions ... # which you can ignore ... There is nothing to translate to TAL here!
0x2000000C label: # Some instruction
(This space left intentionally blank. Feel free to doodle.)
Problem continued on next page.
Login: cs61c-____
Question 6: It’s all MIPS to me (Continued) …………………………… (35 pts, 25 min)
B) Assemble the following TAL code to its machine language representation. You must show all your
work (below) to receive credit.
0x78000000 start: addu $t0, $t9, $s1 = 0x__ 03314012 _______________
0x78000004 loop: lw $v0, -8($sp) = 0x__ 8FA2FFF8 _______________
0x78000008 beq $v0, $0, done = 0x__ 10400004 _______________
0x7800000C nop = 0x
0x78000010 nop = 0x
0x78000014 nop = 0x
0x78000018 nop = 0x
0x7800001C done: j loop = 0x__ 0A000001 ______________
Show your work here.
addu $t0, $t9, $s
lw $v0, -8($sp)
beq $v0, $0, done
j loop