CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Similar documents
Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Definition of Regular Expression

Dr. D.M. Akbar Hussain

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Fig.25: the Role of LEX

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

Compilers Spring 2013 PRACTICE Midterm Exam

Lexical Analysis: Constructing a Scanner from Regular Expressions

CS 241 Week 4 Tutorial Solutions

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

CSCE 531, Spring 2017, Midterm Exam Answer Key

CS 430 Spring Mike Lam, Professor. Parsing

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

CSE 401 Midterm Exam 11/5/10 Sample Solution

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

ECE 468/573 Midterm 1 September 28, 2012

Reducing a DFA to a Minimal DFA

Topic 2: Lexing and Flexing

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Lexical analysis, scanners. Construction of a scanner

CS201 Discussion 10 DRAWTREE + TRIES

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

ASTs, Regex, Parsing, and Pretty Printing

CMPSC 470: Compiler Construction

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

COMP 423 lecture 11 Jan. 28, 2008

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

MIPS I/O and Interrupt

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

What are suffix trees?

Assignment 4. Due 09/18/17

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Theory of Computation CSE 105

Outline. Tiling, formally. Expression tile as rule. Statement tiles as rules. Function calls. CS 412 Introduction to Compilers

Lecture T1: Pattern Matching

TO REGULAR EXPRESSIONS

Stack Manipulation. Other Issues. How about larger constants? Frame Pointer. PowerPC. Alternative Architectures

Today s Lecture. Basics of Logic Design: Boolean Algebra, Logic Gates. Recursive Example. Review: The C / C++ code. Recursive Example (Continued)

Stack. A list whose end points are pointed by top and bottom

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

MTH 146 Conics Supplement

Midterm I Solutions CS164, Spring 2006

Intermediate Information Structures

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

10.5 Graphing Quadratic Functions

Scanner Termination. Multi Character Lookahead

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

From Dependencies to Evaluation Strategies

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

Midterm 2 Sample solution

CMSC 331 First Midterm Exam

Information Retrieval and Organisation

cisc1110 fall 2010 lecture VI.2 call by value function parameters another call by value example:

Basics of Logic Design Arithmetic Logic Unit (ALU)

Lecture T4: Pattern Matching

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Agilent Mass Hunter Software

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University

Suffix trees, suffix arrays, BWT

Algorithm Design (5) Text Search

Presentation Martin Randers

CS 321 Programming Languages and Compilers. Bottom Up Parsing

9.1 apply the distance and midpoint formulas

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

2014 Haskell January Test Regular Expressions and Finite Automata

Uninformed Search. Hal Daumé III. Computer Science University of Maryland CS 421: Introduction to Artificial Intelligence 31 Jan 2012

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

Qubit allocation for quantum circuit compilers

Dynamic Programming. Andreas Klappenecker. [partially based on slides by Prof. Welch] Monday, September 24, 2012

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

EXPONENTIAL & POWER GRAPHS

George Boole. IT 3123 Hardware and Software Concepts. Switching Algebra. Boolean Functions. Boolean Functions. Truth Tables

Principles of Programming Languages

Distributed Systems Principles and Paradigms

Compilation

Section 10.4 Hyperbolas

Creating Flexible Interfaces. Friday, 24 April 2015

Representation of Numbers. Number Representation. Representation of Numbers. 32-bit Unsigned Integers 3/24/2014. Fixed point Integer Representation

University of Waterloo Midterm Examination

CMPT 379 Compilers. Lexical Analysis

Pointwise convergence need not behave well with respect to standard properties such as continuity.

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

Transcription:

CS 241 Fll 2017 Midterm Review Solutions Octoer 24, 2017 Contents 1 Bits nd Bytes 1 2 MIPS Assemly Lnguge Progrmming 2 3 MIPS Assemler 6 4 Regulr Lnguges 7 5 Scnning 9 1 Bits nd Bytes 1. Give two s complement inry representtion (nd corresponding hexdeciml representtion) of the following deciml numers: () 127 0111 1111 (more leding zero its cceptle), ut not 111 1111 () 241 0 1111 0001 (more leding zero its cceptle), ut not 1111 0001 (c) 30 10 0010 (more leding one its cceptle) 2. Give the deciml nd inry representtions of the following two s complement numers: () 0x81 1000 0001, -127 () 0x7F 0111 1111, 127 3. Give the (unsigned) deciml nd inry representtions of 0xCC. 11001100, 204 4. Wht is the rnge (in deciml) of 32-it two s complement numer? 2 31... 2 31 1 1

