Final CSE 131 Winter 2010

Similar documents
Final CSE 131 Fall 2014

Final CSE 131 Winter 2012

Midterm CSE 131 Winter 2012

Midterm CSE 131 Winter 2014

Final CSE 131 Fall 2015

Midterm CSE 131 Winter 2013

Midterm CSE 131 Winter 2015

Midterm CSE 131 Fall 2014

CSE 30 Fall 2012 Final Exam

Final CSE 131B Spring 2004

CSE 30 Winter 2014 Final Exam

CSE 30 Fall 2013 Final Exam

CSE 30 Winter 2009 Final Exam

CSE 30 Spring 2006 Final Exam

Final CSE 131B Spring 2005

CSE 30 Fall 2006 Final Exam

CSE 30 Spring 2007 Final Exam

CSE 30 Fall 2007 Final Exam

Final CSE 131B Winter 2003

Do not start the test until instructed to do so!

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Chapter 2 Bits, Data Types, and Operations

Midterm CSE 131B Spring 2005

Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Chapter 8. Characters and Strings

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Fundamentals of Programming (C)

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CS 159 Credit Exam. What advice do you have for students who have previously programmed in another language like JAVA or C++?

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Data Representation and Binary Arithmetic. Lecture 2

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

6.096 Introduction to C++ January (IAP) 2009

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

1 Lexical Considerations

Fundamentals of Programming

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Fundamental of Programming (C)

ASSIGNMENT 5 TIPS AND TRICKS

Oberon Data Types. Matteo Corti. December 5, 2001

Number Representations

The Binary Number System

Fundamental Data Types

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

CS2141 Software Development using C/C++ C++ Basics

Simple Data Types in C. Alan L. Cox

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Lexical Considerations

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Appendix A Developing a C Program on the UNIX system

Variables and literals

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

First of all, it is a variable, just like other variables you studied

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

Chapter 2 Number System

Kurt Schmidt. October 30, 2018

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lexical Considerations

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Number System (Different Ways To Say How Many) Fall 2016

Review of the C Programming Language for Principles of Operating Systems

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

EE 109 Unit 2. Binary Representation Systems

Chapter 3. Information Representation

Review of the C Programming Language

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

CSE 5A Final Fall 2006

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Fundamentals of Programming

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

CS 415 Midterm Exam Spring SOLUTION

a data type is Types

Question Points Score Total: 100

Transcription:

Student ID Login Name Name Signature _ Final CSE 131 Winter 2010 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 _ (26 points) _ (15 points) _ (36 points) _ (25 points) _ (23 points) _ (14 points) _ (15 points) _ (19 points) _ (13 points) _ (22 points) _ (11 points) Subtotal _(219 points = 100%) Page 12 Extra Credit _ (14 points) [over 6% EC] Total _ 0

1 Given the following CUP grammar snippet (assuming all other Lexing and terminals are correct): Expr ::= Des AssignOp Expr : Systemoutprintln("0"); : Des : Systemoutprintln("1"); : ; Des ::= T_STAR : Systemoutprintln("2"); : Des : Systemoutprintln("10"); : T_AMPERSAND : Systemoutprintln("3"); : Des : Systemoutprintln("11"); : T_PLUSPLUS : Systemoutprintln("4"); : Des : Systemoutprintln("12"); : Des2 : Systemoutprintln("5"); : ; Des2 ::= Des2 : Systemoutprintln("6"); : T_PLUSPLUS : Systemoutprintln("13"); : Des3 : Systemoutprintln("7"); : ; Des3 ::= T_ID : Systemoutprintln("8"); : ; AssignOp ::= T_ASSIGN : Systemoutprintln("9"); : ; What is the output when parsing the follow expression (you should have 24 lines/numbers in your output): Output x = *y++ = &*++z; If variable x is defined to be type float *, what types must variables y and z be defined for this expression to be semantically correct? _ y; _ z; 1

2 Fill in the blanks with one of the following A) Front End B) Intermediate Representation (IR) C) Back End The semantic analyzer is considered part of A complete AST is considered part of Machine independent code improvements are usually performed in _ Machine dependent code improvements are usually performed in The parser is considered part of The code generator is considered part of The lexical analyzer is considered part of Suppose that in the next version of Reduced-C, we extend support for the types that can be used for if-statement and while-statement conditional expressions Specifically, we now allow expressions of type int for conditional expressions in addition to type bool What this means is that the following code int i = 6; while( i-- ) if( i % 2 ) cout << i; is legal and outputs 531 when executed Basically, an implicit typecast to bool is assumed for the conditional expressions Another way to look at it is that an implicit "!= 0" is appended to the conditional expression (for example: (i--)!= 0 and (i % 2)!= 0 ), making the resulting expression be of type bool (1) Give a high-level explanation of what changes, if any, will need to be made to the semantic analysis part of the compiler (Project I), which is involved with name bindings and type checking (2) Give a high-level explanation of what changes, if any, will need to be made to the code generation part of the compiler (Project II) Circle T for true or F for false: T or F In our CUP grammar, operators with high precedence are high in the chain of Expr production rules T or F In our CUP grammar, operators with high precedence are high in the parse T or F In our CUP grammar, unary operator rules are written with left recursion to support left associativity T or F In our CUP grammar, we use the maximal munch principle so expressions like a+++++b parse the same as a++ + ++b 2

