Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
How Functions WorkSaumil Shahwww.net-square.com
Introduction
# who am iSaumil ShahCEO Net-square.Hacker, Speaker, Trainer, Author.M.S. Computer SciencePurdue University.Google: "saumil"LinkedIn: saumilshah
Preview
What is a function?
What is a function?A function is a special SUBROUTINE
What is a function?A function is a special SUBROUTINERe-usable block of codeCan be called from anywhere in the program
What is a function?A function is a special SUBROUTINERe-usable block of codeCan be called from anywhere in the programProgram control jumps to the subroutine......and returns to the next statement after completing the subroutine
Anything else?
Anything else?A function accepts parametersA function returns a value
Anything else?A function accepts parametersA function returns a valueIt may also have LOCAL variables...
Anything else?A function accepts parametersA function returns a valueIt 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)int add(int x, int y){      int sum;      sum = x + y;      return(sum);}
An example - add(x, y)Parametersint add(int x, int y){      int sum;      sum = x + y;      return(sum);}Local VariableReturn 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)1PROLOGUE2Local VariablesBODY3s = add(3, 4)EPILOGUEReturnCalling a function4
add(x, y)PROLOGUEPush 4Local VariablesPush 3BODYCALL addEPILOGUERETCalling a function
CALL does two things:addCALL addRETCalling a function
CALL does two things:addPush EIP on the stackJump to the function's addressCALL addRETCalling a function
addCALL addRETCalling a functionCALL does two things:Push EIP on the stackJump to the function's addressRET simply pops the saved EIP value.
How does it all fit together?
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 beginningESP points to the top of the stack, as usual...ESP...EBP
In the beginningESP points to the top of the stack, as usualEBP is the frame pointer (called Base Pointer). It points to regions within the stack....ESP...EBP
Push the parametersFor add(3,4) we push 3 and 4 on the stack.3ESP4......EBP
CALL addCALL pushes the current EIP on the stack......and jumps to add()Saved EIPESP34......EBP
PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPSaved EIP34......
PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPWhat's a FRAME?Saved EIP34......
PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPWhat's a FRAME?Saved EIP3We shall discuss the frame a bit later.4......
Local VariablesLocal variables are created in the stack memory.sumESPOld EBPEBPSaved EIP34......
Frame for add()The Stack FrameThe stack memory used by a function is termed as its STACK FRAMEsumESPOld EBPEBPSaved EIP34......Frame for main()
Functions and FramesEach function call results in a new frame being created on the stack.func1()frame for func1 ESP
Functions and FramesEach function call results in a new frame being created on the stack.func1()frame for func2 ESPfunc2()frame for func1
Functions and FramesEach function call results in a new frame being created on the stack.frame for func3 ESPfunc1()frame for func2 func2()frame for func1 func3()
frame for func2 frame for func1 Functions and FramesWhen a function returns, the frame is "unwound" or "collapsed".func1()ESPfunc2()func3()
Functions and FramesAnd as new functions get invoked, new frames get created.frame for func4 ESPfunc1()frame for func2 func2()frame for func1 func3()func4()
The Frame PointerEBP is the frame pointer (base pointer).sumOld EBPEBPSaved EIP34......
The Frame PointerEBP is the frame pointer (base pointer).sumlocal varOld EBPEBPLocal variables and Parameters are RELATIVE to the frame pointer.Saved EIP3param 14param 2......
The Frame PointerEBP is the frame pointer (base pointer).sumEBP - 4Old EBPEBPLocal variables and Parameters are RELATIVE to the frame pointer.Saved EIP3EBP + 84EBP - n:  Local varsEBP + n: ParametersEBP + 12......
EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPESPEBPSaved EIP34......
EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPPOP EBP. Restores EBP back to the old frame.Saved EIPESP34......EBP
EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPPOP EBP. Restores EBP back to the old frame.Saved EIPESP34Stack 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.sumOld EBPSaved EIPESP34......EBP
Return!RET instruction pops the saved EIP value back into the EIP register.EIPsumOld EBPProgram control is returns to the next statement after add()Saved EIPESP34......EBP
Return!RET instruction pops the saved EIP value back into the EIP register.EIPsumOld EBPProgram control is returns to the next statement after add()Saved EIP3ESP4ESP shifts down by one word.......EBP
Key Concepts
Review
HOW FUNCTIONS WORKsaumil@net-square.com

More Related Content

