CS61C Midterm Exam by UC Berkeley - Spring 2007, Exams of Structural Analysis

The cs61c midterm exam from the university of california, berkeley for the spring 2007 session, instructed by dr. Dan garcia. The exam covers various topics in electrical engineering and computer sciences, including data representation, number systems, memory management, and mips assembly language.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shalabh_li43y
shalabh_li43y šŸ‡®šŸ‡³

4.5

(18)

88 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of California, Berkeley – College of Engineering
Department of Electrical Engineering and Computer Sciences
Spring 2007 Instructor: Dr. Dan Garcia 2007-03-05
 CS61C Midterm ☺
After the exam, indicate on the line above where you fall in the emotion spectrum between ā€œsadā€ & ā€œsmileyā€...
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) Aaron Alex Brian David Matt Michael Valerie
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 7 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.
Question 0 1 2 3 4 5 Total
Minutes 1 36 36 36 36 36 180
Points 1 15 14 15 15 15 75
Score
pf3
pf4
pf5

Partial preview of the text

Download CS61C Midterm Exam by UC Berkeley - Spring 2007 and more Exams Structural Analysis in PDF only on Docsity!

University of California, Berkeley – College of Engineering

Department of Electrical Engineering and Computer Sciences

Spring 2007 Instructor: Dr. Dan Garcia 2007-03-

/ CS61C Midterm ☺

After the exam, indicate on the line above where you fall in the emotion spectrum between ā€œsadā€ & ā€œsmileyā€...

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) Aaron Alex Brian David Matt Michael Valerie

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 7 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.

Question 0 1 2 3 4 5 Total

Minutes 1 36 36 36 36 36 180

Points 1 15 14 15 15 15 75

Score

Question 1: Is this the best midterm in memory? No, we freed it! (15 pts, 36 min)

a) I have N bits to represent data, and every bit pattern has a unique meaning.

I want to represent 3 times as many things. How many more bits do I need?

b) We are given two nibbles, A (=0xF) and B (=0b0010), and we wish to calculate their SUM = A + B.

We only have a nibble to store the SUM result. What is SUM if all three nibbles (A, B, SUM) were…

Algorithm: SUM = encode( decode-into-decimal(A) + decode-into-decimal(B) )

SUM (single hex

character)

Decimal number

SUM encodes

Was there

overflow?

…sign magnitude? 0x

…ones complement? 0x

…unsigned? 0x

…twos complement? 0x

...encoded with a bias of 7 (like the 0x

way the exponent is encoded w/float)

c) Put the following in chronological order. We’ve started it for you.

Code and Data from various places are stitched together.

1 A CS61C student is assigned a project that implements big_nums.

Execution begins at main.

The student writes his or her code in C.

Link tables are produced.

MAL is translated into TAL.

Static, code, and global space are reserved/initialized in memory.

The student’s C code is translated into MIPS.

Links are ā€œeditedā€

d) Assume we have just enough bits to byte-address 512 10 zebibytes. We

want to define some number of the most-significant bits to encode

910 x 2

50

things, and some number of the least-significant bits to encode

2,000 10 things. How many things can we encode with the remaining bits?

Use IEC language, like ā€œ16 mebithingsā€. Show your work.

Static Stack Heap

1 typedef struct bignum { 2 int len; 3 *char num; 4 char description[100]; 5 } bignum_t; 6 *bignum_t res; 7 8 int main() { 9 bignum_t b; 10 *b.num = (char ) malloc (5 * sizeof(char));

e) For every line

of code on the

right, we want

to know if any

memory is

used, and if

so, where and

how much. If

zero, leave it

blank.

// more code below

Question 3: Goodness, Grandma, what bignums you have! (15 pts, 36 min)

Part A: After your extensive C bignum experience, you were hired by Lawrence Berkeley Labs to

make an arbitrary precision math package. The scientists use scientific notation and keep track of

significant figures. The scientists have written a function called sci_bignum_cmp as shown below.

Unfortunately, the implementation has at least one bug. In the boxes at the bottom, briefly explain all

of them and give sample values for a and b (i.e. 1.23 x 10

4

) that causes sci_bignum_cmp() to reveal

the bug. You may not necessarily use all the boxes.

#define POS '+' #define NEG '-' typedef struct sci_bignum { char sign; // POS or NEG *char significand; // null-terminated string of decimal digits ('.' implicit) unsigned int num_sigfigs; // equal to strlen(significand) int exponent; } sci_bignum_t;

// Compare a to b; return <0 if a < b, 0 if a == b, or >0 if a > b (just like strcmp )

**int sci_bignum_cmp(sci_bignum_t a, sci_bignum_t b) {

if (a->exponent != b->exponent) {

return (a->exponent < b->exponent? -1 : 1);

} else if (a->sign != b->sign) {

return (a->sign < b->sign? -1 : 1);

} else {

for(int i = 0; a->significand[i]; i++) {

if(a->significand[i] != b->significand[i])

return (a->significand[i] < b->significand[i]? -1 : 1); } } }

Bug Description

Values for a and b that reveal the bug

What a correct sci_bignum_cmp should return

What this buggy sci_bignum_cmp returns / does a =

b =

a =

b =

a =

b =

a =

b =

As an example, to store the number 1.23 x 10

4 , the sign would be the char '+', the significand would be the null-terminated string "123", num_sigfigs would be 3 , and the exponent would be 4. There is an implicit decimal point after the first significand digit.

Question 3: Goodness, Grandma, what bignums you have! (cont’d)

Part B: The scientists also use vectors

quite a bit, so they wrote the following C

struct:

*typedef struct sci_vector { sci_bignum_t elts; unsigned int num_elts; } sci_vector_t;

The scientists want the elements of a

given vector to have the same number of

significant digits. In other words, each

element in a vector should be truncated to the smallest num_sigfigs in the vector. They want you to

help them write a function (by filling in the blanks) that will ā€œclean upā€ its argument vector by modifying

all its sci_bignums to have the appropriate number of significant digits. Any excess allocated space

should be freed so there’s no wasted memory. Avoid memory leaks.

#define MIN(a, b) ((a)<(b)?(a):(b))

*void clean_vector(sci_vector_t vec) { unsigned int min_sigfigs = 0xFFFFFFFF; // Initialize to biggest unsigned int

/* get min significant digits */ for (unsigned int i = 0; i < vec->num_elts; i++)

min_sigfigs = MIN( _________________________ , ____________________________);

/* truncate all elts to have min_sigfigs */ *for (unsigned int i = 0; i < vec->num_elts ; i++) { sci_bignum_t b = _____________________________ // convenient reference

*if (______________________________ > ______________________________) { char new_significand = (______) malloc(______________________________); _________________________________________________________ // for strcpy

strcpy(new_significand, b->significand);

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

__________________________________________________________________________

} } }

Question 5: He’s a unix. He’s definitely a unix. He’s dead! (15 pts, 36 min)

Assume (for simplification) that main returns a value to its caller (Unix) through standard MIPS

procedure calling conventions. We wish to see how long our command-line inputs arguments are:

unix% count_argument_characters 0

unix% count_argument_characters I love cs61c! 11

Implement count_argument_characters in MAL MIPS. Follow the hints given by the comments;

you may not need to use all the lines.

main: ______________________ # ans=

word: addiu ____________, -1 # Decrement

beq ____________, done # We’re done!

______ $t0, ____, ____ # change $t

______________________
______________________

letr: ______________________ #

beq $t2, _______, word # end of word

______________________

addiu $t1, $t1, 1 # increment $t

______________________

j ____________________ # keep processing

done: jr $ra