CSCI565 Compiler Design

Size: px
Start display at page:

Download "CSCI565 Compiler Design"

Transcription

1 CSCI565 Compiler Design Spring 2011 Homework 4 Solution Due Date: April 6, 2011 in class Problem 1: Activation Records and Stack Layout [50 points] Consider the following C source program shown below. #include <stdio.h> #include <stdlib.h> int num; int values[4]; void procd(int d){ values[num] = d; num = num + 1; printf("%d\n",d); } int proca(int a){ procd(a); return 0; } int (int b){ if(b <= 0){ return -b; } else { procd(b); procc(b); } return 0; } int procc(int c){ return (c-1); } int main(int argc, char **argv){ num = 0; 34: proca(atoi(argv[1])); 35: (atoi(argv[2])); 36: printf("num: %d\n",num); 37: return 0; 38: } Questions: (a) [05 points] (b) [05 points] (c) [05 points] Show the call graph, i.e. caller-callee relationship for user defined procedures/functions. Show the call tree and its execution history, i.e., the arguments values and output produced by the program s execution when the value of argv[1] is 3 and argv[2] is 2. Discuss for this particular code if the Activation Records (ARs) for each of the functions proca through procd can be allocated statically or not. Explain why or why not. (d) [15 points] Draw the contents of the stack when the control reaches the line in the source code labeled as 20- i.e., just before the program executes the return statement in this line. For the purpose of indicating the return addresses include the designation as N+ for a call instruction on line N. For instance, then procedure invokes the procedure procd in line 22, the corresponding return address can be labeled as 22+ to indicate that the return address should be immediately after line 22. Indicate the contents of the global and local variables to each procedure as well as the links in the AR. Use the AR organization described in class indicating the location of each procedure s local variable in the corresponding AR. (e) [10 points] (f) [10 points] The specific calling sequence of proca invoking procd could be shortened by inlining a copy of procd in the body of procedure proca. The structure of the AR for procedure proca would have to be modified to account for any particular local variables of procd. Discuss the impact on size and access to local variables this transformation would have. For this particular code do you need to rely on the Access s on the AR to access non-local variables? Why or why not? 1 of 7

2 Solution: a) The call graph is a graph where the nodes represent the functions/procedures in the code and the directed edges denote the relationship caller-callee. For this code the call graph is as shown below. main atoi proca procd procc printf b) The call history is distinct from the call tree in the sense the various invocations are explicitly represented in the tree (no longer a graph) as shown on the left hand side below. The execution of the main function for the inputs 3 and 2 is as shown on the right hand side. atoi("3") main(3,"3","2") proca(3) procd(3) atoi("2") procd(2) (2) printf("num: %d\n",3) procc(2) Active Section of the Call Tree when control reaches line 20 prompt>./main num: 3 printf("%d\n",3) printf("%d\n",2) (1) procd(1) procc(1) printf("%d\n",1) (0) c) As can be seen in the call graph depicted in a) above there is a section of the call graph that has a cycle. This means that the procedures and procc are mutually recursive. The implication is that there can potentially be multiple active instances of each of these procedures at a given point in time during the execution. As such there need to be distinct AR one for each of these active invocations and as a consequence these AR need to be allocated on the stack and not statically. Unlike these two procedures, procedures proca and procd can have their AR allocated statically as at any point in time during the execution they only have a single invocation active. d) The organization of the stack with the ARs when the control reaches the execution point labeled 20 is as illustrated below. Values for arguments and global variables reflect the execution when the control reaches this point. Note that no AR includes storage for local variables as no procedure has declared local variables. Instead the ARs include storage for arguments and other fields as discussed in class. In this depiction the stack grows downwards with each procedure invocation. Also depicted a frame for the globally addressed data, in this case the num and values array variables. 2 of 7