2 MIPS Assemly Lnguge Progrmming 1. Write MIPS procedure clled StringMp tht interprets the vlue in $1 s the ddress of string (n rry of chrcters tht ends with -1), nd the vlue in $2 s the ddress of procedure which interprets the vlue in $1 s chrcter nd returns chrcter in $3 (we cll it the chrcter trnsformtion function). StringMp should pply the chrcter trnsformtion function to chrcter of the string nd plce the ddress of the trnsformed string in $3. You my ssume the chrcter trnsformtion function will restore ny registers it modifies. However, you my not ssume the cller of StringMp stores/restores ny registers. StringMp: ; Cllee sves registers sw $31, -4($30) sw $9, -8($30) sw $8, -12($30) sw $5, -16($30) sw $4, -20($30) lis $31.word 20 su $30, $30, $31 ; Constnts lis $5.word -1 ; $5 = EOF list $4.word 4 dd $9, $1, $0 ; $9 = current ddress dd $8, $1, $0 ; sve return vlue forech: lw $1, 0($9) eq $1, $5, end jlr $2 sw $3, 0($9) dd $9, $9, $4 eq $0, $0, forech end: dd $3, $8, $0 ; Restore registers lis $31.word 20 dd $30, $30, $31 lw $31, -4($30) lw $9, -8($30) lw $8, -12($30) lw $5, -16($30) lw $4, -20($30) jr $31 2. Write MIPS progrm tht interprets the vlue in $1 s the ddress of n rry of MIPS instructions 2

nd executes the progrm stored in the rry. You my ssume ech element contins exctly one instruction, nd the MIPS progrm stored in the rry is vlid (ending with jr $31 ) nd tkes no rguments. jr $1 3. Exercise. Write MIPS procedure clled gcd which interprets the vlues in $1 nd $2 s unsigned deciml integers nd computes their gretest common divisor using recursion, plcing the result in $3. Hint: https://en.wikipedi.org/wiki/gretest_common_divisor#using_euclid.27s_lgorithm ; Clculte gcd(,) ; $1 - ; $2 - ; $3 - gcd(,) ; $ ; Recll tht gcd(,) = gcd(, mod ) ; nd gcd(,0) = gcd: ; Sve ll registers used sw $1, -4 ($30) sw $2, -8 ($30) sw $4, -12 ($30) sw $31, -16($30) lis $31.word 16 su $30, $30, $31 ; Bse Cse ; if ==0 return ne $0, $2, recur dd $3, $0, $1 eq $0, $0, clen recur: ; Recurrence Cse ; $1 contins ; $2 contins mod ; $4 contins lel gcd divu $1, $2 dd $1, $2, $0 mfhi $2 lis $4.word gcd jlr $4 clen: ; Lod ll register used lis $31.word 16 dd $30, $30, $31 3

lw $31, -16($30) lw $4, -12($30) lw $2, -8 ($30) lw $1, -4 ($30) jr $31 4. Exercise. According to the MIPS ssemly lnguge reference sheet, if you lod word from ddress 0xffff0004, it will plce the next yte from stndrd input in the destintion register. Write progrm which tkes numer in $1 nd reds in tht mny elements from stndrd input into n rry, stores the ddress of the rry in $1 nd the numer of elements in $2, nd then jumps to symol clled strt. This is essentilly mips.rry except tht the rry elements re provided s chrcters rther thn s integers. ; Constnts lis $29.word 0xffff0004 lis $4.word 4 ; Move numer of elements to $2 dd $2, $1, $0 ; Allocte spce for rry on the stck mult $2, $4 mflo $3 su $30, $30, $3 dd $1, $30, $0 ; $5 points to current ddress dd $5, $30, $0 iloop: eq $3, $0, iendl ; Sve n item from stdin to the rry lw $6, 0($29) sw $6, 0($5) ; Increse rry* nd decrese counter dd $5, $5, $4 su $3, $3, $4 eq $0, $0, iloop ; We re done here: sve $31 nd cll strt iendl: lis $5.word strt sw $31, -4($30) su $30, $30, $4 jlr $5 ; Restore $31 nd exit upon returning dd $30, $30, $4 lw $31, -4($30) 4

jr $31 ; Test code follows ; Prints out the contents of the rry to stdout s chrs ; Does not preserve registers (irresponsile!) strt: lis $4.word 4 lis $11.word 1 lis $28.word 0xffff000c sloop: eq $2, $0, sendl ; Move from rry to stdout lw $5, 0($1) sw $5, 0($28) dd $1, $1, $4 su $2, $2, $11 eq $0, $0, sloop sendl: jr $31 5. Exercise. Given the following hexdump of n ssemled MIPS ssemly lnguge progrm, determine the originl progrm without using.word. 00000000: 0000 1814... 00000004: 0000 0001... 00000008: 0000 2014... 0000000c: 0000 0001... 00000010: 0062 0018... 00000014: 0000 1812... 00000018: 0024 0822.$." 0000001c: 1420 fffc... 00000020: 03e0 0008... Your progrm must use lels where possile. Wht does the progrm do? lis $3.word 1 lis $4.word 1 lel1: mult $3, $3 mflo $3 su $1, $1, $4 ne $1, $0, lel1 jr $31 Given in $2 nd in $1, this progrm clcultes nd stores the result in $3. 5

