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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

ECE 30 Introduction to Computer Engineering

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

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

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

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

MIPS function continued

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Lecture 5: Procedure Calls

MIPS%Assembly% E155%

Reduced Instruction Set Computer (RISC)

ECE260: Fundamentals of Computer Engineering

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

ECE 15B Computer Organization Spring 2010

Reduced Instruction Set Computer (RISC)

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

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Solutions for Chapter 2 Exercises

Architecture II. Computer Systems Laboratory Sungkyunkwan University

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

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

Lecture 4: MIPS Instruction Set

Assembly Programming

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

ECE260: Fundamentals of Computer Engineering

Computer Architecture. The Language of the Machine

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

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

Part II Instruction-Set Architecture. Jan Computer Architecture, Instruction-Set Architecture Slide 1

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

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

EE 361 University of Hawaii Fall

CS 61c: Great Ideas in Computer Architecture

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

Mips Code Examples Peter Rounce

CENG3420 Lecture 03 Review

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

Chapter 2. Instructions:

Instructions: Assembly Language

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

MIPS Functions and the Runtime Stack

Compiling Code, Procedures and Stacks

Computer Architecture. MIPS Instruction Set Architecture

MIPS Instruction Set Architecture (2)

MIPS Reference Guide

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

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

CS61C : Machine Structures

Week 10: Assembly Programming

Lecture 5: Procedure Calls

Storage in Programs. largest. address. address

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

MIPS Instruction Set

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.

CS61C : Machine Structures

Lecture 6 Decision + Shift + I/O

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

Computer Architecture

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

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

Control Instructions

We will study the MIPS assembly language as an exemplar of the concept.

SPIM Instruction Set

COMPUTER ORGANIZATION AND DESIGN

ECE Exam I February 19 th, :00 pm 4:25pm

Chapter 2A Instructions: Language of the Computer

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Lec 10: Assembler. Announcements

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

Midterm. CS64 Spring Midterm Exam

Programming the processor

Lecture 7: Procedures

CSE 2021: Computer Organization

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Transcription:

