Stacks and Function Calls

Similar documents
Run-time Environment

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

Implementing Subroutines. Outline [1]

Implementing Procedure Calls

Chapter 9 :: Subroutines and Control Abstraction

Run-time Environments

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

Run-time Environments

Course Administration

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

This training course provides additional details about the CPU architecture of the MCUs in the M16C/2x and M16C/6x series.

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

Functions in MIPS. Functions in MIPS 1

Today's Topics. CISC 458 Winter J.R. Cordy

Instruction Set Architectures (4)

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

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

CIT Week13 Lecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

Memory and Pointers written by Cathy Saxton

Lecture 7: Binding Time and Storage

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

Today. Putting it all together

Topic 7: Activation Records

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

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

This section provides some reminders and some terminology with which you might not be familiar.

Scope: Global and Local. Concept of Scope of Variable

Function Calls and Stack Allocation

SPIM Procedure Calls

Function Calls and Stack Allocation

Stack. Stack. Function Calls and Stack Allocation. Stack Popping Popping. Stack Pushing Pushing. Page 1

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

Programming Languages

CSE Lecture In Class Example Handout

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

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

System Software Assignment 1 Runtime Support for Procedures

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

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

CA Compiler Construction

Data Structure Layout. In HERA/Assembly

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

Compilers and computer architecture: A realistic compiler to MIPS

Wednesday, October 15, 14. Functions

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

Functions and Procedures

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Chapter 14 Functions

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.

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Subprograms, Subroutines, and Functions

ECE331: Hardware Organization and Design

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

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

real-time kernel documentation

Control Abstraction. Hwansoo Han

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

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

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

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

Code Generation. Lecture 19

Code Generation. Lecture 12

Run Time Environment

G53CMP: Lecture 14. Run-Time Organisation I. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 14 p.1/37

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

Computer Systems Lecture 9

Wednesday, September 27, 2017

EE 361 University of Hawaii Fall

CS 222: Pointers and Manual Memory Management

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

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

University of Washington

Procedure Call. Procedure Call CS 217. Involves following actions

This Lecture. G53CMP: Lecture 14 Run-Time Organisation I. Example: Lifetime (1) Storage Areas

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Parsing Scheme (+ (* 2 3) 1) * 1

Midterm 2 Review Chapters 4-16 LC-3

Bonus Lecture: Fibonacci

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

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.

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

In Java we have the keyword null, which is the value of an uninitialized reference type

Implementing Functions at the Machine Level

CS213. Machine-Level Programming III: Procedures

CS 314 Principles of Programming Languages. Lecture 13

Segmentation in Assembly Language Programming

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

Run-time Environments - 2

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

Lecture 7: Procedures and Program Execution Preview

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

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

CS241 Computer Organization Spring 2015 IA

Transcription:

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 Are re-initialized (re-created, in fact) each time the function is called Static declared outside of all functions, always exist Can make an automatic variable retain its value between invocations by using the static keyword Embedded Systems 3-3

Section Names and Contents Block started by symbol Embedded Systems 3-4

Example of Sections j Embedded Systems 3-5

Today Activation Record Arguments and Automatics Return value Arrays Layout in memory Code for accessing elements Embedded Systems 4-6

Activation Record Each time a function is activated (run), space is needed to store data this is called an activation record arguments data passed to function local variables return value other bookkeeping information Calling a function B from function A involves 1. Possibly placing arguments in a mutually-agreed location 2. Transfering control from function A to the function B 3. Allocating space for B s local data 4. Executing the function B 5. Possibly placing return value in a mutually-agreed location 6. De-allocating space for B s 7. Returning control to the function A Embedded Systems 4-7

Activation Record / Stack Frame Frame base == dynamic link 5 bytes used for old frame base (0[FB], 1[FB]) return address (2[FB], 3[FB], 4[FB]) enter and exitd instructions used to modify, save and restore SP and FB return from subroutine 0x00000 0xFFFFF Embedded Systems 4-8

Example Program with Function Calls const int globald=6; int compute(int x, int y); int squared(int r); void main() { int a, b, c; a = 10; b = 16; c = compute(a,b); } int compute(int x, int y) { int z; z = squared(x); z = z + squared(y) + globald; return(z); } int squared(int r) { return (r*r); } // These are main s automatic variables, and will be // stored in main s frame Embedded Systems 4-9

Step 1 - Passing Arguments Two methods for passing an argument to a function -- through a register or on the stack registers are preferred due to speed Requirements for register passing Argument types are prototype declared At least one argument is of a type which can be assigned to a register Prototype declaration is complete Arguments passed by register are allocated space in the stack for temporary use Compiler keeps track of where arguments are located, so it knows which registers or memory the code should access to find the arguments Embedded Systems 4-10

