Department of Electrical Engineering and Computer Science Spring 2004 Instructor: Dan Garcia CS61C Midterm

Similar documents
Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

Department of Electrical Engineering and Computer Sciences Spring 2010 Instructor: Dr. Dan Garcia CS61C Midterm

Department of Electrical Engineering and Computer Sciences Spring 2013 Instructor: Dr. Dan Garcia CS61C Midterm

Computer Architecture I Midterm I

CS61C Midterm After the exam, indicate on the line above where you fall in the emotion spectrum between sad & smiley...

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

Midterm. CS64 Spring Midterm Exam

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

CS61C Midterm After the exam, indicate on the line above where you fall in the emotion spectrum between sad & smiley...

CS 61C, Fall, 2005, Midterm 1, Garcia Question 1: For those about to test, we salute you (11 pts, 20 min.)

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

Name: University of Michigan uniqname: (NOT your student ID number!)

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

University of California, Berkeley College of Engineering

CS61c MIDTERM EXAM: 3/17/99

Fall 2010 CS61C Midterm 1 Solutions

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

Problem maximum score 1 35pts 2 22pts 3 23pts 4 15pts Total 95pts

Computer Architecture I Mid-Term I

University of California, Berkeley College of Engineering

University of California, Berkeley College of Engineering

Assembler. #13 Running a Program II

Computer Architecture I Midterm I (solutions)

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

CS61C : Machine Structures

CS61c Summer 2014 Midterm Exam

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Do not turn the page until 5:10.

Name: Login: cs61c- Decimal Ternary 5 12 three three

CS61C Final After the exam, indicate on the line above where you fall in the emotion spectrum between sad & smiley...

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

University of California, Berkeley College of Engineering

CS61C Final Exam After the exam, indicate on the line above where you fall in the emotion spectrum between sad & smiley...

CS 61c: Great Ideas in Computer Architecture

NAME: 1a. (10 pts.) Describe the characteristics of numbers for which this floating-point data type is well-suited. Give an example.

CSE 378 Final Exam 3/14/11 Sample Solution

Question Points Score Total: 100

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

Winter 2003 MID-SESSION TEST Monday, March 10 6:30 to 8:00pm

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

Where Are We Now? Linker (1/3) Linker (2/3) Four Types of Addresses we ll discuss. Linker (3/3)

CS61c Summer 2014 Midterm Exam

The University of Michigan - Department of EECS EECS 370 Introduction to Computer Architecture Midterm Exam 1 February 17, 2011

From Code to Program: CALL Con'nued (Linking, and Loading)

This exam is worth 40 points, or 30% of your total course grade. The exam contains eight

Administrivia. Midterm Exam - You get to bring. What you don t need to bring. Conflicts? DSP accomodations? Head TA

CS61C : Machine Structures

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

Winter 2012 MID-SESSION TEST Tuesday, March 6 6:30pm to 8:15pm. Please do not write your U of C ID number on this cover page.

Assembler. Lecture 8 CS301

CS61C Summer 2013 Midterm

MIPS Functions and Instruction Formats

Lecture 10: Program Development versus Execution Environment

Department of Electrical Engineering and Computer Sciences Fall 2017 Instructors: Randy Katz, Krste Asanovic CS61C MIDTERM 2

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

ENCM 369 Winter 2017 Lab 3 for the Week of January 30

Nick Weaver Sp 2019 Great Ideas in Computer Architecture MT 1. Print your name:,

University of Waterloo Midterm Examination

CDA3100 Midterm Exam, Summer 2013

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

8*4 + 4 = 36 each int is 4 bytes

Implementing Procedure Calls

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

CS61C : Machine Structures

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

EE380 Spring 2014 Exam 1 Answer Key

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

CS 110 Computer Architecture MIPS Instruction Formats

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

University of California, Berkeley College of Engineering

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

ECE 30 Introduction to Computer Engineering

Midterm II CS164, Spring 2006

Do not turn the page until 5:10.

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Department of Electrical Engineering and Computer Sciences Fall 2016 Instructors: Randy Katz, Bernhard Boser

CS61C : Machine Structures

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

UCB CS61C : Machine Structures

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 5 2/22/2018

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

CS61C : Machine Structures

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Name: Problem 1: (25 points) Short Answers. a) (5 points) Executing a program

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

