Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Size: px
Start display at page:

Download "Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to"

Transcription

1 Stack Frames EE 357 Unit 8 Stack Frames Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Every call to a subroutine will create a data structure called a on the stack to store this information To access this data structure on the stack an address register pointer called is used in addition to the normal stack pointer () Stack Frames Accessing Values on the Stack Caller s code pushes parameters (arguments) Call to subroutine pushes Callee saves registers it will overwrite (including ) Callee allocates space for l variables Caller llee Ca ans = avg(param1, param2); Subroutines Stack Frame Callee callee s Main Routine s int avg(int a, int b) { Stack Frame int temp=1; // var s Caller Stack Frame Organization Stack pointer () is usually used to access only the top value on the stack To access parameters / arguments, we need to access values We could try to use a displacement from, but other by the callee may change the requiring Need a better solution Saved Regs. Saved (its s, To access parameters we could try to use some displacement [i.e. ]

2 Frame Pointer Frame Pointer and Subroutines Use a new pointer called Frame Pointer () to point to the base of the current routines frame is used as will during the course of subroutine execution Can use constant displacements from to access parameters or variables Key 1: doesn t change during subroutine execution Key 2: Number of parameters and variables is a known value Locals disp. from Arguments disp. from - disp. Saved Regs. Saved (its s, Problem is that each executing subroutine needs its The called subroutine must and setup its own as the first thing it does when it is called. The called subroutine must restore the before it returns and instructions are used to save/restore old and allocate space for s - disp. Callers (its s, LINK Instruction LINK Instruction Example LINK should be of a subroutine Pushes on top of stack and can also allocates space for that routines stack frame ( var s, saved reg s, etc.) LINK An, #imm An should be the, imm = # of bytes for s and other saved values Saves current An () onto the stack Sets to current (top of stack) Adds imm. to to allocate space on the stack for l variables imm. should be negative to allocate space old new old Before LINK old stack frame After LINK 1.) Before LINK 1 2.) old is 2 pushed onto stack 3.) is copied into so now points at old 3 4.) negative 4 immediate value old is added d to to allocate var. old space

3 UNLK Instruction UNLK should be used just before RTS Restores old by popping it off the stack as well as deallocating variable space on the stack. UNLK An Copies An (i.e. ) to so that now points at old. Pops old into An new old old Before UNLK After UNLK 1 old 3 UNLK Instruction Example 1.) Before UNLK 2.) is copied to so that now points below the s 3.) old on the stack is copied into restoring the callers value 4.) is incremented to now point at 2 old 4 LINK & UNLK LINK and UNLK are the single instruction equivalent to the following sequences: LINK An,#imm => MOVE.L An,-() MOVEA.L,An ADDA.L #imm, UNLK => MOVEA.L An, MOVEA.L ()+,An Building a Stack Frame Caller s code pushes parameters (arguments) Parameters pushed in reverse order (param. n first) Call to subroutine pushes First instruction of subroutine is LINK Saves old on the stack and allocates space for variables Last instruction of subroutine is UNLK Deallocates variables and restores old RTS pops Caller s code pops parameters int ans; ans = avg(1,5); int avg(int a, int b) { int temp = 1; return a + b >> temp;

4 &Summary Summary is used as always points at old Local variables are at lower addresses (above) where points [i.e. -4(), -8(), ] is directly below old [i.e. 4()] Parameters are below [i.e. 8(), 12(), 16() ] is always pointing at current top of stack (above ) old Stack Frame Example int ans; ANS:.space 4 MAIN: ans = avg(1,5); MOVE.L #5,-() MOVE.L #1,-() int avg(int a, int b) { BSR.W AVG int temp = 1; 0x052C ADDA.L #8, return a + b >> temp; 3558 MOVE.L D0,ANS 355c.org 0x AVG: LINK,#-4 MOVE.L #1, -4() 3568 MOVE.L 8(),D0 ADD.L 12(),D0 356c MOVE.L -4(),D LSR.L L D1,D0D0 UNLK RTS Assume for MAIN that points to 0x and points to 0x3570 Example 2 Subroutine to sum an array of integers int sumit(int dat[], int length) { int sum, i; sum = 0; for(i=0; i < length; i++) sum = sum + data[i]; return sum; Example 2.data DAT:.space 400.text MAIN: MOVE.L #100,-() MOVE.L #DATA,-() 0x50c BSR.W SUMDAT ADDA.L #8,.org 0x SUMIT: LINK,#-8 CLR.L -4() 355c MOVE.L -4(),D0 ; move sum to D MOVE.L 8(),A0 ; move pointer MOVE.L 12(),-8() ; init i=length MOVE.L -8(),D1 ; move i to D LOOP: ADD.L (A0)+,D0 SUBI.L #1,D1 356c MOVE.L D0,-4() UNLK BNE LOOP DONE: MOVE.L #100,-8() RTS Main s 357c Stack Frame Originally for MAIN, points to 0x and points to 0x