Example of Step 1 void main() { int a, b, c; // These variable are local to main, and will be a = 10; // stored in main s frame b = 16; c = compute(a,b); } _main: enter #06H mov.w #000aH,-2[FB] ; a mov.w #0010H,-4[FB] ; b mov.w -4[FB],R2 ; b mov.w -2[FB],R1 ; a jsr $compute mov.w R0,-6[FB] ; c exitd Move b (arg 2) into R2 Move a (arg 1) into R1 Embedded Systems 4-11

Example of Step 2 void main() { int a, b, c; // These variable are local to main, and will be a = 10; // stored in main s frame b = 16; c = compute(a,b); } _main: enter #06H mov.w #000aH,-2[FB] ; a mov.w #0010H,-4[FB] ; b mov.w -4[FB],R2 ; b mov.w -2[FB],R1 ; a jsr $compute mov.w R0,-6[FB] ; c exitd Jump to subroutine 1. push address of next instruction (mov.w R0,-6[FB]) onto call stack 2. load PC with address $compute Embedded Systems 4-12

Call Stack before main executes jsr compute SP FB c b a main s automatic variables Embedded Systems 4-13

Call Stack arriving at compute SP 1. FB Return Adx. c b a 1. jsr pushes old PC onto stack main s automatic variables Embedded Systems 4-14

Step 3 Allocate space on stack Save dynamic link and allocate space for local processing Enter #n instruction Enter function and allocate n bytes Push FB onto stack (SP -= 2). SP points to data on top of stack. Copy SP to FB (make FB point to where SP currently points) Subtract n from SP automatically allocate n bytes of space on stack MALPM, pp. 81-82 Embedded Systems 4-15

Example of Steps 3 & 4 E1: ;## # FUNCTION compute ;## # FRAME AUTO (z) size 2, offset -6 ;## # FRAME AUTO (y) size 2, offset -2 ;## # FRAME AUTO (x) size 2, offset -4 ;## # REGISTER ARG (x) size 2, REGISTER R1 ;## # REGISTER ARG (y) size 2, REGISTER R2.glb $compute $compute: enter #06H mov.w R1,-4[FB] ; x x mov.w R2,-2[FB] ; y y mov.w -4[FB],R1 ; x jsr $squared mov.w R0,-6[FB] ; z mov.w -2[FB],R1 ; y jsr $squared add.w -6[FB],R0 ; z add.w _globald,r0 mov.w R0,-6[FB] ; z mov.w -6[FB],R0 ; z exitd Step 3. Save dynamic link, Allocate 6 bytes of space on stack for 3 local variables Step 4. Execute body of function Embedded Systems 4-16

Call Stack executing enter #6 in compute SP FB 2. 1. 2. z x y Old FB Return Adx. 2. enter #6 pushes old FB value onto stack, copies SP to FB, and allocates 6 more bytes (for automatic variables x, y, z) c b a main s automatic variables Embedded Systems 4-17

Step 5 Place Return Value in Proper Location Some functions return a value integer, pointer, structure, etc. If not a struct or union, pass back in register for speed If a struct or union, pass back on stack. Pointer to space on stack is provided (and space is allocated) when function is called Embedded Systems 4-18

Example of Step 5 E1: ;## # FUNCTION compute ;## # FRAME AUTO (z) size 2, offset -6 ;## # FRAME AUTO (y) size 2, offset -2 ;## # FRAME AUTO (x) size 2, offset -4 ;## # REGISTER ARG (x) size 2, REGISTER R1 ;## # REGISTER ARG (y) size 2, REGISTER R2.glb $compute $compute: enter #06H mov.w R1,-4[FB] ; x x mov.w R2,-2[FB] ; y y mov.w -4[FB],R1 ; x jsr $squared mov.w R0,-6[FB] ; z mov.w -2[FB],R1 ; y jsr $squared add.w -6[FB],R0 ; z add.w _globald,r0 mov.w R0,-6[FB] ; z mov.w -6[FB],R0 ; z exitd Step 5. Compute returns an int, so copy result to R0 Embedded Systems 4-19

Steps 6 and 7 Deallocate Space and Return Exitd Deallocate space on stack and exit function Copy FB to SP (make SP point to where FB currently points) Pop FB from Stack (SP += 2) Return from subroutine (pop return address from stack) step 7 MALPM, pp. 81-82 Embedded Systems 4-20

Call Stack as compute executes exitd SP FB 8. 9. 10. z x y Old FB Return Adx. 8. exitd copies FB to SP, deallocating space for x,y,z 9. exitd then pops FB 10. exitd pops the return address off the stack into the program counter compute s activation record 9. c b a main s activation record Embedded Systems 4-21

Call Stack as compute continues in main SP FB c b a main s activation record Embedded Systems 4-22

Activation Record Auto Vars and Arguments!"# $% &&%'(!"# $% &&%'(!"# $% &&%')!"# $% &&%'*!"# $% &&%'+!"# $%( &&%'!"# $%( &&%'(, $%+ &&%-, $% &&%., $%( &&%((, $% &&%(,/0", $%,/0",0$%. 0$%( 1%20$%- Embedded Systems 4-23

Putting It All Together Next we ll see how the stack grows and shrinks main calls compute compute calls squared squared ends, returning control to compute compute ends, returning control to main Embedded Systems 4-24

Main() Function ;## # FUNCTION main ;## # FRAME AUTO (c) size 2, offset -6 ;## # FRAME AUTO (b) size 2, offset -4 ;## # FRAME AUTO (a) size 2, offset -2.section program,align _main: enter #06H mov.w #000aH,-2[FB] ; a mov.w #0010H,-4[FB] ; b mov.w -4[FB],R2 ; b mov.w -2[FB],R1 ; a jsr $compute mov.w R0,-6[FB] ; c exitd! " #!"#$%&'( #) $ $! Embedded Systems 4-25

Compute() Function ;## # FUNCTION compute ;## # FRAME AUTO (z) size 2, offset -6 ;## # FRAME AUTO (y) size 2, offset -2 ;## # FRAME AUTO (x) size 2, offset -4 ;## # REGISTER ARG (x) size 2, REGISTER R1 ;## # REGISTER ARG (y) size 2, REGISTER R2.glb $compute $compute: enter #06H mov.w R1,-4[FB] ; x x mov.w R2,-2[FB] ; y y mov.w -4[FB],R1 ; x jsr $squared mov.w R0,-6[FB] ; z mov.w -2[FB],R1 ; y jsr $squared add.w -6[FB],R0 ; z add.w _globald,r0 mov.w R0,-6[FB] ; z mov.w -6[FB],R0 ; z exitd Embedded Systems 4-26

Squared() Function ;## # FUNCTION squared ;## # FRAME AUTO (r) size 2, offset -2 ;## # REGISTER ARG (r) size 2, REGISTER R1 ;## # ARG Size(0) Auto Size(2) Context Size(5).glb $squared $squared: enter #02H mov.w R1,-2[FB] ; r r mov.w -2[FB],R0 ; r mul.w -2[FB],R0 ; r exitd Embedded Systems 4-27

Call Stack before main executes jsr compute SP FB c b a main s automatic variables Embedded Systems 4-28

Call Stack arriving at compute SP 1. FB Return Adx. c b a 1. jsr pushes old PC onto stack main s automatic variables Embedded Systems 4-29

Call Stack executing enter #6 in compute SP FB 2. 1. 2. z x y Old FB Return Adx. 2. enter #6 pushes old FB value onto stack, copies SP to FB, and allocates 6 more bytes (for x, y, z) c b a main s automatic variables Embedded Systems 4-30

Call Stack as compute executes jsr squared Return Adx. 3. jsr pushes old PC onto stack SP FB 3. 2. 1. 2. z x y Old FB Return Adx. compute s activation record c b a main s activation record Embedded Systems 4-31

Call Stack as squared executes enter #2 r Old FB Return Adx. 4. enter #2 pushes old FB value onto stack, copies SP to FB and allocates 2 more bytes (for r) SP FB 3. 4. 2. 1. 4. 2. z x y Old FB Return Adx. compute s activation record c b a main s activation record Embedded Systems 4-32

Call Stack as squared executes exitd SP FB 5. 6. 7. 6. r Old FB Return Adx. z x y Old FB Return Adx. 5. exitd copies FB to SP, deallocating space for r 6. exitd then pops FB 7. exitd pops the return address off the stack into the program counter compute s activation record c b a main s activation record Embedded Systems 4-33

Call Stack as compute executes exitd SP FB 8. 9. 10. z x y Old FB Return Adx. 8. exitd copies FB to SP, deallocating space for x,y,z 9. exitd then pops FB 10. exitd pops the return address off the stack into the program counter compute s activation record 9. c b a main s activation record Embedded Systems 4-34

Call Stack as compute continues in main SP FB c b a main s activation record Embedded Systems 4-35

Notes for Actual Implementation by Compiler Desired Format: offset[fb] variable_or_item_name Order in which local variables are located in activation record is cryptic Instead assume items are added the following order (with decreasing address) One byte items Automatic variables, in order of declaration Arguments which have been passed by register, in order of declaration Two byte items etc. Why? For local temporary storage Automatic variables, in order of declaration Arguments which have been passed by register, in order of declaration Why? For local temporary storage Don t forget the old frame pointer and return address Embedded Systems 4-36

Example of Passing Mixed Arguments ;## # FUNCTION compute2 ;## # FRAME AUTO ( z) size 2, offset -2 ;## # FRAME AUTO ( x) size 2, offset -2 ;## # FRAME ARG ( f) size 4, offset 5 ;## # FRAME ARG ( y) size 2, offset 9 ;## # REGISTER ARG ( x) size 2, REGISTER R2 ;## # ARG Size(6) Auto Size(2) Context Size(5) $compute2: enter #02H 3%&&24 mov.w R2,-2[FB] 5 ; x x mov.w -2[FB],R1 ; x jsr $squared 6 mov.w R0,-2[FB] ; z mov.w 9[FB],R1 ; y Load argument y from stack jsr $squared add.w -2[FB],R0 ; z lde.w _globald,r1 add.w R1,R0 mov.w R0,-2[FB] ; z mov.w -2[FB],R0 ; z exitd Embedded Systems 4-37