Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# How Functions Work

14,427

Published on

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 …

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.

30 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Views
Total Views
14,427
On Slideshare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
1
3
Likes
30
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. How Functions Work
Saumil Shah
Net-Square
www.net-square.com
• 2. Introduction
• 3. # who am i
Saumil Shah
CEO Net-square.
Hacker, Speaker, Trainer, Author.
M.S. Computer Science
Purdue University.
• 4. Preview
• 5. What is a function?
A function is a special SUBROUTINE
• 6. What is a function?
A function is a special SUBROUTINE
Re-usable block of code
Can be called from anywhere in the program
• 7. 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
• 8. Anything else?
A function accepts parameters
A function returns a value
• 9. Anything else?
A function accepts parameters
A function returns a value
It may also have LOCAL variables...
• 10. 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.
• 11. An example - add(x, y)
Parameters
{
int sum;
sum = x + y;
return(sum);
}
Local Variable
Return Value
• 12. Where are all the values stored?
How are parameters passed?
Where are local variables stored?
• 13. Where are all the values stored?
How are parameters passed?
Where are local variables stored?
It is all accomplished using the STACK!
• 14. 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.
• 15. Calling a function
1
PROLOGUE
2
Local Variables
BODY
3
EPILOGUE
Return
Calling a function
4
PROLOGUE
Push 4
Local Variables
Push 3
BODY
EPILOGUE
RET
Calling a function
RET
Calling a function
• 19. CALL does two things:
Push EIP on the stack
RET
Calling a function
RET
Calling a function
CALL does two things:
Push EIP on the stack
RET simply pops the saved EIP value.
• 21. How does it all fit together?
Let's see what happens on the stack.
• 22. 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.
• 23. In the beginning
ESP points to the top of the stack, as usual
...
ESP
...
EBP
• 24. 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
• 25. Push the parameters
For add(3,4) we push 3 and 4 on the stack.
3
ESP
4
...
...
EBP
CALL pushes the current EIP on the stack...
Saved EIP
ESP
3
4
...
...
EBP
• 27. Prologue
The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.
Old EBP
EBP
ESP
Saved EIP
3
4
...
...
• 28. 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
...
...
• 29. 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
...
...
• 30. Local Variables
Local variables are created in the stack memory.
sum
ESP
Old EBP
EBP
Saved EIP
3
4
...
...
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()
• 32. Functions and Frames
Each function call results in a new frame being created on the stack.
func1()
frame for func1
ESP
• 33. 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
• 34. 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()
• 35. frame for func2
frame for func1
Functions and Frames
When a function returns, the frame is "unwound" or "collapsed".
func1()
ESP
func2()
func3()
• 36. 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()
• 37. The Frame Pointer
EBP is the frame pointer (base pointer).
sum
Old EBP
EBP
Saved EIP
3
4
...
...
• 38. 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
...
...
• 39. 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
...
...
• 40. Epilogue
The Epilogue cleans up the stack frame. Local variables are effectively destroyed.
sum
Old EBP
ESP
EBP
Saved EIP
3
4
...
...
• 41. 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
• 42. 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
• 43. Return!
RET instruction pops the saved EIP value back into the EIP register.
sum
Old EBP
Saved EIP
ESP
3
4
...
...
EBP
• 44. 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
• 45. 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
• 46. Key Concepts
• 47. Review
• 48. END
How Functions Work
Saumil Shah
Net-Square
www.net-square.com