3 global data num = 3 values : values[0] = 3 values[1] = 2 values[2] = 1 values[3] = 0 main Ret. Address: Ret. Address: 29+ Arg 1: argc = 3 Arg 1: b = 1 Arg 2: argv[1] = "3" argv[2] = "2" procc Ret. Address: 23+ Ret. Address: 35+ Arg 1: c = 1 Arg 1: b = 2 SP procc Ret. Address: 29+ Ret. Address: 23+ Arg 1: b = 0 Arg 1: c = 2 e) Procedure procd has a single input argument and only manipulates global variables. Its inlining in procedure proca would have no effect on the AR as its single argument is never written to and thus needs not to retain any value across the invocations of procd. The resulting code for procedure proca after the inlining of procd is as shown below. int proca(int a){ values[num] = a; num = num + 1; printf("%d\n",a); return 0; } f) As is the case of C, there is no reason to include the Access on the procedures AR. The C language does not support nested scopes at the procedure level (although it does support nested static scopes inside each procedure). The only local variables each procedure may access include the local variables as the global or file level variable for which there is a single copy active at any given point in time during the execution. There is never the need to access local variables of another procedure that is currently active. No Access is required and neither the use of the Display mechanism. 3 of 7

4 Problem 2: Register Allocation [50 points] Consider the following 3-address representation of a computation depicted below on the left-hand side. In this assembly code the access to a procedures arguments and local variables as well as the return value is done using the Activation Record Pointer () and using the offsets as indicated on the right-hand side of the figure. Global variables are accessed via a global data pointer (GP) as is the case when accessing a global array of integer values A[ ]. Finally, the return of the control to the calling context is accomplished by the return instruction on line 34. Source Line 34: L1: L3: L4: Assembly t1 = - 4 t1 = *t1 t2 = - 8 t2 = *t2 if (t1 < 0) goto L3 t3 = GP + 12 *t3 = 1 t4 = GP + 16 *t4 = 0 t6 = + 16 t7 = 0 *t6 = t7 t8 = + 16 if (t8 == 32) goto L3 t9 = GP + 20 t11 = t8 * 4 t12 = t9 + t11 t12 = *t12 if (t12 = t2) goto L2 t5 = + 16 t5 = *t5 t13 = + 4 *t13 = t5 t8 = + 16 t9 = t8 + 1 t8 = + 16 *t8 = t9 t10 = + 4 *t10 = -1 return Comment t1 = param a if (a < 0) return -1 global x = 1 global y = 0 i = 0 if (i < 31) return -1 t12 = A[i] if (A[i] == b) return i return value = i i = i + 1 return value = -1 AR offset arg : b -8 arg : a -4 return address return value Access local: i 16 Questions: (a) [10 points] (b) [15 points] Derive the set of basic blocks in this assembly code. Recall that a basic block is maximal a sequence of instructions with a single entry and exit points. For each basic block determine which variables, such as the temporary variables, are live when the control leaves the basic block (this information will later one be used for the register allocator) as well as the worst-case frequency of execution of each basic block when the procedure to which this code corresponds is executed. Present your results in a tabular form. Using the top-down register allocation algorithm described in class determine which variables should be assigned to registers under the assumption you only have 3 physical registers, r0, r1 and r2. Rewrite the code using these three physical registers and assuming that for temporary computations you can use other three scratch register r11, r12 and r13. (c) [25 points] Using the graph-coloring register allocation algorithm rewrite the 3-address code using 3 physical registers for the two definitions of instruction interference described in class. Present the interference graphs (or matrices) along with a possible color assignment. You do not have to show the intermediate coloring algorithm steps but show the live ranges of each variable. Again assume you have the additional three registers r11, r12 and r13 for temporary computations. 4 of 7