Problem 1 (20 points) 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 cube, g, and h are pure functions that each return an integer with no side effects to other data structures. int cube (int n) { return (n*n*n); int g (int k) { int h (int i, int j) { int slowcode(int a, int b) { int t = 100; int t = 100; int tmp; int p = 1, s=0; int p = 1; do { do { if (s) p += t*cube(p); else p += h(s+a*t, p+a*t); printf("t:%d, p:%d\n",t,p); t++; tmp = a*t; p += h(tmp, p+tmp); while(t<g(p/256) + h(a, b)); while(t<g(p>>8) + h(a, b)); return h(p, t); return h(p, t); 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 (s=0): reduce storage required and operations to allocate/deallocate it as a local. 2. dead code elimination: if (0) always zero, so no need for if or then clause, reduces code length and eliminates branch operation 3. algebraic simplification: (s+a*t = 0+a*t = a*t): reduces number of operations 4. strength reduction (p/256 = p>>8): turns division into a simpler shift operation 5. common subexpression elimination (a*t): the subexpression is only computed once. 1

Problem 2 (2 parts, 40 points) MIPS and C Programming Part A (16 points) Given two arrays A and B, of 64 integers, write a C fragment that loops through the two arrays and computes the average difference of the corresponding elements (i.e., A[i] B[i] for i from 0 to 63) and assigns the result to the variable avgdiff. The C fragment should also compute the minimum and maximum of the differences and assign them to the variables mindiff and maxdiff, respectively. For maximum credit, declare and initialize any necessary variables. NOTE: A and B can be any 64-element integer arrays, not just the example given. Pay careful attention to how you initialize mindiff and maxdiff the minimum difference could be greater than 0 and the maximum difference might be less than 0. int A[64] = {-17, 2, 93, 9,... -14, 7; // given int B[64] = {-19, 5, 93, 7,... -14, 8; // given int Diff = A[0]-B[0]; int avgdiff = Diff; int mindiff = Diff; int maxdiff = Diff; int i; for (i = 1; i<64; i++){ Diff = A[i]-B[i]; avgdiff += Diff; if (Diff < mindiff) mindiff = Diff; if (Diff > maxdiff) maxdiff = Diff; avgdiff = avgdiff>>6; 2

Part B (24 points) Write MIPS code for the fragment in Part A. Store the avgdiff computed in register $4, the mindiff in register $5, and maxdiff in register $6. For maximum credit use a minimum number of instructions. A: B: Label Instruction Comment Loop: Skip: Skip2: Exit:.data.word -17, 2, 93, 9,... -14, 7.word -19, 5, 93, 7,... -14, 8.text addi $1, $0, 4 lw $7, A($0) lw $8, B($0) sub $9, $7, $8 addi $4, $9, 0 addi $5, $9, 0 addi $6, $9, 0 slti $10, $1, 256 beq $10, $0, Exit lw $7, A($1) lw $8, B($1) sub $9, $7, $8 add $4, $4, $9 slt $10, $9, $5 beq $10, $0, Skip addi $5, $9, 0 slt $10, $6, $9 beq $10, $0, Skip2 addi $6, $9, 0 addi $1, $1 4 j Loop sra $4, $4, 6 given given initialize i ($1) read A[0] read B[0] Diff = A[0] - B[0] init avgdiff = Diff init mindiff = Diff init maxdiff = Diff is i < 64 ($1<256)? if not, exit loop read A[i] read B[i] Diff = A[i] - B[i] running sum of Diff is Diff < mindiff? if not, Skip update mindiff = Diff is Diff > maxdiff? if not, Skip2 update maxdiff = Diff i++ keep looping avgdiff = avgdiff/64 3

Problem 3 (4 parts, 20 points) Short Answer Part A (4 points) Write a single MIPS instruction that is equivalent to the original fragment. Assume little endian byte ordering. Original: lui $4, 0xFF00 Equivalent MIPS statement: lw $3, 1000($0) lbu $3, 1003($0) and $3, $3, $4 srl $3, $3, 24 Part B (4 points) Suppose the instruction jal Foo is at instruction memory address 2020 and Foo is a label of an instruction at memory address 4040. When this instruction is executed, what changes occur to the registers. List all registers that are changed (both general purpose and special purpose) and give their new values. Register Value $31 2024 PC 4040 Part C (6 points) For each of the following, write a single MIPS instruction to implement the C fragment? Assume variables A, B, C, and D are of type int and are stored in registers $1, $2, $3, and $4. A = B & 7; andi $1, $2, 7 C = D / 256; sra $3, $4, 8 Part D (6 points) Consider the MIPS code on the left which implements the array declaration and access on the right, where the variables Z, Y, X, and Value reside in $4, $5, $6, and $7 respectively. addi $1, $0, 48 mult $1, $4 mflo $1 sll $2, $5, 4 add $1, $1, $2 add $1, $1, $6 sll $1, $1, 2 sw $7, Array($1) int Z, Y, X, Value; int Array[? ][ 3 ][ 16 ]; Array[Z][Y][X] = Value; What does this code reveal about the dimensions of Array? Fill in the blanks in the array declaration with the size of each dimension that can be determined from the code. If a dimension cannot be known from this code, put a? in its blank. Assume a 32-bit operating system. 4

Problem 4 (4 parts, 25 points) Garbage Collection Below is a snapshot of heap storage. Values that are pointers are denoted with a $. The heap pointer 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 16 6096 12 6128 8 6160 8 6004 33 6036 28 6068 4 6100 $6004 6132 60 6164 0 6008 40 6040 12 6072 55 6104 $6016 6136 75 6168 16 6012 12 6044 $6016 6076 8 6108 $6176 6140 16 6172 12 6016 0 6048 $6100 6080 6148 6112 12 6144 20 6176 $6132 6020 $6100 6052 $6116 6084 8 6116 $6032 6148 6046 6180 $6100 6024 $6088 6056 4 6088 4 6120 $6176 6152 80 6184 $6116 6028 8 6060 0 6092 40 6124 0 6156 26 6188 0 Part A (9 points) Suppose the stack holds a local variable whose value is the memory address $6044. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that are not garbage. Addresses of Non-Garbage Objects: 6044, 6016, 6088, 6100, 6004, 6176, 6132, 6116, 6032 Part B (6 points) If a reference counting garbage collection strategy is being used, what would be the reference count of the objects at the following addresses? Reference count of object at $6044 = 1 Reference count of object at $6100 = 3 Reference count of object at $6116 = 2 Part C (6 points) If the local variable whose value is the address $6044 is popped from the stack, which addresses from Part A will be reclaimed by each of the following strategies? If none, write none. Reference Counting: 6044 Mark and Sweep: 6044, 6016, 6088, 6100, 6004, 6176, 6132, 6116, 6032 Part D (4 points) What benefit does reference counting garbage collection provide that mark and sweep garbage collection strategy does not provide? Benefit: It is incremental, no need to stop and collect. It is not cache-hostile. It is simple and more efficient. 5

Problem 5 (2 parts, 30 points) Doubly Linked Lists Consider a doubly linked list that is implemented using the following struct definitions. NOTE: These are the same as the structs used in Project 2-1, except the data field in llnode_t is of type int and the DLinkedList has no size field. typedef struct llnode_t { int data; struct llnode_t* previous; struct llnode_t* next; LLNode; typedef struct dll_t { struct llnode_t* head; struct llnode_t* tail; struct llnode_t* current; DLinkedList; Part A (12 points) Assume a 32-bit system and consider the following create_dlinkedlst function: DLinkedList* create_dlinkedlist() { DLinkedList* newlist = (DLinkedList*)malloc(sizeof(DLinkedList)); newlist->head = NULL; newlist->tail = NULL; newlist->current = NULL; return newlist; A.1 What integer is passed to malloc when this function executes? 12. A.2 Which region of memory holds the variable newlist? stack. A.3 How much space (in bytes) is allocated for newlist in this region of memory? 4 bytes. A.4 How much space (in bytes) is allocated for the return value of create_dlinkedlst? 4 bytes. Part B (18 points) Complete the C function Insert_Node_After that takes a pointer to an LLNode and inserts it after the current node in the doubly linked list pointed to by the input parameter DLL. Return 0 if the current node is NULL, otherwise return 1 (this code is already provided). Be sure to update the tail of DLL if N becomes the new tail. DLL s current field should not change. int Insert_Node_After (LLNode *N, DLinkedList *DLL) { if(dll->current == NULL){ return 0; else{ LLNode *C = DLL->current; N->previous = C; N->next = C->next; if (C == DLL->tail) DLL->tail = N; else (C->next)->previous = N; C->next = N; return 1; 6

Problem 6 (2 parts, 40 points) Consider the following C code fragment: typedef struct { int Start; int End; trip_info_t; int TripAdvisor() { int odometer = 981005; int Gallons[] = {16, 6; trip_info_t TI; int rate; int Update(trip_info_t, int [], int *); TI.Start = 180; TI.End = 420; rate = Update(TI, Gallons, &odometer); return(odometer); int Update(trip_info_t Trip, int G[], int *OD) { int miles, MPG; miles = Trip.End Trip.Start; MPG = miles/g[1]; *OD += miles; return(mpg); Activation Frames Part A (18 points) Suppose TripAdvisor has been called so that the state of the stack is as shown below. Describe the state of the stack just before Update deallocates locals and returns to TripAdvisor. Fill in the unshaded boxes to show TripAdvisor's and Update's activation frames. Include a symbolic description and the actual value (in decimal) if known. For return addresses, show only the symbolic description; do not include a value. Label the frame pointer and stack pointer. address description Value 9900 RA of TA s caller 9896 FP of TA s caller SP, TripAdvisor's FP 9892 RV 9888 odometer 981245 9884 Gallons[1] 6 9880 Gallons[0] 16 9876 TI.End 420 9872 TI.Start 180 9868 rate 9864 RA 9860 FP 9892 9856 Trip.End 420 9852 Trip.Start 180 9848 G 9880 9844 OD 9888 9840 RV FP: 9840 9836 miles 240 SP: 9832 9832 MPG 40 7

Part B (22 points) Write MIPS code fragments to implement the subroutine Update by following the steps below. Do not use absolute addresses in your code; instead, access variables relative to the frame pointer. Assume no parameters are present in registers (i.e., access all parameters from Update s activation frame). You may not need to use all the blank lines provided. First, write code to properly set Update s frame pointer and to allocate space for Update s local variables and initialize them if necessary. Update: add $30, $29, $0 set FP addi $29, $29, -8 allocate locals miles = Trip.End Trip.Start; lw $1, 12($30) read T.Start lw $2, 16($30) sub $1, $2, $1 sw $1, -4($30) read T.End T.End-T.Start store in miles MPG = miles/g[1]; lw $2, 8($30) lw $2, 4($2) div $1, $2 read G (base address) read G[1] miles/g[1] mflo $3 result in $3 sw $3, -8($30) store in MPG *OD += miles; lw $4, 4($30) lw $2, 0($4) add $5, $2, $1 sw $5, 0($4) read OD (address) dereference it *OD + miles *OD = *OD+miles return(mpg); (store return value, deallocate locals, and return) sw $3, 0($30) add $29, $30, $0 jr $31 put MPG in RV slot deallocate locals return to caller 8