How Functions Work

50 %
50 %
Information about How Functions Work
Technology

Published on April 29, 2011

Author: saumilshah

Source: slideshare.net

Description

A short introduction on how functions work. Functions are the building blocks of any modern programming language. This tutorial shows you how functions are implemented and how the process stack plays an important role in supporting functions.

How Functions Work Saumil Shah Net-Square www.net-square.com

Introduction

# who am i Saumil Shah CEO Net-square. Hacker, Speaker, Trainer, Author. M.S. Computer Science Purdue University. Google: "saumil" LinkedIn: saumilshah

Preview

What is a function? A function is a special SUBROUTINE

What is a function? A function is a special SUBROUTINE Re-usable block of code Can be called from anywhere in the program

What is a function? A function is a special SUBROUTINE Re-usable block of code Can be called from anywhere in the program Program control jumps to the subroutine... ...and returns to the next statement after completing the subroutine

Anything else? A function accepts parameters A function returns a value

Anything else? A function accepts parameters A function returns a value It may also have LOCAL variables...

Anything else? A function accepts parameters A function returns a value It may also have LOCAL variables... ...created when function is invoked, and destroyed when the function returns. Scope limited to that function only.

An example - add(x, y) Parameters int add(int x, int y) { int sum; sum = x + y; return(sum); } Local Variable Return Value

Where are all the values stored? How are parameters passed? Where are local variables stored?

Where are all the values stored? How are parameters passed? Where are local variables stored? It is all accomplished using the STACK!

Where are all the values stored? How are parameters passed? Where are local variables stored? It is all accomplished using the STACK! Parameters are pushed on the stack before calling the function. Local variables are stored in stack memory as well.

Calling a function

add(x, y) 1 PROLOGUE 2 Local Variables BODY 3 s = add(3, 4) EPILOGUE Return Calling a function 4

add(x, y) PROLOGUE Push 4 Local Variables Push 3 BODY CALL add EPILOGUE RET Calling a function

add CALL add RET Calling a function

CALL does two things: add Push EIP on the stack Jump to the function's address CALL add RET Calling a function

add CALL add RET Calling a function CALL does two things: Push EIP on the stack Jump to the function's address RET simply pops the saved EIP value.

How does it all fit together? Let's see what happens on the stack.

How does it all fit together? Let's see what happens on the stack. ESP is the stack pointer. It always points to the top of the stack.

In the beginning ESP points to the top of the stack, as usual ... ESP ... EBP

In the beginning ESP points to the top of the stack, as usual EBP is the frame pointer (called Base Pointer). It points to regions within the stack. ... ESP ... EBP

Push the parameters For add(3,4) we push 3 and 4 on the stack. 3 ESP 4 ... ... EBP

CALL add CALL pushes the current EIP on the stack... ...and jumps to add() Saved EIP ESP 3 4 ... ... EBP

Prologue The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack. Old EBP EBP ESP Saved EIP 3 4 ... ...

Prologue The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack. Old EBP EBP ESP What's a FRAME? Saved EIP 3 4 ... ...

Prologue The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack. Old EBP EBP ESP What's a FRAME? Saved EIP 3 We shall discuss the frame a bit later. 4 ... ...

Local Variables Local variables are created in the stack memory. sum ESP Old EBP EBP Saved EIP 3 4 ... ...

Frame for add() The Stack Frame The stack memory used by a function is termed as its STACK FRAME sum ESP Old EBP EBP Saved EIP 3 4 ... ... Frame for main()

Functions and Frames Each function call results in a new frame being created on the stack. func1() frame for func1 ESP

Functions and Frames Each function call results in a new frame being created on the stack. func1() frame for func2 ESP func2() frame for func1

Functions and Frames Each function call results in a new frame being created on the stack. frame for func3 ESP func1() frame for func2 func2() frame for func1 func3()

frame for func2 frame for func1 Functions and Frames When a function returns, the frame is "unwound" or "collapsed". func1() ESP func2() func3()

Functions and Frames And as new functions get invoked, new frames get created. frame for func4 ESP func1() frame for func2 func2() frame for func1 func3() func4()

The Frame Pointer EBP is the frame pointer (base pointer). sum Old EBP EBP Saved EIP 3 4 ... ...

The Frame Pointer EBP is the frame pointer (base pointer). sum local var Old EBP EBP Local variables and Parameters are RELATIVE to the frame pointer. Saved EIP 3 param 1 4 param 2 ... ...

The Frame Pointer EBP is the frame pointer (base pointer). sum EBP - 4 Old EBP EBP Local variables and Parameters are RELATIVE to the frame pointer. Saved EIP 3 EBP + 8 4 EBP - n: Local vars EBP + n: Parameters EBP + 12 ... ...

Epilogue The Epilogue cleans up the stack frame. Local variables are effectively destroyed. sum Old EBP ESP EBP Saved EIP 3 4 ... ...

Epilogue The Epilogue cleans up the stack frame. Local variables are effectively destroyed. sum Old EBP POP EBP. Restores EBP back to the old frame. Saved EIP ESP 3 4 ... ... EBP

Epilogue The Epilogue cleans up the stack frame. Local variables are effectively destroyed. sum Old EBP POP EBP. Restores EBP back to the old frame. Saved EIP ESP 3 4 Stack pointer now points to where EIP was saved before CALL add(). ... ... EBP

Return! RET instruction pops the saved EIP value back into the EIP register. sum Old EBP Saved EIP ESP 3 4 ... ... EBP

Return! RET instruction pops the saved EIP value back into the EIP register. EIP sum Old EBP Program control is returns to the next statement after add() Saved EIP ESP 3 4 ... ... EBP

Return! RET instruction pops the saved EIP value back into the EIP register. EIP sum Old EBP Program control is returns to the next statement after add() Saved EIP 3 ESP 4 ESP shifts down by one word. ... ... EBP

Key Concepts

Review

END How Functions Work Saumil Shah Net-Square www.net-square.com

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Work function - Wikipedia, the free encyclopedia

The photoelectric work function is the minimum photon energy required to liberate an electron from a substance, in the photoelectric effect. If the photon ...
Read more

Function (mathematics) - Wikipedia, the free encyclopedia

The function composition of two functions takes the output of one function as the input of a second one. More specifically, ...
Read more

Work with MATLAB Functions in Microsoft Excel - MATLAB ...

Work with MATLAB Functions in Microsoft Excel; On this page; Differences Between Spreadsheet Link and Microsoft Excel Functions; Spreadsheet Link Function ...
Read more

How S-Functions Work - MATLAB & Simulink

Introduction. To create S-functions, you need to understand how S-functions work. Such knowledge requires an understanding of how the Simulink ® engine ...
Read more

SQL Functions - Oracle

SQL Functions . SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. Do not confuse SQL functions ...
Read more

PHP 5 Functions - W3Schools

PHP User Defined Functions. Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used ...
Read more

Functions - Free Math Help

Algebraic functions are a way to express a certain equation in term of input and output. For example, a simple function might return twice the input value ...
Read more

Algebra Lessons at Cool math .com - Functions

Algebra functions lessons with lots of worked examples and practice problems. Very easy to understand!Prealgebra exponent lessons, examples and practice ...
Read more

Functions - Khan Academy

Khan Academy is a nonprofit with the mission of providing a free, ... Functions are mathematical entities that assign unique outputs to given inputs.
Read more

The Wolfram Functions Site

The world's largest collection of formulas and graphics about more than 300,000 mathematical functions for the mathematics and science communities.
Read more