5 Solution: (a) The basic blocks are as shown below on the right-hand side. On the left-hand side we have a table of which variables are live out side each basic block. As can be seen the live ranges of these variables is extremely short. In fact only variables t2 and t8 have live ranges that span multiple basic blocks. ENTRY t1 = - 4 t1 = *t1 t2 = - 8 t2 = *t2 if (t1 < 0) goto L3 t3 = GP + 12 *t3 = 1 t4 = GP + 16 *t4 = 0 t6 = + 16 t7 = 0 *t6 = t7 BB1 BB2 L1: t8 = + 16 BB3 if (t8 == 32) goto L3 t9 = GP + 20 t11 = t8 * 4 t12 = t9 + t11 BB4 t12 = *t12 if (t12 = t2) goto L2 Live Out Variable t t t t t t t t t t t t t Live Ranges Instruction Ranges t1 [0-5] t2 [3-20] [26-31] t3 [6-7] t4 [8-9] t5 [21-24] t6 [10-12] t7 [11-12] t8 [13-17] [26-30] t9 [16-17] [28-30] t10 [32-33] t11 [17-18] t12 [18-20] t13 [23-24] t5 = + 16 t5 = *t5 t13 = + 4 *t13 = t5 BB5 L3: t10 = + 4 *t10 = -1 BB7 t8 = + 16 t9 = t8 + 1 t8 = + 16 *t8 = t9 BB6 34: L4: return BB8 EXIT (b) In the figure below on the right-hand side we have the frequency of execution of each block and the corresponding metric for each variable. Under the top-down allocation strategy we would place variables t8, t9 and t12 in registers leaving all the other using register r11, r12 and r13. Based on the metric developed for this top-down allocator we would select the temporary variables t8, t9 and t12 as being mapped to the registers r0, r1 and r2. The resulting code using the physical registers r0, r1 and r2 is as shown in the figure below. Notice that the modified code has no spill code, Instead, and fir the conditional statement on line 19 there is the need to reload the value of t2 the input parameter of the function a second time in register r12, hence the additional two lines of code with addresses labeled of 7

6 Source Line 00: 00: L1: Assembly r11 = - 4 r11 = *r11 r12 = - 8 r12 = *r12 if (r11 < 0) goto L3 r11 = GP + 12 *r11 = 1 r12 = GP + 16 *r12 = 0 r11 = + 16 r12 = 0 *r11 = r12 if (r0 == 32) goto L3 r1 = GP + 20 r11 = r0 * 4 r2 = r1 + r11 r2 = *r2 r12 = - 8 r12 = *r12 Comment t1 = param a if (a < 0) return -1 global x = 1 global y = 0 i = 0 if (i < 31) return -1 t12 = A[i] BB ID Frequency Variable t t t t t t t t t t t t t Variable Metric t1 4 t2 35 t3 2 t4 2 t5 4 t6 2 t7 2 t8 352 t9 128 t10 2 t11 64 t t : L3: L4: if (r2 = r12) goto L2 r11 = + 16 r11 = *r11 r12 = + 4 *r12 = r11 r1 = r0 + 1 *r0 = r1 r11 = + 4 *r11 = -1 return if (A[i] == b) return i return value = i i = i + 1 return value = -1 (c) On the left we have the interference table for the simpler notion of interference whereas on the right we have for the notion of interference taking into account the use of the value in each register when the RHS is evaluated. The live ranges on the right-hand side of the figure include the R superscript for a Read as the last operation or a W for the Write as the first operation on the instruction at that specific line. Live Ranges Instruction Ranges t1 [0-5] t2 [3-20] [26-31] t3 [6-7] t4 [8-9] t5 [21-24] t6 [10-12] t7 [11-12] t8 [13-17] [26-30] t9 [16-17] [28-30] t10 [32-33] t11 [17-18] t12 [18-20] t13 [23-24] t10 t11 t9 t12 t8 t1 t2 t7 t3 t4 t5 t13 t6 Live Ranges Instruction Ranges t1 [0-5] t2 [3-20] [26-31] t3 [6-7] t4 [8-9] t5 [21-24] t6 [10-12] t7 [11-12] t8 [13-17] [26-30] t9 [16-17] [28-30] t10 [32-33] R t11 [17-18 ] W t12 [18-20] t13 [23-24] t10 t11 t9 t12 t8 t1 t2 t7 t3 t4 t5 t13 t6 As can be seen there is a slight, but important, difference in terms of the interference graph. For the first definition of interference we have a clique of size 4 in the graph which means that we will not be able to color it with less than 4 colors. For the second definition we can easily color it with 3 colors and hence use 3 registers. For the first definition, and given that we only have 3 available registers we need to select one of these colors to spill. The coloring algorithm suggests dropping one of the higher degree nodes. In this case all the nodes in the 4-clique have the same degree. Looking at the live ranges t11 is the node that corresponds to the shortest range and we can choose that one not to assign any register to in reality using the alternative register to temporary load the corresponding value in register for the computations in lines 17 and of 7

