ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam Solutions 9 December 2015

Similar documents
ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 9 December 2015

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 18 November 2015

ECE 2035 Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 1 May 2013

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 19 November 2014

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam Solutions 14 December 2016

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 14 December 2016

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

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

Q1: /14 Q2: /12 Q3: /8 Q4: /8. Total: /42

CSE Lecture In Class Example Handout

ECE 2035 Programming HW/SW Systems Fall problems, 4 pages Exam Three 14 November Your Name (please PRINT clearly) Signed.

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Review of Activation Frames. FP of caller Y X Return value A B C

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

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

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

MIPS Functions and the Runtime Stack

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

a) Do exercise (5th Edition Patterson & Hennessy). Note: Branches are calculated in the execution stage.

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

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 20 November 2013

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

MIPS function continued

CSE Lecture In Class Example Handout

Chapter 2A Instructions: Language of the Computer

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Lecture 5: Procedure Calls

Homework 4 - Solutions (Floating point representation, Performance, Recursion and Stacks) Maximum points: 80 points

ECE/CS 552: Introduction to Computer Architecture ASSIGNMENT #1 Due Date: At the beginning of lecture, September 22 nd, 2010

Instruction Set Architectures (4)

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

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Compiling Code, Procedures and Stacks

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Language of the Machine Recursive functions

Q1: /30 Q2: /25 Q3: /45. Total: /100

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Functions and Instruction Formats

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

CS61C : Machine Structures

COMP2611: Computer Organization MIPS function and recursion

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Lecture 7: Binding Time and Storage

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Anne Bracy CS 3410 Computer Science Cornell University

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Instructions: Assembly Language

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Procedures and Stacks

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Hakim Weatherspoon CS 3410 Computer Science Cornell University

ECE 15B Computer Organization Spring 2010

CS61C : Machine Structures

Lecture 7: Procedures and Program Execution Preview

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Reduced Instruction Set Computer (RISC)

CS61C : Machine Structures

Solutions for Chapter 2 Exercises

Lec 10: Assembler. Announcements

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Computer Architecture

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

MIPS Procedure Calls. Lecture 6 CS301

CENG3420 Lecture 03 Review

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Chapter 2. Instructions:

CS 61c: Great Ideas in Computer Architecture

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

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

ECE260: Fundamentals of Computer Engineering

Implementing Procedure Calls

Implementing Functions at the Machine Level

Low-Level C Programming. Memory map Pointers Arrays Structures

Reduced Instruction Set Computer (RISC)

More C functions and Big Picture [MIPSc Notes]

Run Time Environments

UCB CS61C : Machine Structures

CS 316: Procedure Calls/Pipelining

Transcription:

Problem 1 (20 pos) Optimization Perform at least five standard compiler optimizations on the following C code fragment by writing the optimized version (in C) to the right. Assume square, f, and g are pure functions that each return an eger with no side effects to other data structures. square ( n) { return (n*n); f ( m) { g ( c, d) { mycode( a, b) { mycode( a, b) { x = 100; x = 100; y = 1, z=64; y = 1; do { tmp = g(a, b); if (z) do { y += x*square(z); y += x<<12; else y += g(z+a*x, y+a*x); prf("x:%d,y:%d\n",x,y); x--; prf("x:%d,y:%d\n",x,y); x--; while(x>f(y*1024)+g(a, b)); while(x>f(y<<10) + tmp); return g(y, x); return g(y, x); Briefly describe which standard compiler optimizations you applied and how they improve storage and/or execution efficiency in this code example (be specific; e.g., replaces 2 MIPS operations with 1 operation on every loop iteration ). 1. constant propagation (z=64): reduce storage required and operations to allocate/deallocate it as a local. 2. dead code elimination: if (64) always nonzero, so no need for if or else clause, reduces code length and eliminates branch operation 3. function inlining of square: eliminates overhead of function call and enables additional strength reduction optimization below (y +=x*64*64) 4. strength reduction (y +=x<<12; f(y<<10)): turns multiplication o a simpler shift operation 5. loop invariant removal (g(a, b)): this computation, including the overhead of subroutine call (activation frame allocation/deallocation), is only done once, instead of on every loop iteration. 1

Problem 2 (2 parts, 30 pos) Suppose we have the following definition which is used to create singly linked lists. typedef struct Link { ID; Value; struct Link *Next; Link; Linked Lists Part A (6 pos) Complete the following subroutine which inserts a Link (poed to by the input parameter NewLink) o the list just after the Link poed to by the input parameter Before. You may assume that neither input parameter is NULL. Before s Next field may po to another Link or it may be NULL. NewLink s Next field is NULL. void SpliceIn(Link *NewLink, Link *Before){ NewLink->Next = Before->Next Before->Next = NewLink ; /* part A*/ ; /* part A*/ Part B Complete the following recursive subroutine which takes a poer to the head of a linked list and returns a poer to a copy of the linked list. Follow the steps specified below. Link * CopyList(Link *Head) { if (Head == NULL) return _NULL ; /* part B.1 */ Link *LinkCopy ; /* part B.2 */ LinkCopy = (Link *) malloc (sizeof(link)) ; /* part B.3 */ if ( LinkCopy == NULL ){ /* part B.4 */ prf( Error: Insufficient space. ); exit(1); LinkCopy->ID = Head->ID ; /* part B.5 */ LinkCopy->Value = Head->Value ; /* part B.5 */ LinkCopy->Next = CopyList(Head->Next) ; /* part B.6 */ return LinkCopy; Part B.1 (3 pos) Fill in what should be returned if the list is empty. Part B.2 (3 pos) Add a local variable called LinkCopy that is a poer to a Link object. Part B.3 (5 pos) Allocate space for a Link structure using malloc and make LinkCopy po to the object allocated. Be sure to include appropriate type casting to avoid type errors. Part B.4 (3 pos) Fill in the test for whether malloc found enough space which controls the pr statement. Part B.5 (5 pos) Copy the values of Head s ID and Value fields to LinkCopy. Part B.6 (5 pos) Call CopyList recursively to copy the rest of the list and assign the result to LinkCopy s Next field. 2

Problem 3 (3 parts, 25 pos) Consider a hash table that is implemented using the following struct definitions. #define NUMBUCKETS 5 typedef struct Entry { Key; Value; struct Entry *Next; Entry; Associative Sets typedef struct { Entry HashTable; *Buckets[NUMBUCKETS]; Size; Part A (6 pos) What is the value of each of these (assume a 32 bit system): sizeof(entry) = 12 sizeof(hashtable) = 24 Part B (10 pos) Suppose the entries are maained in a sorted linked in each bucket in order from small to large keys. Complete the C function Find_Key that efficiently searches the hash table for an entry corresponding to a specified key (i.e., it should end the search as early as possible). It should return a poer to the matching Entry if Key is found or return NULL if Key is not found in the hash table. Entry *Find_Key(HashTable *HT, Key) { Entry *ThisEntry; Index; Hash( Key); /* function prototype for hash function */ Index = Hash(Key); ThisEntry = HT->Buckets[Index]; while((thisentry!= NULL) && (ThisEntry->Key <= Key)){ if (ThisEntry->Key == Key) return(thisentry); else ThisEntry = ThisEntry->Next; return NULL; Part C (9 pos) Suppose a hash table created using the structs above contains 155 entries total and the entries are evenly distributed across the 5 hash table buckets, each implemented as a sorted linked list of Entry structs. An application performs 500 lookups of various keys: 375 of the lookups find the key in the list and 125 lookups fail to find the key. The keys that are found are distributed throughout the list so that each position is equally likely to be where a key is found. What is the average number of key comparisons that would be needed for a lookup in this list implementation? (Show work. Note: you may not have to use all data provided.) L=155/5 = 31 Avg # comparisons = (31+1)/2 = 16 Average number of comparisons: 16 3

Problem 4 (3 parts, 20 pos) Garbage Collection Below is a snapshot of heap storage. Values that are poers are denoted with a $. The heap poer is $6188. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr value addr value addr value addr value addr value addr value 6000 8 6032 12 6064 20 6096 16 6128 12 6160 $6000 6004 33 6036 28 6068 4 6100 $6052 6132 $6172 6164 0 6008 0 6040 $6120 6072 $6132 6104 6010 6136 $6016 6168 16 6012 16 6044 80 6076 8 6108 5 6140 72 6172 $6052 6016 $6100 6048 16 6080 $6148 6112 148 6144 20 6176 0 6020 $6172 6052 0 6084 $6172 6116 8 6148 6046 6180 $6000 6024 25 6056 100 6088 4 6120 32 6152 $6080 6184 0 6028 30 6060 0 6092 $6080 6124 $6080 6156 26 6188 0 Part A (9 pos) Suppose the stack holds a local variable whose value is the memory address $6120 and register $3 holds memory address $6016. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that would be marked by a mark-and-sweep garbage collection algorithm. Addresses of Marked Objects: 6120, 6080, 6148, 6000, 6172, 6052, 6016, 6100 Part B (3 pos) If a reference counting garbage collection strategy is being used, what would be the reference count of the object at address $6172? Reference count of object at $6172 = 2 Part C (8 pos) If the local variable whose value is the address $6120 is popped from the stack, which addresses will be reclaimed by each of the following strategies? If none, write none. Reference Counting: 6120 Mark and Sweep: 6120, 6080, 6148 Old-New Space (copying): 6120, 6080, 6148 4

Problem 5 (3 parts, 30 pos) MIPS and C programming Part A (8 pos) Suppose the instruction jal Foo is executed which changes the values of the following registers to: Register Value $31 3216 PC 3620 What is the address of the first instruction of the subroutine Foo and what is the address of the jal Foo instruction? Subroutine Foo starts at address: 3620 Address of jal Foo instruction: 3212 Part B (12 pos) Suppose variables A, B, and C are of type and are stored in registers $1, $2, and $3. Write a MIPS code fragment that computes C = A * min(a, B);. Use only registers $0, $1, $2, and $3 and for maximum credit, use a minimal number of instructions and include comments. Label Instruction Comment Min: End: slt $3, $1, $2 bne $3, $0, Min mult $1, $2 j End mult $1, $1 mflo $3 Part C (10 pos) What does the following code fragment pr? # is A<B? # if so, branch to Min # if not, compute A*B # and end # Min: A*A # put result in $3 A[10] = {1990, 3, 1992, 5, 1999, 1, 2001, 3, 2005, 1; B[10] = {1991, 2, 1993, 1, 1998, 7, 2002, 1, 2007, 6; i,j; for(i=1; i<10; i=i+2) { for (j=1; j<10; j=j+2) { if (A[i] == B[j]) { prf( C: %d, As: %d, Bs: %d.\n, A[i], A[i-1], B[j-1]); break; C: 1, As: 1999, Bs: 1993 C: 1, As: 2005, Bs: 1993 5

Problem 6 (40 pos) Activation Frames The function Bar (below left) calls function Foo after completing code block 1. Write MIPS code that properly calls Foo. Include all instructions between code block 1 and code block 2. Note that code block 1 may change the values of the local variables (e.g., assume i can be any value from 0 to 2). Symbolically label all required stack entries and give their initial values if known (below right). Bar() { A[] = {9, 25, 27; i = 0; y = 5; (code block 1) A[1] = Foo(A, &y, A[i]); (code block 2) Bar s FP 9900 XXX XXX 9896 A[2] 27 9892 A[1] 25 9888 A[0] 9 9884 i 0 SP 9880 y 5 9876 Bar s RA N/A 9872 Bar s FP 9900 9868 A 9888 9864 &y 9880 9860 A[i] A[i] 9856 label instruction comment addi $29, $29, -24 # allocate activation frame sw $31, 20($29) # preserve bookkeeping info sw $30, 16($29) addi $1, $30, -12 # compute A sw $1, 12($29) # push A addi $2, $30, -20 # compute &y sw $2, 8($29) # push &y lw $2, -16($30) # compute A[i] by loading i, sll $2, $2, 2 # scaling it, and adding it to add $2, $2, $1 # the base address of A lw $3, 0($2) # push A[i] sw $3, 4($29) jal Foo # call Foo lw $31, 20($29) # restore bookkeeping info lw $30, 16($29) lw $2, 0($29) # read return value sw $2, 8($1) # store return value in A[2] addi $29, $29, 24 # deallocate activation frame 6