3 In object-oriented languages like Java, determining which overloaded method code to bind to (to execute) is done at run time rather than at compile time (this is known as dynamic dispatching or dynamic binding) However, the name mangled symbol denoting a particular method name is determined at compile time Given the following Java class definitions, specify the output of each print() method invocation class Curly public void print(curly l) Systemoutprintln("Curly 1"); class Moe extends Curly public void print(curly c) Systemoutprintln("Moe 1"); public void print(moe m) Systemoutprintln("Moe 2"); public class Overloading_Final_Exam public static void main (String [] args) Curly stooge1 = new Moe(); Curly stooge2 = new Larry(); Curly stooge3 = new Curly(); Moe stooge4 = new Larry(); Moe stooge5 = new Moe(); Larry stooge6 = new Larry(); class Larry extends Moe public void print(moe m) Systemoutprintln("Larry 1"); public void print(larry l) Systemoutprintln("Larry 2"); public void print(curly c) Systemoutprintln("Larry 3"); Now remove the entire method print(moe m) in class Moe and remove the entire method print(curly c) in class Larry Specify the output of each print() method with these changes below stooge1print( stooge6 ); stooge2print( stooge1 ); stooge3print( stooge4 ); stooge4print( stooge5 ); stooge5print( stooge6 ); stooge6print( stooge6 ); stooge1print( (Moe) stooge6 ); stooge2print( (Moe) stooge1 ); stooge3print( (Larry) stooge4 ); stooge4print( (Curly) stooge5 ); stooge5print( (Moe) stooge6 ); stooge6print( (Moe) stooge6 ); ( (Moe) stooge1)print( (Moe) stooge6 ); ( (Larry) stooge2)print( (Moe) stooge1 ); ( (Curly) stooge3)print( (Larry) stooge4 ); ( (Moe) stooge4)print( (Curly) stooge5 ); ( (Curly) stooge5)print( (Moe) stooge6 ); ( (Moe) stooge6)print( (Moe) stooge6 ); 3

4 In your Project 2, how did you (your group) handle code gen for short-circuiting with the logical AND and logical OR operators? Be specific how your project implemented this! Using Reduced-C syntax, first define a struct A with members of type bool and pointer to struct A named x and ptr, respectively Then define a variable named foo which is an array of an array (with dimensions 7x4) of pointers to struct A such that foo[0][0]->ptr = foo[6][3]; is a valid expression This will take more than one line of Reduced-C code You can use two columns in the space below if you wish: struct definition on the left, array definition on the right For each of the following make no assumptions of what may be above or below each window of instructions unless otherwise stated Use virtual register notation Change the following into three instructions which are most likely a time improvement over the single instruction when it comes to actual code generation x represents a memory location r5 = r3 + r7 x = r5 r5 = x r7 = r6 + r5 x = r7 What term describes this particular kind of peephole optimization? Change the following into three instructions which are most likely a time improvement over the single instruction when it comes to actual code generation r1 = r2 * 130 What term describes this particular kind of peephole optimization? 4

5 What gets printed in the following C++ program (just like Reduced-C without "function : " in front of each function definition)? If a value is unknown/undefined or otherwise cannot be determined by the code given, put a question mark ('?') for that output Hint: Draw stack frames! int a = 45; int b = 67; int c = 89; void fubar( int * x, int & y, int z ) ++*x; ++y; ++z; void foo1( int d, int * e, int & f ) ++d; ++*e; ++f; cout << a << endl; _ cout << b << endl; _ cout << c << endl; _ cout << d << endl; _ cout << *e << endl; _ cout << f << endl; _ fubar( &d, d, d ); fubar( e, *e, *e ); fubar( &f, f, f ); cout << a << endl; _ cout << b << endl; _ cout << c << endl; _ cout << d << endl; _ cout << *e << endl; _ cout << f << endl; _ int main() foo1( a, &b, c ); cout << a << endl; _ cout << b << endl; _ cout << c << endl; _ return 0; Using the Right-Left rule write the C definition of a variable named foo that is a pointer to an array of 7 elements where each element is a pointer to a function that takes a pointer to a pointer to a double as a single parameter and returns a pointer to a 2-d array of 4 rows and 13 columns where each element is a pointer to a struct bar 5