7 The figure below depicts the resulting code with the assignment of registers for the second definition of interference and hence using the three registers r0, r1 and r2. Source Line Assembly Comment 34: L1: L3: L4: r0 = - 4 r1 = - 8 r1 = *r1 if (r0 < 0) goto L3 r0 = GP + 12 *r0 = 1 r0 = GP + 16 *r0 = 0 r2 = 0 *r0 = r2 if (r0 == 32) goto L3 r2 = GP + 20 r11 = r0 * 4 r0 = r2 + r11 if (r0 = r1) goto L2 r1 = + 4 *r1 = r0 r2= r0 + 1 *r0 = r2 r1 = + 4 *r1= -1 return t1 = param a if (a < 0) return -1 global x = 1 global y = 0 i = 0 if (i < 31) return -1 t12 = A[i] if (A[i] == b) return i return value = i i = i + 1 return value = -1 7 of 7

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz University of Southern California (USC) Computer Science Department Compiler Design Spring 2014 Run-Time Environments Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute

More information

CSCI Compiler Design

CSCI Compiler Design CSCI 565 - Compiler Design Spring 2010 Final Exam - Solution May 07, 2010 at 1.30 PM in Room RTH 115 Duration: 2h 30 min. Please label all pages you turn in with your name and student number. Name: Number:

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments - 2

Run-time Environments - 2 Run-time Environments - 2 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

More information

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering Supporting Nested Procedures James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Memory Layout

More information

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

Assignment 11: functions, calling conventions, and the stack

Assignment 11: functions, calling conventions, and the stack Assignment 11: functions, calling conventions, and the stack ECEN 4553 & 5013, CSCI 4555 & 5525 Prof. Jeremy G. Siek December 5, 2008 The goal of this week s assignment is to remove function definitions

More information

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.

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. 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.5 6 compute jump/branch targets memory PC +4 new pc Instruction Fetch

More information

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

CSC 8400: Computer Systems. Using the Stack for Function Calls CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

Compilers and Code Optimization EDOARDO FUSELLA

Compilers and Code Optimization EDOARDO FUSELLA Compilers and Code Optimization EDOARDO FUSELLA Contents Data memory layout Instruction selection Register allocation Data memory layout Memory Hierarchy Capacity vs access speed Main memory Classes of

More information

Scope: Global and Local. Concept of Scope of Variable

Scope: Global and Local. Concept of Scope of Variable Concept of Scope of Variable : Computer Architecture I Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs135/ In assembly, who has access to a memory location/variable?

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 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)

More information

Implementing Subroutines. Outline [1]

Implementing Subroutines. Outline [1] Implementing Subroutines In Text: Chapter 9 Outline [1] General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

Procedure and Object- Oriented Abstraction

Procedure and Object- Oriented Abstraction Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks

More information

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return Outline Implementing Subprograms In Text: Chapter 10 General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14 BIL 104E Introduction to Scientific and Engineering Computing Lecture 14 Because each C program starts at its main() function, information is usually passed to the main() function via command-line arguments.

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Lecture 5. Announcements: Today: Finish up functions in MIPS

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C Chapter 4 Functions Based on slides McGraw-Hill Additional material 4/5 Lewis/Martin Function Smaller, simpler, subcomponent of program Provides abstraction Hide low-level details Give high-level structure

More information

Course Administration

Course Administration Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

More information

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15 Stack Frames Geoffrey Brown Bryce Himebaugh Indiana University September 2, 2016 Geoffrey Brown, Bryce Himebaugh 2015 September 2, 2016 1 / 15 Outline Preserving Registers Saving and Restoring Registers

More information

Programming in C. Pointers and Arrays

Programming in C. Pointers and Arrays Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

More information

CSE 333 Lecture 7 - final C details

CSE 333 Lecture 7 - final C details CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington Today s topics: - a few final C details header guards and other preprocessor tricks

More information

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009 Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009 April 16, 2009 Instructions: Please write your answers on the printed exam. Do not turn in any extra pages. No interactive electronic devices

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 5: Functions. Scope. 1 Functions: Explicit declaration Declaration, definition, use, order matters. Declaration: defines the interface of a function; i.e., number and types

More information

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100 ECE 2035(B) Programming for Hardware/Software Systems Fall 2013 Exam Two October 22 nd 2013 Name: Q1: /20 Q2: /30 Q3: /24 Q4: /26 Total: /100 1/6 For functional call related questions, let s assume the

More information

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal Decimal Representation Binary Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write

More information

Decimal Representation

Decimal Representation Decimal Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write number as 4705 10

More information

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

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

CS 0449 Sample Midterm

CS 0449 Sample Midterm Name: CS 0449 Sample Midterm Multiple Choice 1.) Given char *a = Hello ; char *b = World;, which of the following would result in an error? A) strlen(a) B) strcpy(a, b) C) strcmp(a, b) D) strstr(a, b)

More information

Understanding Pointers

Understanding Pointers Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer

More information

Fundamentals of Programming Session 12

Fundamentals of Programming Session 12 Fundamentals of Programming Session 12 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CSCI 565: Compiler Design and Implementation Spring 2006

CSCI 565: Compiler Design and Implementation Spring 2006 CSCI 565: Compiler Design and Implementation Spring 2006 Midterm Exam Solution Feb. 28, 2006 Problem 1: Attributive Grammar and Syntax Directed Translation [30 points] A language such as C allows for user

More information

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V. Runtime management CS3300 - Compiler Design Runtime management V Krishna Nandivada IIT Madras Copyright c 2001 by Antony L Hosking Permission to make digital or hard copies of part or all of this work

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 1. Do a page check: you should have 8 pages including this cover sheet. 2. You have 50 minutes

More information

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read Function Calls Tom Kelliher, CS 240 Feb. 13, 2002 1 Administrivia Announcements Collect homework. Assignment Read 3.7 9. From Last Time SPIM lab. Outline 1. Function calls: stack execution model, memory

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 FUNCTIONS INTRODUCTION AND MAIN All the instructions of a C program are contained in functions. üc is a procedural language üeach function performs a certain task A special

More information

CSC258: Computer Organization. Functions and the Compiler Tool Chain

CSC258: Computer Organization. Functions and the Compiler Tool Chain CSC258: Computer Organization Functions and the Compiler Tool Chain 1 A Note about This Week s Quiz There were very few exercises this week, except for writing substantial pieces of code. Instead, I provided

More information

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compilation /15a Lecture 7. Activation Records Noam Rinetzky Compilation 0368-3133 2014/15a Lecture 7 Activation Records Noam Rinetzky 1 Code generation for procedure calls (+ a few words on the runtime system) 2 Code generation for procedure calls Compile time

More information

Programming Languages: Lecture 12

