Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
How Functions Work<br />Saumil Shah<br />Net-Square<br />www.net-square.com<br />
Introduction<br />
# who am i<br />Saumil Shah<br />CEO Net-square.<br />Hacker, Speaker, Trainer, Author.<br />M.S. Computer Science<br />Pu...
Preview<br />
What is a function?<br />A function is a special SUBROUTINE<br />
What is a function?<br />A function is a special SUBROUTINE<br />Re-usable block of code<br />Can be called from anywhere ...
What is a function?<br />A function is a special SUBROUTINE<br />Re-usable block of code<br />Can be called from anywhere ...
Anything else?<br />A function accepts parameters<br />A function returns a value<br />
Anything else?<br />A function accepts parameters<br />A function returns a value<br />It may also have LOCAL variables......
Anything else?<br />A function accepts parameters<br />A function returns a value<br />It may also have LOCAL variables......
An example - add(x, y)<br />Parameters<br />int add(int x, int y)<br />{<br />      int sum;<br />      sum = x + y;<br />...
Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />
Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />It is all acc...
Where are all the values stored?<br />How are parameters passed?<br />Where are local variables stored?<br />It is all acc...
Calling a function<br />
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<b...
add(x, y)<br />PROLOGUE<br />Push 4<br />Local Variables<br />Push 3<br />BODY<br />CALL add<br />EPILOGUE<br />RET<br />C...
add<br />CALL add<br />RET<br />Calling a function<br />
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 />...
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 ...
How does it all fit together?<br />Let's see what happens on the stack.<br />
How does it all fit together?<br />Let's see what happens on the stack.<br />ESP is the stack pointer.<br />It always poin...
In the beginning<br />ESP points to the top of the stack, as usual<br />...<br />ESP<br />...<br />EBP<br />
In the beginning<br />ESP points to the top of the stack, as usual<br />EBP is the frame pointer (called Base Pointer). It...
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 />
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...
Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<...
Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<...
Prologue<br />The Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.<br />Old EBP<br />EBP<br />ESP<...
Local Variables<br />Local variables are created in the stack memory.<br />sum<br />ESP<br />Old EBP<br />EBP<br />Saved E...
Frame for add()<br />The Stack Frame<br />The stack memory used by a function is termed as its STACK FRAME<br />sum<br />E...
Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />func1()<br />frame fo...
Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />func1()<br />frame fo...
Functions and Frames<br />Each function call results in a new frame being created on the stack.<br />frame for func3 <br /...
frame for func2 <br />frame for func1 <br />Functions and Frames<br />When a function returns, the frame is "unwound" or "...
Functions and Frames<br />And as new functions get invoked, new frames get created.<br />frame for func4 <br />ESP<br />fu...
The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />Old EBP<br />EBP<br />Saved EIP<br />3<br />...
The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />local var<br />Old EBP<br />EBP<br />Local v...
The Frame Pointer<br />EBP is the frame pointer (base pointer).<br />sum<br />EBP - 4<br />Old EBP<br />EBP<br />Local var...
Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br ...
Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br ...
Epilogue<br />The Epilogue cleans up the stack frame. Local variables are effectively destroyed.<br />sum<br />Old EBP<br ...
Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />sum<br />Old EBP<br />Saved EIP<br ...
Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />EIP<br />sum<br />Old EBP<br />Prog...
Return!<br />RET instruction pops the saved EIP value back into the EIP register.<br />EIP<br />sum<br />Old EBP<br />Prog...
Key Concepts<br />
Review<br />
END<br />How Functions Work<br />Saumil Shah<br />Net-Square<br />www.net-square.com<br />
How Functions Work
How Functions Work
How Functions Work
How Functions Work
Nächste SlideShare
Wird geladen in …5
×

How Functions Work

27.549 Aufrufe

Veröffentlicht am

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.

Veröffentlicht in: Technologie, Business
  • Increasing Sex Drive And Getting Harder Erections, Naturally  https://bit.ly/30G1ZO1
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • Legitimate jobs paying $40/h Tap into the booming online job, industry and start working now! ■■■ https://tinyurl.com/y4urott2
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • I made $2,600 with this. I already have 7 days with this...  https://tinyurl.com/make2793amonth
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • @Fedele Mantuano yes i agree
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier
  • very straightforward and excellently designed slides, but it would perhaps be better to show the stack in reverse, to emphasize that it grows downwards
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

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 />

×