5 What It Means To You A compilers class (CS410) will go into more detail of how to organize the stack frame For now, make sure you realize and understand When you look at assembly output of compiled C code, all functions will Local variables (those declared inside a function) are allocated to the May not want to create that 2 MB array inside your function A register usually called the frame pointer will be often be used as a pointer to some in the frame, with values being accessed at a particular

Subroutines. we jump to a new location in the code

Subroutines. we jump to a new location in the code Subroutines EE 57 Unit 7 Subroutines Stacks Subroutines (or functions) are portions of code that we can call from anywhere in our code, execute that t subroutine, and then A subroutine to calculate the

More information

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines EE 352 Unit 6 Stack Frames Recursive Routines Arguments and Return Values MIPS convention is to use certain registers for this task $a0 - $a3 used to pass up to 4 arguments. If more arguments, use the

More information

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values 1 Arguments and Return Values 2 EE 109 Unit 16 Stack Frames MIPS convention is to use certain registers for this task used to pass up to 4 arguments. If more arguments, use the stack used for return value

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

Lab 2 Use Traps. Lab 2 Input and Output 2 nd Semester. Lab 2 English. Lab 2 Pseudocode

Lab 2 Use Traps. Lab 2 Input and Output 2 nd Semester. Lab 2 English. Lab 2 Pseudocode Lab 2 Input and Output Lab 2 Use Traps Write (i.e. design and implement) an assembly language program that will accept user input from the keyboard and echo this to the terminal screen. Input should terminate

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

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames 6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (Instruc. Pointer) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in

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

EE 109 Unit 15 Subroutines and Stacks

EE 109 Unit 15 Subroutines and Stacks 1 EE 109 Unit 15 Subroutines and Stacks 2 Program Counter and GPRs (especially $sp, $ra, and $fp) REVIEW OF RELEVANT CONCEPTS 3 Review of Program Counter PC is used to fetch an instruction PC contains

More information

Recursive Subroutine Calls Example

Recursive Subroutine Calls Example The purpose of this example is to examine how all parameters, local variables, return addresses, and frame pointers are stored on the stack when a main program calls a procedure "Process" as well as when

More information

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"

More information

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls Goals of this Lecture Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"

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

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference

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

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

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning

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

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

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

CSCE 5610: Computer Architecture

CSCE 5610: Computer Architecture HW #1 1.3, 1.5, 1.9, 1.12 Due: Sept 12, 2018 Review: Execution time of a program Arithmetic Average, Weighted Arithmetic Average Geometric Mean Benchmarks, kernels and synthetic benchmarks Computing CPI

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

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

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

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack Part 7 Stacks The Stack Piles of Data Stack Stack A stack is an abstract data structure that stores objects Based on the concept of a stack of items like a stack of dishes Data can only be added to or

More information

stack frame where register An is used as the argument pointer.

stack frame where register An is used as the argument pointer. STACK FRAMES The MC68000 provides two special instructions to allocate and deallocate a data structure called a frame in the stack to make subroutines easier to code. general structure of a frame: SP local

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

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be tricky) when you translate high-level code into assembler code. MIPS Programming This is your crash course in assembler programming; you will teach yourself how to program in assembler for the MIPS processor. You will learn how to use the instruction set summary to

More information

EE 3170 Microcontroller Applications

EE 3170 Microcontroller Applications EE 3170 Microcontroller Applications Lecture 12: Advanced Assembly Language Programming Part II- Stacks Calling Conventions & Base Pointer Usage & Subroutine Examples - Miller 5.5-5.7 Based on slides for