Programming Languages: Lecture 12 1 Programming Languages: Lecture 12 Chapter 10: Implementing Subprograms Jinwoo Kim jwkim@jjay.cuny.edu Chapter 10 Topics 2 The General Semantics of Calls and Returns Implementing Simple Subprograms Implementing

More information

CSE 230 Intermediate Programming in C and C++ Functions

CSE 230 Intermediate Programming in C and C++ Functions CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions

More information

Processes. Johan Montelius KTH

Processes. Johan Montelius KTH Processes Johan Montelius KTH 2017 1 / 47 A process What is a process?... a computation a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access Run Time Environment Activation Records Procedure Linkage Name Translation and Variable Access Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University

More information

A process. the stack

A process. the stack A process Processes Johan Montelius What is a process?... a computation KTH 2017 a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other processes

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Run-Time Environments

Run-Time Environments 1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 Procedure Activation and Lifetime A procedure is activated when called

More information

Computer Systems Lecture 9

Computer Systems Lecture 9 Computer Systems Lecture 9 CPU Registers in x86 CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions

More information

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function CMPT 127 Spring 2019 Grade: / 20 First name: Last name: Student Number: Lab Exam 1 D400 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return -1? Answer:

More information

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

Functions and Procedures

Functions and Procedures Functions and Procedures Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself

More information

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls? Memory Allocation in C Functions in C Global data pointer: R4 Global and static variables Specify positive offsets Frame pointer: Points to current code block Negative offset Stack Pointer: Top of stack

More information

Compilation 2014 Activation Records (Part 1)

Compilation 2014 Activation Records (Part 1) Compilation 2014 Activation Records (Part 1) Aslan Askarov aslan@cs.au.dk Revised from slides by E. Ernst (Abstract) computer organization Program memory code segment contains program text data segment

More information

CSC 2400: Computer Systems. Arrays and Strings in C

CSC 2400: Computer Systems. Arrays and Strings in C CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays: C vs.

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer Computer Architecture Chapter 2-2 Instructions: Language of the Computer 1 Procedures A major program structuring mechanism Calling & returning from a procedure requires a protocol. The protocol is a sequence

More information

CS 316: Procedure Calls/Pipelining

CS 316: Procedure Calls/Pipelining CS 316: Procedure Calls/Pipelining Kavita Bala Fall 2007 Computer Science Cornell University Announcements PA 3 IS out today Lectures on it this Fri and next Tue/Thu Due on the Friday after Fall break

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ. C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,

More information

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han s and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Tuesday Feb. 15 at 11 am - note extension

More information

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. Function Calls Tom Kelliher, CS 220 Oct. 24, 2011 1 Administrivia Announcements Assignment SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how. From Last Time Outline 1.

More information

Sample Midterm (Spring 2010)

Sample Midterm (Spring 2010) Sample Midterm (Spring 2010) Solutions are shown in this style. This exam was given in Spring 2010. 1. Executing Programs on IA32 (30 pts) The following questions relate to how programs are compiled for

More information

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

More information

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction Procedures as Control Abstractions CSc 520 Principles of Programming Languages 30: Procedures Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona

More information

Readings and References. Procedure Detail. Leaf procedures. Non-leaf procedure. Calling tree. Layout of stack frame (little leaf)

Readings and References. Procedure Detail. Leaf procedures. Non-leaf procedure. Calling tree. Layout of stack frame (little leaf) Readings and References Procedure Detail CSE 410 - Computer Systems October 10, 2001 Reading Other References D. Sweetman, See MIPS Run, Morgan Kauffman, Publishers Chapter 10, C Programming on MIPS 10-Oct-2001

More information

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/14/17 Overview (1A) Copyright (c) 2009-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! Gives

More information

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

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

Midterm II CS164, Spring 2006

Midterm II CS164, Spring 2006 Midterm II CS164, Spring 2006 April 11, 2006 Please read all instructions (including these) carefully. Write your name, login, SID, and circle the section time. There are 10 pages in this exam and 4 questions,

More information