3 MIPS Assemler 1. Give the symol tle for following MIPS ssemly lnguge progrm: mult: mult $1, $2 eq $0, $0, rnchloction frog: gorf:.word gorf rnchloction: ; rnch here ; now return jr $31 end: ; end of the progrm Symol tle: mult 0 frog 8 gorf 8 rnchloction 12 end 16 2. Wht is the purpose of the first nd second pss in n ssemler? Why does ssemlers require two psses? First pss: crete symol tle (with some error-checking). Second pss: syntx & error checking + mchine code output Becuse lels cn e used efore they re defined 3. Ed tries to write his CS350 ssignment in MIPS tht requires to loop through 90,000 lines of code. Descrie potentil prolem tht Ed could encounter if he uses rnching instructions. How could Ed solve this prolem? Brnch instructions use 16-it two s complement integers, so they cn only encode rnches from 2 15 to 2 15 1 (-32768 to 32767). To encode jump of 90 000 instructions, we cn store the vlue of the trget ddress in register nd jump to tht dresss. lis $5.word jumptohere jr $5... jumptohere:... 4. Wht informtion cn we not get ck when we disssemle progrm? Any comments nd the nmes of lels will e lost when the progrm is ssemled. Although we cn retrieve the exct instructions used, it cn e difficult to determine the context of those instructions. 6

4 Regulr Lnguges Note tht for simplicity, you my chose to llow leding zeroes. 1. Prove tht the lnguge L = {A, B, C, D} is regulr. It suffices to provide the regulr expression (A B C D) to show tht L is regulr. 2. Give DFA for ech of the following lnguges: No solutions will e provided for this question () The lnguge L over Σ = Γ {+}, where Γ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, such tht: L = {u + v : u, v Γ, u 0 (mod2) nd v 1 (mod2)} () Exercise. The lnguge L over Σ = {0, 1} which contins strings with inry vlue tht is congruent to 3 modulo 8. (Tht is, the inry vlue of string x divided y 8 gives reminder of 3). Hint: Think out the 3 lest-significnt (rightmost) its. 3. Give regulr expression for ech of the following lnguges: () The lnguge L over Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} of positive deciml numers divisile y 4. (0 4 8) (Σ ( (0 2 4 6 8)(0 4 8) ) ) (1 3 5 7 9)(2 6) () Exercise. The lnguge L over Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,,, c, d, e, f,.word, x, } of vlid MIPS.word directives with positive deciml nd ll hexdeciml rguments. (Use to represent spce).word ((0 1 2 3 4 5 6 7 8 9) ( 0x(0 1 2 3 4 5 6 7 8 9 c d e f) )) 4. You re given regulr lnguge L nd DFA M = {Σ, Q, q 0, A, δ} for the lnguge. In pseudocode, write function tht tkes string s Σ s n rgument nd returns 1 if the string is in lnguge L, 0 otherwise. ssuming s hs length n stte <- q 0 for i from 1 to n stte <- δ( stte, s i ) return ( stte A) 5. Give n NFA for ech of the following lnguges: No solutions will e provided for this question () The lnguge A over Σ = {0, 1} of strings ends with either 100 or 1. () The lnguge B over Σ = {0, 1} tht is the complement of A. (For ny lnguge A Σ, the complement of A is Σ \ A) (c) The lnguge over Σ = {c,,r,t} where words in the lnguge must end with cr or hve ct s sustring. (d) The lnguge over Σ = {c,,r,t} where words must end with tr nd hve rt s sustring. (e) Exercise. The lnguge over Σ = {,,c,d} where words must either include t lest one nd no s, or must include t lest one followed y t lest one c. 6. For the 2 NFAs from the previous question, give n ɛ-nfa for the lnguge B A. Descrie the lnguge ccepted y this utomton. 7

7. Convert the following NFA into DFA., strt 0 1 2 3 DFA: strt 0 0,2 0,2,3 0,1 0,1,3 8

5 Scnning MINUS DECR 0-9 0-9 strt START 1-9 INT -f,0-9 0 e x -f,0-9 ZERO ZEROX HEXINT -d,f-z E -z,0-9 n -z,0-9, -m,o-z,0-9 ID d -z,0-9, EN END -c,e-z,0-9?? 1. Using the ove DFA, produce tokeniztion of the following strings using the Simplified Mximl Munch lgorithm. If the scnning produces n error, output ll the tokens recognized up until the error nd then the string ERROR. () 0xc0x1-23--70xff HEXINT 0xc0, ID x1, INT -23, DECR --, INT 70, ID xff () 0xeef--0xkcd HEXINT 0xeef, DECR --, ERROR 9