More information

Compiling Code, Procedures and Stacks

Compiling Code, Procedures and Stacks Compiling Code, Procedures and Stacks L03-1 RISC-V Recap Computational Instructions executed by ALU Register-Register: op dest, src1, src2 Register-Immediate: op dest, src1, const Control flow instructions

More information

SRC Assembly Language Programming - III

SRC Assembly Language Programming - III Computer Architecture Laboratory SRC Assembly Language Programming - III Goals: a) Learn assembly language subroutine methodology b) Write a subroutine in SRC assembler 1. As a group we will develop a

More information

Topic 3-a. Calling Convention 2/29/2008 1

Topic 3-a. Calling Convention 2/29/2008 1 Topic 3-a Calling Convention 2/29/2008 1 Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 2/29/2008

More information

Procedure Call. Procedure Call CS 217. Involves following actions

Procedure Call. Procedure Call CS 217. Involves following actions Procedure Call CS 217 Procedure Call Involves following actions pass arguments save a return address transfer control to callee transfer control back to caller return results int add(int a, int b) { return

More information

Storage in Programs. largest. address. address

Storage in Programs. largest. address. address Storage in rograms Almost all operand storage used by programs is provided by memory. Even though registers are more efficiently accessed by instructions, there are too few registers to hold the stored

More information

Run-Time Data Structures

Run-Time Data Structures Run-Time Data Structures Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers, it is used for:

More information

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program Fall 2006 CS333: Computer Architecture University of Virginia Computer Science Michele Co Announcements Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly Homework #2 Due next Wednesday, Sept.

More information

X86 Stack Calling Function POV

X86 Stack Calling Function POV X86 Stack Calling Function POV Computer Systems Section 3.7 Stack Frame Reg Value ebp xffff FFF0 esp xffff FFE0 eax x0000 000E Memory Address Value xffff FFF8 xffff FFF4 x0000 0004 xffff FFF4 x0000 0003

More information

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure Procedures, the basic idea CSCI206 - Computer Organization & Programming Introduction to Procedures zybook: 81 (for next class) MIPS Procedure convention 1 Prepare parameters in $a0 through $a3 2 Return

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Stack Frames Revisited Recall Why ISAs Define Calling Conventions A compiler

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

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

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

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

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

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

Subroutine. Chapter 8

Subroutine. Chapter 8 Chapter 8 Subroutine Expected Outcomes Describe and apply the stack for data storage Describe the process of subroutine in any programs Develop a subroutine and code Interpret subroutine process in the

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

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

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

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Calling Conventions See P&H 2.8 and 2.12 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

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

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

MIPS Procedure Calls - Review

MIPS Procedure Calls - Review MIPS Stacks and Subroutine Calls Cptr280 Dr Curtis Nelson MIPS Procedure Calls - Review When making a procedure or function call, it is necessary to: Place parameters you wish to pass where they can be

More information

Subroutines. passing data

Subroutines. passing data Subroutines passing data Mechanisms: pass by value pass by result pass by value result/ value return/ copy restore pass by reference pass by name pass by lazy evaluation Techniques: 1. in registers 2.

More information

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12, and A.5-6 Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University Upcoming agenda PA1 due yesterday PA2 available and discussed during lab section this week

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

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

Stacks and Function Calls

Stacks and Function Calls Stacks and Function Calls Embedded Systems 3-1 Remember the Memory Map for Our MCU Embedded Systems 3-2 Classifying Data Variables Automatic declared within a function Only exist while the function executes

More information

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Hakim Weatherspoon CS 3410 Computer Science Cornell University Hakim Weatherspoon 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. compute jump/branch

More information

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

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions CS220 Logic Design Outline Calling Conventions Multi-module Programs 1 Calling and Returning We have already seen how the call instruction is used to execute a subprogram. call pushes the address of the

More information

Procedure Call and Return Procedure call

Procedure Call and Return Procedure call Procedures int len(char *s) { for (int l=0; *s!= \0 ; s++) l++; main return l; } void reverse(char *s, char *r) { char *p, *t; int l = len(s); reverse(s,r) N/A *(r+l) = \0 ; reverse l--; for (p=s+l t=r;

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015. CSC 3210 Computer Organization and Programming Georgia State University March 31, 2015 This lecture Plan for the lecture: Recap: Register Saving Subroutine Linkage call instruction jmpl instruction ret

More information

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

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call Call without Parameter Value Transfer What are involved? ESP Stack Pointer Register Grows by 4 for EIP (return address) storage Stack -- Memory which holds register contents Will keep the EIP of the next

More information

Implementing Subprograms

Implementing Subprograms 1 Implementing Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University CS315 Programming Languages Pinar Duygulu The General Semantics of Calls and Returns 2 The subprogram call and return

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

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

CS61 Section Solutions 3

CS61 Section Solutions 3 CS61 Section Solutions 3 (Week of 10/1-10/5) 1. Assembly Operand Specifiers 2. Condition Codes 3. Jumps 4. Control Flow Loops 5. Procedure Calls 1. Assembly Operand Specifiers Q1 Operand Value %eax 0x104

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

Lecture 9 Subroutines

Lecture 9 Subroutines CPE 390: Microprocessor Systems Spring 2018 Lecture 9 Subroutines Bryan Ackland Department of Electrical and Computer Engineering Stevens Institute of Technology Hoboken, NJ 07030 Adapted from HCS12/9S12

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

CS 2210 Programming Project (Part IV)

CS 2210 Programming Project (Part IV) CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation

More information

Wednesday, October 15, 14. Functions

Wednesday, October 15, 14. Functions Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:

More information

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

Machine-level Programming (3)

Machine-level Programming (3) Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control

More information

6.004 Computation Structures Spring 2009

6.004 Computation Structures Spring 2009 MIT OpenCourseWare http://ocw.mit.edu 6.004 Computation Structures Spring 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. M A S S A C H U S E T T

More information

Review of Activation Frames. FP of caller Y X Return value A B C

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 Mechanisms in Procedures Passing Control

More information

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing

IA32 Stack The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, IA32 Stack Popping. IA32 Stack Pushing 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region

More information

C Calling Conventions

C Calling Conventions C Calling Conventions 1. parameters are passed on the run-time or system stack, SP (or A7) 2. parameters pushed on stack in right to left order of call A6 used as the stack frame pointer local variables

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2005 Overview Variable types Memory sections in C Parameter passing

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

Mark Redekopp, All rights reserved. EE 357 Unit 4b. CF Assembly Basics

Mark Redekopp, All rights reserved. EE 357 Unit 4b. CF Assembly Basics EE 357 Unit 4b CF Assembly Basics OPERATIONS AND INSTRUCTIONS Coldfire Instruction Classes Data Transfer Move data between processor & memory Move data between registers w/in processor Can specify.b,.w,.l

More information

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Implementing Threads Operating Systems In Depth II 1 Copyright 2018 Thomas W Doeppner All rights reserved The Unix Address Space stack dynamic bss data text Operating Systems In Depth II 2 Copyright 2018

More information

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones CprE 288 Introduction to Embedded Systems Course Review for Exam 3 Instructors: Dr. Phillip Jones 1 Announcements Exam 3: See course website for day/time. Exam 3 location: Our regular classroom Allowed

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

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI. CSCI 402: Computer Architectures Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI Recall Big endian, little endian Memory alignment Unsigned

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE 1 ARCHITECTURE MODEL The basic instruction set of a computer is comprised of sequences of REGISTER TRANSFERS. Example: Add A, B, C Register B # A

More information

Tutorial 1 Microcomputer Fundamentals

Tutorial 1 Microcomputer Fundamentals Tutorial 1 Microcomputer Fundamentals Question 1 What do these acronyms mean? (a) CPU? (b) ROM? (c) EPROM? (d) RWM? (e) RAM? (f) I/O? What role does the CPU play in a computer system? Why is ROM an essential

More information

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8 Objectives ICT106 Fundamentals of Computer Systems Topic 8 Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8 To understand how HLL procedures/functions are actually implemented

More information

Segmentation in Assembly Language Programming

Segmentation in Assembly Language Programming 1 2 Segmentation in General Segmentation in Assembly Language Programming UNIX programs have 3 segments Text segment Executable machine instructions Data segment Initialized data BSS segment (Block Started

More information