6 Global and static variables are only initialized once and initialization guards can be used to enforce this property for each global and static variable However, in certain cases, an initialization guard can be avoided by utilizing the data or bss section to initialize a global or static variable Consider the following Reduced-C program: const int negone = -1; static int uig; static int cig = 5 + negone; bool rig = uig == cig; function : int foo( int i ) static int ril = i + 1; static bool uil; //A //B //C //D //E if(uil) return cig * ril * negone; uil = true; return cig; function : void main() while( --cig > 1 ) static int cil = 3; //F cout << foo( ++cil + cig ) << endl; /* May be other code here that does not output anything */ Each global and static variable is marked with a letter A-F For each, indicate below whether an initialization guard is absolutely needed (answer "Y") or not needed (answer "N") Give a very brief explanation for why or why not Y/N Reason (A) (B) (C) (D) (E) (F) What is the output of the above program? 6

7 Given the C array declaration C int a[3][2]; Mark with an A the memory location(s) where we would find a: a[1][1] low memory high memory Each box above represents a byte in memory Why do function pointers in Reduced-C need to be 8 bytes? Be specific With respect to the assembly code generated, what is different between a global variable definition and a static variable definition? For example, int x; vs static int x; Be specific With respect to the assembly code generated, what is different between accessing a global variable that is defined in one file and accessing an extern declaration of a global variable of the same name declared in another file? For example, int y; y = 5; vs extern int y; y = 5; This question asks only about the assembly code to access the variable ( y = 5; ) and nothing else Be specific What is the output of the following C++ program (similar to this quarter's Reduced-C spec)? void foo( int x ) static int y = x - 2; cout << y++ << endl; if ( x >= 4 && y <= 8 ) foo( y - 2 ); else cout << "Whoa!" << endl; cout << ++y << endl; int main() foo( 6 ); return 0; 7

8 Given the following program, specify the order of the output lines when run and sorted by the address printed with the %p format specifier on a Sun SPARC Unix and Linux system For example, which line will print the lowest memory address, then the next higher memory address, etc up to the highest memory address? #include <stdioh> #include <stdlibh> void foo1( int *, int ); /* Function Prototype */ void foo2( int, int * ); /* Function Prototype */ int main( int argc, char *argv[] ) int a = 42; int b; foo1( &argc, a ); /* 1 */ (void) printf( "1: foo1 --> %p\n", foo1 ); /* 2 */ (void) printf( "2: b --> %p\n", &b ); /* 3 */ (void) printf( "3: argc --> %p\n", &argc ); /* 4 */ (void) printf( "4: a --> %p\n", &a ); /* 5 */ (void) printf( "5: argv --> %p\n", &argv ); void foo1( int *c, int d ) int e; int f = d; static struct foo int a; int b; g; foo2( f, c ); /* 6 */ (void) printf( "6: ga --> %p\n", &ga ); /* 7 */ (void) printf( "7: d --> %p\n", &d ); /* 8 */ (void) printf( "8: e --> %p\n", &e ); /* 9 */ (void) printf( "9: f --> %p\n", &f ); /* 10 */ (void) printf( "10: malloc-1 --> %p\n", c = (int *) malloc(50) ); /* 11 */ (void) printf( "11: gb --> %p\n", &gb ); /* 12 */ (void) printf( "12: c --> %p\n", &c ); void foo2( int h, int *i ) int j[3]; static int k = 411; /* 13 */ (void) printf( "13: h --> %p\n", &h ); /* 14 */ (void) printf( "14: j[1] --> %p\n", &j[1] ); /* 15 */ (void) printf( "15: i --> %p\n", &i ); /* 16 */ (void) printf( "16: k --> %p\n", &k ); /* 17 */ (void) printf( "17: j[0] --> %p\n", &j[0] ); /* 18 */ (void) printf( "18: malloc-2 --> %p\n", i = (int *) malloc(150) ); smallest value (lowest memory address) largest value (highest memory addresses) Variables declared to be will not be optimized by the compiler 8

9 Given the following C++ program (whose semantics in this case is similar to our Reduced-C) and a real compiler's code gen as discussed in class, fill in the values of the global and local variables and parameters in the run time environment for the SPARC architecture when the program reaches the comment /* HERE */ Do not add any unnecessary padding struct fubar int * a; float b; int c; ; int a; float b = 125; void foo( float & f, int i ) struct fubar var1[2]; int var2; int * var3; var2 = 375; var3 = (int *) calloc( 1, sizeof(int) ); f = -375; var1[1]c = i + 5; *var3 = var2-3; var1[0]a = &var1[1]c; i = 750; var1[0]c = a - 5; var1[1]a = &i; var1[0]b = b; var1[1]b = f + 3; /* HERE */ hypothetical decimal memory locations a: 1000 b: Heap low memory 4000 80360 free( var3 ); int main() foo( b, a ); return 0; %fp 80460 high memory 9

10 Given the following Reduced-C code, write the equivalent non-optimized SPARC assembly code that should be generated by a compliant compiler following this quarter's specs Be sure to allocate all local variables and temporaries on the run time stack Store and access all formal parameters on the run time stack /* Reduced-C */ function : int foo( int & a, int x ) int b; int c = 42; int * d; b = a; d = &x; /* Equivalent SPARC Assembly code */ return c - b; To make it easiest for us to give you as many points possible, comment each group of instructions For example:! The next X lines translate the stmt! b = a assembly instr 1 assembly instr 2 Make it very clear which group of assembly instructions you want us to grade for each part of the original source code Draw lines between or rectangles around groups of assembly instructions that are logically associated with a task or instruction from the original source code 10

11 Name the part of the compilation sequence which performs the following Use the letters below associated with the compilation sequence parts A) Compiler B) Loader C) Preprocessor D) Linkage Editor E) Assembler expands # directives & strips comments from its input high-level language (HLL) translates assembly code into machine code performs syntax analysis on its input high-level language (HLL) takes an executable file on disk and makes it ready to execute in memory translates HLL code (for example, C) into assembly code combines all object modules into a single executable file performs semantic analysis on its input high-level language (HLL) resolves undefined external symbols with defined global symbols in other modules Give an example of something (either in C/C++ or our Reduced-C) that is: a) an r-value (neither addressable nor assignable) b) a non-modifiable l-value (an object locator that is addressable but not assignable) c) a modifiable l-value (an object locator that is addressable and assignable) 11

