Download Numerical Representation - Machine Structures - Exams and more Exams Structural Analysis in PDF only on Docsity!
University of California, Berkeley – College of Engineering
Department of Electrical Engineering and Computer Science
Spring 2004 Instructor: Dan Garcia 2004 - 03 - 08
CS61C Midterm
Last Name
First Name
Student ID Number
Login cs61c-
The name of your TA (please circle) Alex Chema Jeremy Paul Roy
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
- This booklet contains 6 numbered pages including the cover page plus photocopied pages
from COD and K&R. 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 points if done incorrectly) involves filling in the front of this page and putting
your name & login on every sheet of paper.
- You have 180 minutes to complete this exam. The times listed by each problem will allow you
to finish with 45 (!) minutes left to check your answers. The exam is closed book, no
computers, PDAs or calculators. You may use one page (US Letter, front and back) of notes.
- 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 7 8 9 Total Min 2 8 20 30 30 30 15 1 14 150 Max 0/- 1 2 4 8 16 14 18 6 3 4 75 Score
Question 1: Big Ideas (2 Points – 2 minutes)
We’ve discussed four design principles that guide the authors of instruction sets (& played a part in
MIPS design). What is one of them and how did it affect the design? Be as brief as possible.
We’ve shown one of them for you to refresh your memory.
Design Principle How was the MIPS design affected?
Smaller is faster MIPS has 32 registers, rather than many more.
Question 2: Numerical Representation (4 Points – 8 minutes)
a) Below is a table corresponding to the different systems for representing #s. Fill in the six blanks in
the table. Each column should contain the same #, written different ways. Show your work below.
Decimal (base 10) - 310 Scratch^ space
8 bit Sign-Magnitude (in hex)
8 bit One’s Complement (in hex) 0x
8 bit Two’s Complement (in hex)
b) We’ve seen the decimal point and the binary point, but as you can guess, there’s also a hex point.
Fill in the table below. NB: This is a different question than (a) above – there is no encoding here.
Decimal (base 10) - 18.25 10 Scratch space
Hexadecimal (base 16) 20.2 16
Question 3: Floating Point (8 Points – 20 minutes)
a) Shown below is a number whose value is described by the fields (sign, exponent, significand) of
the IEEE 754 32-bit floating-point standard. What is the next-largest (closest to it but larger [closer
to +∞] than it) number that can be represented? Write it in the same format in the blanks below.
12 111000002 00…0 2 Next-largest ⇒
b) Using IEEE 754 32-bit floating point, what is the largest positive number x that makes this
expression true: x + 1.0 = 1.0? Assume there is no rounding with extra guard/rounding bits (i.e., we
truncate the bits outside the given fractional mantissa field). Write the answer in the same format as
in (a) above. Show all work!
Show your work below
Question 5: MIPS Assembly Language (14 Points – 30 minutes)
a) Below is a function written in C and the same function partially compiled into MAL. Fill in the
blanks (and the comments!) to complete the compilation. Use register names, not #s. (8 points)
int replaceInt(int array, int toReplace, int replaceWith) { for(;array; array++) { if(array == toReplace) { array[0] = replaceWith; return array; } } return NULL; } line 0 # replaceInt: ___ ___ ___(____)
1 beq ___ ___ endLoop # We’re done 2 beq ___ ___ doReplace # Let’s replace it 3 addiu ___ $a0 ___ # _______________________ 4 j ________________ 5 doReplace: ____ ___ ___(____) # _______________________ 6 ____ ___ ___ # _______________________ 7 j ret 8 endLoop: move ___ ___ # return NULL 9 ret: jr $ra
b) Now, provide us with the MIPS code that would correspond to the following C function call.
You may not need all the lines (or blanks) below. Note: myArray starts at 8($sp). (3 points)
replaceInt(myArray, 1, 2)
c) Optimize the code above and reduce the number of instructions to fewer than 10. You can do this
through slight adjustments of fewer than four lines of code. Your answer should be in the form of
directives that tell us how the code will be changed: “Move line __ to __ (and change __)”. If
your destination is between two lines, use fractional line numbers. Your last command should be
“Delete line __”. E.g., if you wanted to move line 2 right after line 4 (but have it now be labeled
doReplace) you would write: “move line 2 to 4.5 and change the doReplace label to be at
line 4.5 ”. You may not need all the lines below; leave “and change __” blank if not nec.). (3 pts)
Move line __ to __ (and change ______________________________________________________) Move line __ to __ (and change ______________________________________________________) Move line __ to __ (and change ______________________________________________________) Delete line __
Name: _______________________________ Login: cs61c-____
Question 6: MIPS Reverse-Engineering (18 Points – 30 Minutes)
a) You have heard of Jedi hackers reverse-engineering programs. Prove you belong in that elite
group by converting the following MIPS function mystery into C code. Show your work by adding
comments to the code to help you understand it. (10 points)
1 mystery: bnez $a0, recur # int mystery(int n) { 2 li $v0, 0 # 3 jr $ra # 4 recur: sub $sp, $sp, 8 # 5 sw $ra, 4($sp) # 6 sub $a0, $a0, 1 # 7 jal mystery # 8 sw $v0, 0($sp) # 9 jal mystery # 10 lw $t0, 0($sp) # 11 addu $v0, $v0, $t0 # 12 addu $v0, $v0, 1 # 13 add $a0, $a0, 1 # 14 lw $ra, 4($sp) # 15 add $sp, $sp, 8 # 16 jr $ra #
b) You may have noticed that mystery doesn’t follow proper register conventions but somehow
works. Tell us which line in particular is the most blatant offender and what’s wrong with it? (2 pts)
Line # How does it violate register conventions?
c) What is printed as a result of printf(“%d”, mystery(32))? Show your work. (4 points)
d) What is printed as a result of printf(“%d”, mystery(34))? Show your work. (2 points)