CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

Similar documents
CSE P 501 Exam 8/5/04

CSE 582 Autumn 2002 Exam Sample Solution

CSE P 501 Exam 11/17/05 Sample Solution

CSE 582 Autumn 2002 Exam 11/26/02

CSE 413 Winter 2001 Final Exam Sample Solution

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Computer Systems Organization V Fall 2009

Sample Exam I PAC II ANSWERS

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CSC 8400: Computer Systems. Machine-Level Representation of Programs

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

CSE P 501 Exam Sample Solution 12/1/11

Introduction to Compilers Professor Jeremy Siek

Second Part of the Course

CSE 401 Final Exam. December 16, 2010

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Overview of Compiler. A. Introduction

Towards the Hardware"

Intermediate Code Generation

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Program Exploitation Intro

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CSE351 Spring 2018, Midterm Exam April 27, 2018

Procedure-Calling Conventions October 30

Topic 6: Code Generation

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Summary: Direct Code Generation

CSE 401 Midterm Exam Sample Solution 11/4/11

Subprograms, Subroutines, and Functions

CSE P 501 Exam 12/1/11

Instructions and Instruction Set Architecture

ECE251 Midterm practice questions, Fall 2010

Subprograms: Local Variables

SOURCE LANGUAGE DESCRIPTION

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

UW CSE 351, Winter 2013 Midterm Exam

CSC 2400: Computer Systems. Using the Stack for Function Calls

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

CSC 8400: Computer Systems. Using the Stack for Function Calls

THEORY OF COMPILATION

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

CSE 5317 Midterm Examination 4 March Solutions

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

The Hardware/Software Interface CSE351 Spring 2013

Subprograms: Arguments

Assembly Language: Function Calls" Goals of this Lecture"

16.317: Microprocessor Systems Design I Fall 2015

CSE 401 Midterm Exam Sample Solution 2/11/15

CS241 Computer Organization Spring Addresses & Pointers

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Assembly Language: Function Calls" Goals of this Lecture"

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

CSE 351 Midterm - Winter 2015

CPS104 Recitation: Assembly Programming

The x86 Instruction Set

Compiler Construction D7011E

15-213/18-243, Spring 2011 Exam 1

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

An Introduction to x86 ASM

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

15-213/18-243, Fall 2010 Exam 1 - Version A

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

Assembly Language: Function Calls

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

CS61 Section Solutions 3

Credits and Disclaimers

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

16.317: Microprocessor Systems Design I Fall 2013

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

PRESENTED BY: SANTOSH SANGUMANI & SHARAN NARANG

Digital Forensics Lecture 3 - Reverse Engineering

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

You may work with a partner on this quiz; both of you must submit your answers.

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

CSE 351 Midterm - Winter 2017

CS241 Computer Organization Spring 2015 IA

Syntax-Directed Translation. Lecture 14

SOEN228, Winter Revision 1.2 Date: October 25,

Lecture 10 Return-oriented programming. Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller

System calls and assembler

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

16.317: Microprocessor Systems Design I Spring 2015

W4118: PC Hardware and x86. Junfeng Yang

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE 351 Midterm - Winter 2015 Solutions

Machine-Level Programming II: Control and Arithmetic

last time Assembly part 2 / C part 1 condition codes reminder: quiz

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Machine-Level Programming II: Control Flow

Transcription:

1. (10 points) Write a regular ression or regular ressions that generate the following sets of strings. (a) (5 points) All strings containing a s, b s, and c s with at least one a and at least one b. [abc]*a[abc]*b[abc]* [abc]*b[abc]*a[abc]* (b) (5 points) All strings of 0 s and 1 s with at most one pair of consecutive 1 s. Here are two possible solutions (0 10)*1?1?(0 01)* 1 (0 10)*11(0 01)* 2. (8 points) Pascal defined real (floating-point) numeric constants as follows: ::= [0-9] s ::= + real ::= s. s s. s E scalefactor s E scalefactor scalefactor ::= s (+ - ) s Draw a DFA that accepts real constants as defined above. (You don t need to construct an NFA and use algorithms to convert it to a DFA just draw a suitable DFA diagram.) E E +, - Page 1 of 6

