Final CSE 131B Spring 2004

Similar documents
Final CSE 131B Spring 2005

Final CSE 131B Winter 2003

Midterm CSE 131B Spring 2005

Midterm CSE 131 Winter 2014

Final CSE 131 Winter 2010

Midterm CSE 131 Winter 2012

Midterm CSE 131 Fall 2014

Final CSE 131 Fall 2014

Final CSE 131 Winter 2012

CSE 30 Spring 2006 Final Exam

CSE 30 Fall 2013 Final Exam

CSE 30 Winter 2014 Final Exam

CSE 30 Fall 2012 Final Exam

Midterm CSE 131 Winter 2013

CSE 30 Fall 2006 Final Exam

CSE 30 Spring 2007 Final Exam

CSE 30 Fall 2007 Final Exam

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

Final CSE 131 Fall 2015

CSE 30 Winter 2009 Final Exam

Midterm CSE 131B Spring 2006

Kurt Schmidt. October 30, 2018

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

211: Computer Architecture Summer 2016

CSE 5A Final Fall 2006

Midterm CSE 131 Winter 2015

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

Programming in C - Part 2

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS16 Exam #1 7/17/ Minutes 100 Points total

Introduction to Programming Using Java (98-388)

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

PRINCIPLES OF OPERATING SYSTEMS

CSE 333 Midterm Exam 7/29/13

Common Misunderstandings from Exam 1 Material

CSE 374 Programming Concepts & Tools

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Lecture 03 Bits, Bytes and Data Types

Exercise Session 2 Systems Programming and Computer Architecture

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CSE 333 Midterm Exam Sample Solution 7/29/13

ECEN 449 Microprocessor System Design. Review of C Programming

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

Procedure and Object- Oriented Abstraction

High Performance Computing in C and C++

CS3157: Advanced Programming. Announcement

QUIZ. What are 3 differences between C and C++ const variables?

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Exercise Session 2 Simon Gerber

EL2310 Scientific Programming

Lecture 3: C Programm

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

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

Arrays and Pointers in C. Alan L. Cox

CS C Primer. Tyler Szepesi. January 16, 2013

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Short Notes of CS201

Lecture 2, September 4

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

FUNCTIONS POINTERS. Pointers. Functions

CS201 - Introduction to Programming Glossary By

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Understanding Pointers

Dynamic memory allocation

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Intermediate Code Generation

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

POINTER AND ARRAY SUNU WIBIRAMA

We would like to find the value of the right-hand side of this equation. We know that

C introduction: part 1

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Motivation was to facilitate development of systems software, especially OS development.

Creating a C++ Program

CSE 333 Midterm Exam Sample Solution 5/10/13

Lectures 5-6: Introduction to C

CSE 401/M501 Compilers

Topics Introduction to Microprocessors

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CS 415 Midterm Exam Spring 2002

CSCI 171 Chapter Outlines

Object-Oriented Programming, Iouliia Skliarova

CSE 333 Midterm Exam 5/10/13

CSE 333 Lecture 2 Memory

CS 61c: Great Ideas in Computer Architecture

CSE 12 Spring 2016 Week One, Lecture Two

Advanced Systems Programming

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Data Representation and Storage. Some definitions (in C)

Transcription:

Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points) (23 points) Subtotal Page 9 Extra Credit (204 points) (10 points) Total 0

1a. Consider the following C/C++ program: #include <stdio.h> int main() { int y = 1; int x[5]; int i; for ( i = 0; i <= 5; ++i ) x[i] = i; printf( "%d\n", y ); /* Print the value of y */ printf( "%p\n", &y ); /* Print the address of y */ return 0; Explain why the first printf() statement prints the value 5 when compiled with some compiler on some architecture (it does on SPARC with cc and CC)? (5 points) Explain why the first printf() statement prints the value 1 when compiled with the same compiler on the same architecture as above but with the second printf() statement removed? (5 points) 1b. C/C++ structs are assignable only if they are equivalent types. Two structs that are not equivalent cannot be assigned even with a cast. Why? (5 points) How can you get around this limitation? Give a specific example. (5 points) Why is this OKAY? (5 points) [Hint: Discuss types of equivalence used in your answers] 1