ECE550 PRACTICE Midterm

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Transcription:

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 Smaller is faster How was the MIPS design affected? 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) -3 10 Scratch space 8 bit Sign-Magnitude (in hex) 8 bit One s Complement (in hex) 0x80 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. 1 2 11100000 2 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 2/6

Name: Login: cs61c- Question 4: C (16 Points 30 minutes) We ve written matchsubstr below in C. Some of the lines are buggy and some are perfectly fine. Circle BUGGY or OK for each labeled C statement and if buggy, why it is and provide the fix. A line may be buggy for multiple reasons, so be sure you're descriptive. Use the comments near each statement as a guide for what the line SHOULD do. If the code is buggy and you have a more clever/intuitive way of doing the same thing, feel free to do it your way. Note: You can assume only valid input will be provided (two non-empty, null-terminated strings). /* This function tries to find a substring (sub) within another (string). * If matchsubstr() finds the substring, it returns the index of the start * of the substring. If there is more than one match, it returns the first. * This is the scheme-equivalent of an equal? match (not eq? match) */ int matchsubstr(char sub[], char string[]) { /* Holds the location we re checking (and will return if a match). */ A: int loc; BUGGY If buggy, why? /* These variables are pointers to the chars in sub/string */ B: char *c1, c2; BUGGY If buggy, why? /* We want to iterate through the string looking for a match, so we start at * loc=0 (beginning) and keep going as long as we have characters remaining */ C: for(loc=0; strlen(string[loc]); loc++) { BUGGY If buggy, why? /* We step through the substring using c1 and c2 to reference the * letters in sub and string. We stop when we have either exhausted * all the characters in sub (and thus found a match) or when we * encounter two characters that are not equivalent. */ D: for(c1 = sub, c2 = string&loc; BUGGY If buggy, why? E: ; BUGGY If buggy, why? F: c1++, c2++) { BUGGY If buggy, why? /* If we didn't find a match, we break out */ G: if(c1!= c2) { BUGGY If buggy, why? break; /* We return the location if we found a match */ H: if(1) { BUGGY If buggy, why? return loc; /* Return -1 if we didn't find a match */ return -1; 3/6

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 4/6

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) 5/6

Question 7: MIPS Binary (6 points, 15 minutes) Assemble the following code assuming that the label code corresponds to the address 0x00080000. You should fill in the table below with the hexadecimal value for the instruction. You must show your work to receive credit. code: addiu $v0, $zero, 0 # Inst 01 0x24020000 while_loop: lw $v1,-4($a0) # Inst 02 beq $v1,$0,loop_exit # Inst 03 # There are 10 instructions here j while_loop # Inst 14 loop_exit: Question 8: Starting a Program (3 points, 1 minute) For each of the tasks below, label it with the first two letters of the system whose job it is: COmpiler, ASsembler, LInker, or LOader. Resolve undefined labels using the relocation information and symbol table Copy the parameters (if any) to the main program onto the stack Change move $t0,$t1 into add $t0,$zero,$t1 Question 9: Memory Management (4 points, 14 minutes) Assume a simplistic view of 5 bytes of memory, and no header overhead. Further, assume best-fit, when given multiple identical best options, chooses the space closest to the head of the freelist. Also assume both first-fit and best-fit always flush-left within a chunk of free space (i.e., allocates the memory closest to the head of the freelist). These are shown below with two identical memories: FIRST-FIT BEST-FIT COMMAND /* COMMENT */ LABEL: 01234 01234 MEMORY: ----- ----- /* Begin, 5 contiguous bytes of memory free */ AA--- AA--- A=malloc(2); /* Both first- and best-fit flush-left */ AAB-- AAB-- B=malloc(1); /* A simple request */ AABC- AABC- C=malloc(1); /* Another simple request memory almost full */ AA-C- AA-C- free(b); /* Freeing B fragments our memory: 2 1-bytes */ AADC- AADC- D=malloc(1); /* Note best-fit chose space 2 over 4 */ Will first-fit ever succeed with a call to malloc where best-fit would fail? If so, draw memories (as above, except you may initialize them to any state, as long as they are equal) and give a short sequence of malloc and free calls that proves your point. If not, explain in at most 3 sentences. Yes, first-fit will succeed where best-fit would fail. (fill in a table below just like shown above) FIRST-FIT BEST-FIT COMMAND LABEL: 01234 01234 No, first-fit will never succeed where best-fit would fail. (answer below in at most 3 sentences) 6/6