How Functions Work

21,938 views

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 an important role in supporting functions.

Published in: Technology, Business
4 Comments
44 Likes
Statistics
Notes
No Downloads
Views
Total views
21,938
On SlideShare
0
From Embeds
0
Number of Embeds
6,836
Actions
Shares
0
Downloads
1
Comments
4
Likes
44
Embeds 0
No embeds

No notes for slide

How Functions Work

  1. 1. How Functions Work<br />Saumil Shah<br />Net-Square<br />www.net-square.com<br />
  2. 2. Introduction<br />
  3. 3. # who am i<br />Saumil Shah<br />CEO Net-square.<br />Hacker, Speaker, Trainer, Author.<br />M.S. Computer Science<br />Purdue University.<br />Google: "saumil"<br />LinkedIn: saumilshah<br />
  4. 4. Preview<br />
  5. 5. What is a function?<br />A function is a special SUBROUTINE<br />
  6. 6. What is a function?<br />A function is a special SUBROUTINE<br />Re-usable block of code<br />Can be called from anywhere in the program<br />
  7. 7. What is a function?<br />A function is a special SUBROUTINE<br />Re-usable block of code<br />Can be called from anywhere in the program<br />Program control jumps to the subroutine...<br />...and returns to the next statement after completing the subroutine<br />
  8. 8. Anything else?<br />A function accepts parameters<br />A function returns a value<br />
  9. 9. Anything else?<br />A function accepts parameters<br />A function returns a value<br />It may also have LOCAL variables...<br />
  10. 10. Anything else?<br />A function accepts parameters<br />A function returns a value<br />It may also have LOCAL variables...<br />...created when function is invoked,<br />and destroyed when the function returns.<br />Scope limited to that function only.<br />
  11. 11. An example - add(x, y)<br />Parameters<br />int add(int x, int y)<br />{<br /> int sum;<br /> sum = x + y;<br /> return(sum);<br />}<br />Local Variable<br />Return Value<br />
  12. 12. Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />
  13. 13. Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />It is all accomplished using the STACK!<br />
  14. 14. Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />It is all accomplished using the STACK!<br />Parameters are pushed on the stack before calling the function.<br />Local variables are stored in stack memory as well.<br />
  15. 15. Calling a function<br />
  16. 16. add(x, y)<br />1<br />PROLOGUE<br />2<br />Local Variables<br />BODY<br />3<br />s = add(3, 4)<br />EPILOGUE<br />Return<br />Calling a function<br />4<br />
  17. 17. add(x, y)<br />PROLOGUE<br />Push 4<br />Local Variables<br />Push 3<br />BODY<br />CALL add<br />EPILOGUE<br />RET<br />Calling a function<br />
  18. 18. add<br />CALL add<br />RET<br />Calling a function<br />
  19. 19. CALL does two things:<br />add<br />Push EIP on the stack<br />Jump to the function's address<br />CALL add<br />RET<br />Calling a function<br />
  20. 20. add<br />CALL add<br />RET<br />Calling a function<br />CALL does two things:<br />Push EIP on the stack<br />Jump to the function's address<br />RET simply pops the saved EIP value.<br />
  21. 21. How does it all fit together?<br />Let's see what happens on the stack.<br />
  22. 22. How does it all fit together?<br />Let's see what happens on the stack.<br />ESP is the stack pointer.<br />It always points to the top of the stack.<br />
  23. 23. In the beginning<br />ESP points to the top of the stack, as usual<br />...<br />ESP<br />...<br />EBP<br />
  24. 24. In the beginning<br />ESP points to the top of the stack, as usual<br />EBP is the frame pointer (called Base Pointer). It points to regions within the stack.<br />...<br />ESP<br />...<br />EBP<br />
  25. 25. Push the parameters<br />For add(3,4) we push 3 and 4 on the stack.<br />3<br />ESP<br />4<br />...<br />...<br />EBP<br />
  26. 26. CALL add<br />CALL pushes the current EIP on the stack...<br />...and jumps to add()<br />Saved EIP<br />ESP<br />3<br />4<br />...<br />...<br />EBP<br />
  27. 27. Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<br />Saved EIP<br />3<br />4<br />...<br />...<br />
  28. 28. Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<br />What's a FRAME?<br />Saved EIP<br />3<br />4<br />...<br />...<br />
  29. 29. Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<br />What's a FRAME?<br />Saved EIP<br />3<br />We shall discuss the frame a bit later.<br />4<br />...<br />...<br />
  30. 30. Local Variables<br />Local variables are created in the stack memory.<br />sum<br />ESP<br />Old EBP<br />EBP<br />Saved EIP<br />3<br />4<br />...<br />...<br />
  31. 31. Frame for add()<br />The Stack Frame<br />The stack memory used by a function is termed as its STACK FRAME<br />sum<br />ESP<br />Old EBP<br />EBP<br />Saved EIP<br />3<br />4<br />...<br />...<br />Frame for main()<br />
  32. 32. Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />func1()<br />frame for func1 <br />ESP<br />
  33. 33. Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />func1()<br />frame for func2 <br />ESP<br />func2()<br />frame for func1 <br />
  34. 34. Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />frame for func3 <br />ESP<br />func1()<br />frame for func2 <br />func2()<br />frame for func1 <br />func3()<br />
  35. 35. frame for func2 <br />frame for func1 <br />Functions and Frames<br />When a function returns, the frame is "unwound" or "collapsed".<br />func1()<br />ESP<br />func2()<br />func3()<br />
  36. 36. Functions and Frames<br />And as new functions get invoked, new frames get created.<br />frame for func4 <br />ESP<br />func1()<br />frame for func2 <br />func2()<br />frame for func1 <br />func3()<br />func4()<br />
  37. 37. The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />Old EBP<br />EBP<br />Saved EIP<br />3<br />4<br />...<br />...<br />
  38. 38. The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />local var<br />Old EBP<br />EBP<br />Local variables and Parameters are RELATIVE to the frame pointer.<br />Saved EIP<br />3<br />param 1<br />4<br />param 2<br />...<br />...<br />
  39. 39. The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />EBP - 4<br />Old EBP<br />EBP<br />Local variables and Parameters are RELATIVE to the frame pointer.<br />Saved EIP<br />3<br />EBP + 8<br />4<br />EBP - n: Local vars<br />EBP + n: Parameters<br />EBP + 12<br />...<br />...<br />
  40. 40. Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br />ESP<br />EBP<br />Saved EIP<br />3<br />4<br />...<br />...<br />
  41. 41. Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br />POP EBP. Restores EBP back to the old frame.<br />Saved EIP<br />ESP<br />3<br />4<br />...<br />...<br />EBP<br />
  42. 42. Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br />POP EBP. Restores EBP back to the old frame.<br />Saved EIP<br />ESP<br />3<br />4<br />Stack pointer now points to where EIP was saved before CALL add().<br />...<br />...<br />EBP<br />
  43. 43. Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />sum<br />Old EBP<br />Saved EIP<br />ESP<br />3<br />4<br />...<br />...<br />EBP<br />
  44. 44. Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />EIP<br />sum<br />Old EBP<br />Program control is returns to the next statement after add()<br />Saved EIP<br />ESP<br />3<br />4<br />...<br />...<br />EBP<br />
  45. 45. Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />EIP<br />sum<br />Old EBP<br />Program control is returns to the next statement after add()<br />Saved EIP<br />3<br />ESP<br />4<br />ESP shifts down by one word.<br />...<br />...<br />EBP<br />
  46. 46. Key Concepts<br />
  47. 47. Review<br />
  48. 48. END<br />How Functions Work<br />Saumil Shah<br />Net-Square<br />www.net-square.com<br />

×