12 Extra Credit What gets printed when this program is executed? #include <stdioh> int main() char a[] = "Foundations of Computer "; char b[] = "Science"; char *p1 = a + 1; char *p2 = b; printf( "%c", *p2++ ); printf( "%c", p2[2] ); printf( "%c", *++p1 ); _ printf( "%c", *(p1 = p1 + 4) - 1 ); _ printf( "%c", p1[4] ); printf( "%c", *++p2 ); printf( "%c", *--p2 ); printf( "%c", *p2-2 ); printf( "%c", p1[-3] -2 ); _ return 0; What is the output of the following Reduced-C program? Assume local variables are allocated in decreasing memory locations So, for example, i is at a lower memory address than f function : void main() float f = 325; float f2 = (float)&f; int i = (int)&f2; *(float *)((int)&i + (int)f2 - i) = (*(float *)f2)++; ++*(int **)&i; i = ((int *)i)[-2] - (int)&i; cout << f << " " << f2 << " " << i; Tell me something you learned in this class that is extremely valuable to you and that you think you will be able to use for the rest of your computer science career (1 point if serious; you can add non-serious comments also) Crossword Puzzle (next page) (1 point) 12

Hexadecimal - Character 00 NUL 01 SOH 02 STX 03 ETX 04 EOT 05 ENQ 06 ACK 07 BEL 08 BS 09 HT 0A NL 0B VT 0C NP 0D CR 0E SO 0F SI 10 DLE 11 DC1 12 DC2 13 DC3 14 DC4 15 NAK 16 SYN 17 ETB 18 CAN 19 EM 1A SUB 1B ESC 1C FS 1D GS 1E RS 1F US 20 SP 21! 22 " 23 # 24 $ 25 % 26 & 27 28 ( 29 ) 2A * 2B + 2C, 2D - 2E 2F / 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7 38 8 39 9 3A : 3B ; 3C < 3D = 3E > 3F? 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4A J 4B K 4C L 4D M 4E N 4F O 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5A Z 5B [ 5C \ 5D ] 5E ^ 5F _ 60 61 a 62 b 63 c 64 d 65 e 66 f 67 g 68 h 69 i 6A j 6B k 6C l 6D m 6E n 6F o 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7A z 7B 7C 7D 7E ~ 7F DEL A portion of the C Operator Precedence Table Operator Associativity ++ postfix increment L to R -- postfix decrement [] array element () function call -------------------------------------- * indirection R to L ++ prefix increment -- prefix decrement & address-of sizeof size of type/object (type) type cast -------------------------------------- * multiplication L to R / division % modulus -------------------------------------- + addition L to R - subtraction -------------------------------------- -------------------------------------- = assignment R to L 13

Scratch Paper 14

Scratch Paper 15