How Functions Work

  • 1. How Functions WorkSaumil Shahwww.net-square.com
  • 3. # who am iSaumil ShahCEO Net-square.Hacker, Speaker, Trainer, Author.M.S. Computer SciencePurdue University.Google: "saumil"LinkedIn: saumilshah
  • 5. What is a function?
  • 6. What is a function?A function is a special SUBROUTINE
  • 7. What is a function?A function is a special SUBROUTINERe-usable block of codeCan be called from anywhere in the program
  • 8. What is a function?A function is a special SUBROUTINERe-usable block of codeCan be called from anywhere in the programProgram control jumps to the subroutine......and returns to the next statement after completing the subroutine
  • 10. Anything else?A function accepts parametersA function returns a value
  • 11. Anything else?A function accepts parametersA function returns a valueIt may also have LOCAL variables...
  • 12. Anything else?A function accepts parametersA function returns a valueIt may also have LOCAL variables......created when function is invoked,and destroyed when the function returns.Scope limited to that function only.
  • 13. An example - add(x, y)int add(int x, int y){ int sum; sum = x + y; return(sum);}
  • 14. An example - add(x, y)Parametersint add(int x, int y){ int sum; sum = x + y; return(sum);}Local VariableReturn Value
  • 15. Where are all the values stored?How are parameters passed?Where are local variables stored?
  • 16. Where are all the values stored?How are parameters passed?Where are local variables stored?It is all accomplished using the STACK!
  • 17. 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.
  • 19. add(x, y)1PROLOGUE2Local VariablesBODY3s = add(3, 4)EPILOGUEReturnCalling a function4
  • 20. add(x, y)PROLOGUEPush 4Local VariablesPush 3BODYCALL addEPILOGUERETCalling a function
  • 21. CALL does two things:addCALL addRETCalling a function
  • 22. CALL does two things:addPush EIP on the stackJump to the function's addressCALL addRETCalling a function
  • 23. addCALL addRETCalling a functionCALL does two things:Push EIP on the stackJump to the function's addressRET simply pops the saved EIP value.
  • 24. How does it all fit together?
  • 25. How does it all fit together?Let's see what happens on the stack.
  • 26. 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.
  • 27. In the beginningESP points to the top of the stack, as usual...ESP...EBP
  • 28. In the beginningESP points to the top of the stack, as usualEBP is the frame pointer (called Base Pointer). It points to regions within the stack....ESP...EBP
  • 29. Push the parametersFor add(3,4) we push 3 and 4 on the stack.3ESP4......EBP
  • 30. CALL addCALL pushes the current EIP on the stack......and jumps to add()Saved EIPESP34......EBP
  • 31. PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPSaved EIP34......
  • 32. PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPWhat's a FRAME?Saved EIP34......
  • 33. PrologueThe Prologue saves the old frame pointer (EBP) and sets EBP to top of stack.Old EBPEBPESPWhat's a FRAME?Saved EIP3We shall discuss the frame a bit later.4......
  • 34. Local VariablesLocal variables are created in the stack memory.sumESPOld EBPEBPSaved EIP34......
  • 35. Frame for add()The Stack FrameThe stack memory used by a function is termed as its STACK FRAMEsumESPOld EBPEBPSaved EIP34......Frame for main()
  • 36. Functions and FramesEach function call results in a new frame being created on the stack.func1()frame for func1 ESP
  • 37. Functions and FramesEach function call results in a new frame being created on the stack.func1()frame for func2 ESPfunc2()frame for func1
  • 38. Functions and FramesEach function call results in a new frame being created on the stack.frame for func3 ESPfunc1()frame for func2 func2()frame for func1 func3()
  • 39. frame for func2 frame for func1 Functions and FramesWhen a function returns, the frame is "unwound" or "collapsed".func1()ESPfunc2()func3()
  • 40. Functions and FramesAnd as new functions get invoked, new frames get created.frame for func4 ESPfunc1()frame for func2 func2()frame for func1 func3()func4()
  • 41. The Frame PointerEBP is the frame pointer (base pointer).sumOld EBPEBPSaved EIP34......
  • 42. The Frame PointerEBP is the frame pointer (base pointer).sumlocal varOld EBPEBPLocal variables and Parameters are RELATIVE to the frame pointer.Saved EIP3param 14param 2......
  • 43. The Frame PointerEBP is the frame pointer (base pointer).sumEBP - 4Old EBPEBPLocal variables and Parameters are RELATIVE to the frame pointer.Saved EIP3EBP + 84EBP - n: Local varsEBP + n: ParametersEBP + 12......
  • 44. EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPESPEBPSaved EIP34......
  • 45. EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPPOP EBP. Restores EBP back to the old frame.Saved EIPESP34......EBP
  • 46. EpilogueThe Epilogue cleans up the stack frame. Local variables are effectively destroyed.sumOld EBPPOP EBP. Restores EBP back to the old frame.Saved EIPESP34Stack pointer now points to where EIP was saved before CALL add().......EBP
  • 47. Return!RET instruction pops the saved EIP value back into the EIP register.sumOld EBPSaved EIPESP34......EBP
  • 48. Return!RET instruction pops the saved EIP value back into the EIP register.EIPsumOld EBPProgram control is returns to the next statement after add()Saved EIPESP34......EBP
  • 49. Return!RET instruction pops the saved EIP value back into the EIP register.EIPsumOld EBPProgram control is returns to the next statement after add()Saved EIP3ESP4ESP shifts down by one word.......EBP