3. (12 points) (LR parsing) In languages like Scheme, arithmetic ressions are written as parenthesized lists beginning with an operator followed by the operands. Here is a simple grammar for ressions involving addition and subtraction and the single integer constant 1. ::= int ( op ) op ::= + - int ::= 1 (a) (10 points) Construct the LR(0) state machine for this grammar. ::=. int ::=. ( op ) int ::=. 1 ::= (. op ) op :: =. + op :: =. - ( int 1 + - ::= int. int ::= 1. op :: = +. op :: = -. op ( ( ::= ( op. ) ::=. int ::=. ( op ) int ::=. 1 int 1 ::= ( op. ) int ::=. int ::=. ( op ) 1 int ::=. 1 ) ::= ( op. ) ::= ( op ). (b) (2 points) Is this grammar LR(0)? Why or why not? Yes. There are no shift-reduce or reduce-reduce conflicts. Page 2 of 6

4. (6 points) Some erimental programming languages include an n+1/2 loop with the following syntax: repeat while ( ) end The semantics of this loop is that is executed then the conditional ression is evaluated. If is false, the loop terminates, otherwise, if it is true, is executed and the loop repeats starting with. Give the x86 code shape for this loop using a style similar to that used in lecture for other statements and conditional ressions. Here s a straightforward solution that leaves the parts of the loop in the same order they appear in the source code. loop: done: jmp false done jmp loop It s also possible to rearrange the order to get rid of the unconditional jump that s executed each iteration in the above code. jmp start continue: start: jmp true continue Page 3 of 6

5. (12 points) Suppose we want to add the following conditional statement to MiniJava: ifequal (1, 2) smaller larger statement3 The meaning of this is that is executed if the integer ressions 1 and 2 are equal; is executed if 1 < 2, and statement3 is executed if 1 > 2. (a) (5 points) Give context-free grammar production(s) for the ifequal statement that allows either or both of the smaller and larger parts of the statement to be omitted. If both the smaller and larger parts of the statement appear, they should appear in that order. Here are two solutions. The first one uses ε-productions stmt ::= ifequal (, ) stmt optsmaller optlarger optsmaller ::= smaller stmt ε optlarger ::= larger stmt ε The other one is more brute-force but doesn t include any ε-productions. stmt ::= ifequal (, ) stmt ifequal (, ) stmt smaller stmt ifequal (, ) stmt larger stmt ifequal (, ) stmt smaller stmt larger stmt (b) (5 points) Is the grammar with your production(s) from part (a) ambiguous? If not, argue informally why not; if it is ambiguous, give an example that shows that it is. Yes. This grammar has the same sort of problem as the dangling else in the usual grammar for conditional statements. There are two possible ways to derive, for example, ifequal (, ) ifequal (, ) stmt smaller stmt A derivation can be given where the smaller part is associated with the second ifequal, and another can be given that associates it with the first ifequal. (c) (2 points) When compiling this statement, what rule(s) or condition(s) should the type checker verify? We need to check that the two ressions both have type integer. Page 4 of 6

6. (5 points) Suppose we have two classes B and D, where D is a subclass (derived class) of B, and these classes contain the methods shown below. class B { void f() { void g() { void h() { class D extends B { void k() { void g() { Recall that our convention is that in generated x86 assembly code, the label for a method m in class C is C$m. Show what the generated virtual method dispatch tables for classes B and D would look like in x86 assembly language. (Hint: this is supposed to be an easy question don t over-analyze it.) B$$ dd 0 ; no superclass dd B$f dd B$g dd B$h D$$ dd B$$ dd B$f dd D$g dd B$h dd D$k A key point here is that the subclass method table must have pointers to the first three methods (f, g, and h) in the same order that they appear in the superclass table. Page 5 of 6

7. (12 points) Write an x86 assembly-language version of the following C function. Your answer doesn t need to use exactly the same code shape presented in class for the various statements and ressions (i.e., it only needs to be legal x86 code that works properly), but you do need to use the C function-calling conventions properly, i.e., push arguments onto the stack, set up a new stack frame, etc., and you must include assembly language code for all statements given here (i.e., don t omit the assignment to the local variable a, for example). int factorial(int n) { int a; a = n; if (a <= 1) { return 1; else { return a * factorial(a-1); factorial: push ebp ; function prologue mov ebp,esp sub esp,4 mov eax,[ebp+8] ; a = n; mov [ebp-4],eax cmp eax,1 ; a? 1 (a still in eax) jg else mov eax,1 ; a<=1 here, return 1 mov esp,ebp pop ebp ret ; (no jmp needed) else: sub eax,1 ; a-1 (a still in eax here) push eax ; factorial(a-1) call factorial add esp,4 ; pop argument imul eax,[ebp-4] ; factorial(a-1) * a in eax mov esp,ebp ; return pop ebp ret Page 6 of 6