2. The following C function is one way to calculate whether a year is a leap year. In particular, it uses several constructs that were part of Project 2 Code Generation: if-else conditional, modulus operator, boolean expressions containing equality checks, and logical AND and OR expressions with short-circuiting. Using your hard-earned code generation talents from Project 2, translate this into SPARC assembly. Nothing fancy, no optimizations, just perform a direct translation keeping in mind short-circuiting semantics. (20 points) int leapyear( unsigned int year ) { if ( (year % 400 == 0) ((year % 4 == 0) && (year % 100!= 0)) ) return 1; else return 0; Given the array declaration C int a[3][2]; Oberon-like VAR a : ARRAY 3, 2 OF INTEGER Mark with an A the memory locations where we would find (4 points) a: a[2][0] a[2,0] low memory high memory 2

3. Why is passing and returning references or pointers to structs/objects as arguments and return values usually recommended over passing/returning structs/objects by value in most languages? (4 points) What gets printed? (28 points) VAR x : INTEGER; VAR y : BOOLEAN; PROCEDURE foo1( a : BOOLEAN; VAR b : INTEGER ) : INTEGER; VAR i : INTEGER; VAR j : BOOLEAN; i := b; b := 77; j := a; a := FALSE; OUTPUT x, " ", y; OUTPUT a, " ", b; OUTPUT i, " ", j; RETURN i; END foo1; PROCEDURE foo( a : INTEGER; VAR b : BOOLEAN ); VAR i, j : INTEGER; i := a; a := 66; j := foo1( b, a ); b := TRUE; OUTPUT x, " ", y; OUTPUT a, " ", b; OUTPUT i, " ", j; END foo; x := 55; y := FALSE; foo( x, y ); OUTPUT x, " ", y; END. 3

4. Identify where each of the following program parts live in the Java runtime environment as discussed in class. (12 points) public class Foo { private static Foo a; a private int b; b public Foo() { Foo() a = this; this ++b; main() public static void main( String[] args ) { args Foo c = new Foo(); c int d; d c = new Foo(); where c is pointing c.method( d ); method() private void method( int e ) { e int f; f f = e; Using the Right-Left rule (which follows the operator precedence rules) write the definition of a variable named foo that is a pointer to an array of 9 elements where each element is a pointer to a function that takes a pointer to a struct Pub as the single parameter and returns a pointer to a 7x19 2-D array where each element is a pointer to a struct Fubar. (6 points) Regarding type checking, reference (VAR) parameters require the actual arguments to be and to the formal parameter type while value parameters require the actual arguments to be to the formal parameter type. (6 points) 4

5. Given the following program, order the printf() lines so that the values that are printed when run on a Sun SPARC Unix system are displayed from smallest value to largest value and label the corresponding C/C++ Runtime area. (20 points) void foo( int, int * ); /* Function Prototype */ int a; int main( int argc, char *argv[] ) { smallest value Runtime (low memory) Area static int b = 420; int c = 404; foo( argc, &c ); /* 1 */ (void) printf( "a --> %p\n", &a ); /* 2 */ (void) printf( "b --> %p\n", &b ); /* 3 */ (void) printf( "c --> %p\n", &c ); /* 4 */ (void) printf( "argc --> %p\n", &argc ); /* 5 */ (void) printf( "malloc --> %p\n", malloc(50) ); /* 6 */ (void) printf( "foo --> %p\n", foo ); void foo( int d, int *e ) { int f = 911; int g; /* 7 */ (void) printf( "d --> %p\n", &d ); /* 8 */ (void) printf( "e --> %p\n", &e ); /* 9 */ (void) printf( "f --> %p\n", &f ); /* 10 */ (void) printf( "g --> %p\n", &g ); largest value (high memory) Why do compilers typically allocate space for arguments in the Runtime Stack even when they pass them in registers? (4 points) Object-oriented languages allow operator, function, and constructor overloading. In these languages, the function name is not always a unique identifier, since you can have multiple related definitions. For lookup purposes, the compiler must construct a distinct identifier for each function. Sometimes, such overloaded functions will have different return types as well. How would you create distinct identifiers for such functions? (4 points) 5

6. Given the following Oberon program, emit the unoptimized SPARC assembly language code that should be generated for the two procedures foo1() and foo(). Assume no optimizations treat each instruction separately without any knowledge of any previously computed/loaded/stored values that may still be in a register from a previous instruction. Draw a line between each group of assembly language instructions that represent the emitted code generated for each instruction and label them with the instruction number. All local variables must be allocated and accessed on the Stack (do not map them directly into a local register). (26 points) PROCEDURE foo1( VAR x : INTEGER; VAR y : INTEGER ) : INTEGER; VAR i, j : INTEGER; (**** Local Stack variables Do not initialize to zero ****) j := y + 5; (* 1 *) i := x; (* 2 *) y := j - 10; (* 3 *) RETURN x; (* 4 *) END foo1; PROCEDURE foo( a : INTEGER; VAR b : INTEGER ); VAR i, j : INTEGER; (**** Local Stack variables Do not initialize to zero ****) i := foo1( a, b ); (* 5 *) b := foo1( i, j ); (* 6 *) (* Don't worry about any local variables not initialized *) END foo; (*... *) END..global foo, foo1, main foo1:.section ".text" foo: 6

7. Show the memory layout of the following C struct/record definition taking into consideration the SPARC data type memory alignment restrictions discussed in class. Fill bytes in memory with the appropriate struct/record member/field name. For example, if member/field name p takes 4 bytes, you will have 4 p's in the appropriate memory locations. If the member/field is an array, use the name followed by the index number. For example, some number of p0's, p1's, p2's, etc. Place an X in any bytes of padding. Structs are padded so the total size is evenly divisible by the most strict alignment requirement of its members. (11 points) struct foo { char a; fubar: short b; double c; short d[3]; int e; low memory struct foo fubar; Generate SPARC assembly code for the 3 labeled statements below. Assume the two local variables have been allocated on the Stack with an appropriate save instruction. Draw a line between and label the group of translated instructions for each labeled line. (11 points) PROCEDURE P(); VAR i : INTEGER; VAR ptr : POINTER TO INTEGER; NEW( ptr ); (* 1 *) ptr^ := 420; (* 2 *) i := ptr^; (* 3 *) END P; END. 7

8. Give the order of the phases of compilation in a typical compiler as discussed in class (8 points) A Machine-specific code improvement (optional) C Parser (Semantic analysis/intermediate code gen.) E Machine-independent code improvement (optional) G Source language (for example, C) B Scanner (lexical analysis) D Parser (syntax analysis) F Target code generation H Target language (for ex., assembly) > > > > > > > Give the order of the typical C compilation stages and on to actual execution as discussed in class (8 points) B loader D ld (Linkage Editor) C exe/a.out (executable image) F ccomp (C compiler) G cpp (C preprocessor) E as (assember) H Source file A Program Execution gcc > > > > > > > How did you implement the OUTPUT function in your Project 2? (5000 points) Tell me something you learned in this class that is extremely valuable and that you think you will be able to use for the rest of your programming/computer science career. (2 points) 8

9. Extra Credit (10 points) Given the following ANSI/ISO C/C++ variable definitions, identify which expressions will produce a static semantic compiler error. A) No compiler error B) Compiler error int main() { char * s = "CSE 131B Rocks!"; char * p1 = &s[4]; const char * p2 = s; char * const p3 = s + 7; const char * const p4 = &*(s + 9); p1 = s; *p1 = 'A'; p2 = s; *p2 = 'A'; p3 = s; *p3 = 'A'; p4 = s; *p4 = 'A'; *&p1[9] = *(s + 1); ((char *) p4) = (char *) p3; Note: cc, CC, and g++ report these as errors; gcc reports these as warnings! :-/ 9

Scratch Paper 10

Scratch Paper 11