Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
The Stack FrameAn Ivaylo Marinkov presentation        ivaylo@tsarstva.bg
Call StackA stack data structure storinginformation of a computer programsactive subroutines.Whilst important for the soft...
Call Stack PurposeKeeping record of the point to whicheach active subroutine should returncontrol when finished.
Active SubroutineA routine that has been called buthas not finished execution yet.Afterwords, control should bereturned to...
For example, the DrawSquaresubroutine calls the DrawLinesubroutine from four differentplaces. DrawLine must know whereto r...
Call Stack Inner Workings The caller pushes the return address onto the stack (winding). The called subroutine, when it fi...
Should pushing consume all thespace allocated for the call stack, anerror called stack overflow willoccur.There is usually...
Additional Call Stack FunctionsLocal data storage – keeping local-scopevariable values.Parameter passing – storage for val...
Structure       A call stack is       composed of       stack frames,       machine and       application banary       int...
The Stack FrameGenerally speaking, aprocedures stack frame containsall the information necessary tosave and restore the st...
Strictly speaking, it is only necessaryfor the calling program and thecalled procedure to agree on thestructure of the sta...
The frame pointer is                                 stored in register $30,                                 also known as...
1. Pass the arguments. The firstfour arguments are passed inregisters $a0-$a3. The remainingarguments are pushed onto thes...
1. Pass the arguments. The firstfour arguments are passed inregisters $a0-$a3. The remainingarguments are pushed onto thes...
Within the called routine, the following stepsare necessary:1. Establish the stack frame by subtractingthe frame size from...
To return from a call, a function places thereturned value into $v0 and executes thefollowing steps:1. Restore any callee-...
DebuggingThe purpose of a debugger suchas GDB (gnu.org/software/gdb) isto allow you to see what is goingon “inside” anothe...
In Practice        An illustration to        viewing the call        stack using GDB.        1. Compile your        progra...
2. Navigate to your programs directoryand run GDB: gdb your_programIf all went fine, you will land on acommand prompt.
3. Then install some breakpoints using thebreak command:break function_name4. Now run. The program will proceeduntil the f...
You can select a frame using the frame ncommand and view frame informationusing the info frame n command.Some of the detai...
View the call stack using the backtracecommand. A backtrace is a summary ofhow your program got where it is. Itshows one l...
Resourceshttps://en.wikipedia.org/wiki/Call_stackhttp://www.cs.uaf.edu/~cs301/notes/Chapter9/node11.htmlhttp://chortle.ccs...
Upcoming SlideShare
Loading in …5
×

The Stack Frame

3,539 views

Published on

A presentation about the stack frame by Ivaylo Marinkov http://tsarstva.bg

Published in: Education
  • Be the first to comment

The Stack Frame

  1. 1. The Stack FrameAn Ivaylo Marinkov presentation ivaylo@tsarstva.bg
  2. 2. Call StackA stack data structure storinginformation of a computer programsactive subroutines.Whilst important for the softwaresproper functioning, its details areusually hidden and usage is automatedin high-level programming languages.
  3. 3. Call Stack PurposeKeeping record of the point to whicheach active subroutine should returncontrol when finished.
  4. 4. Active SubroutineA routine that has been called buthas not finished execution yet.Afterwords, control should bereturned to the point where the callhas been made.Routines may be nested to any leveland recursion is possibe – hence thestack structure.
  5. 5. For example, the DrawSquaresubroutine calls the DrawLinesubroutine from four differentplaces. DrawLine must know whereto return once completed.This is accomplished by pushing theaddress following the callinstruction – the return address –onto the stack with each call.
  6. 6. Call Stack Inner Workings The caller pushes the return address onto the stack (winding). The called subroutine, when it finishes, pops the return address off the stack and transfers control to it (unwinding). If a called subroutine calls on yet another subroutine, it will push another address onto the stack, and so on, with the information stacking up and unstacking as the program dictates.
  7. 7. Should pushing consume all thespace allocated for the call stack, anerror called stack overflow willoccur.There is usually a single call stackassociated with each process thread.However, the program may createadditional call stacks for tasks such assignal-hadling or cooperativemultitasking.
  8. 8. Additional Call Stack FunctionsLocal data storage – keeping local-scopevariable values.Parameter passing – storage for valuespassed by calling code.Evalution stack – in some casesoperands for logical andarithmetic operations may bestored in the call stack.Current instance pointer –for this pointer inobject-oriented languages.
  9. 9. Structure A call stack is composed of stack frames, machine and application banary interface- dependant data structures containing subroutine state information.
  10. 10. The Stack FrameGenerally speaking, aprocedures stack frame containsall the information necessary tosave and restore the state of theprocedure.
  11. 11. Strictly speaking, it is only necessaryfor the calling program and thecalled procedure to agree on thestructure of the stack frame for eachprocedure call.However, the specification of acalling convention facilitates the useof procedure libraries by definingthe structure of the stack frameuniformly for all procedure calls.
  12. 12. The frame pointer is stored in register $30, also known as $fp. A stack frame consists of the memory on the stack between the frame pointer and the stack pointer. Three steps are necessary to call aCalling convention used in theMIPS architecture stack frame procedure.
  13. 13. 1. Pass the arguments. The firstfour arguments are passed inregisters $a0-$a3. The remainingarguments are pushed onto thestack.2. Save the caller-saved registers.This includes registers $t0-$t9, ifthey contain live values at the callsite. The endless MIPS3. Execute a jal instruction. cycle
  14. 14. 1. Pass the arguments. The firstfour arguments are passed inregisters $a0-$a3. The remainingarguments are pushed onto thestack.2. Save the caller-saved registers.This includes registers $t0-$t9, ifthey contain live values at the callsite. The endless MIPS3. Execute a jal instruction. cycle
  15. 15. Within the called routine, the following stepsare necessary:1. Establish the stack frame by subtractingthe frame size from the stack pointer.2. Save the callee-saved registers in theframe. Register $fp is always saved. Register$ra and registers $a0-$a3 need to be saved ifthey are in use and the routine itself makescalls. Any of the registers $s0- $s7 that areused by the callee need to be saved.3. Establish the frame pointer by adding thestack frame size to the address in $sp.
  16. 16. To return from a call, a function places thereturned value into $v0 and executes thefollowing steps:1. Restore any callee-saved registers thatwere saved upon entry.2. Pop the stack frame by subtracting theframe size from $sp.3. Return by jumping to the address inregister $ra.
  17. 17. DebuggingThe purpose of a debugger suchas GDB (gnu.org/software/gdb) isto allow you to see what is goingon “inside” another programwhile it executes--or whatanother program was doing atthe moment it crashed.
  18. 18. In Practice An illustration to viewing the call stack using GDB. 1. Compile your program with the -g option, like cc -g -o p1 p1.c
  19. 19. 2. Navigate to your programs directoryand run GDB: gdb your_programIf all went fine, you will land on acommand prompt.
  20. 20. 3. Then install some breakpoints using thebreak command:break function_name4. Now run. The program will proceeduntil the first breakpoint.
  21. 21. You can select a frame using the frame ncommand and view frame informationusing the info frame n command.Some of the details this commanddisplays are the addresses of the frame,the next frame down (called by thisframe) and the next frame up (caller ofthis frame).
  22. 22. View the call stack using the backtracecommand. A backtrace is a summary ofhow your program got where it is. Itshows one line per frame, for manyframes, starting with the one currentlyin execution (frame zero), followed by itscaller (frame one), and on up the stack.
  23. 23. Resourceshttps://en.wikipedia.org/wiki/Call_stackhttp://www.cs.uaf.edu/~cs301/notes/Chapter9/node11.htmlhttp://chortle.ccsu.edu/assemblytutorial/Chapter-26/ass26_4.htmlhttp://www.chemie.fu-berlin.de/chemnet/use/info/gdb/http://www.freebsd.org/doc/en/books/developers-handbook/debugging.htmlhttps://www.gnu.org/software/gdb/Background images from various sources. This presentation was created using only free and open source software including the Ubuntu Linux operating system, LibreOffice, Mozilla Firefox, Geany, GDB and KolourPaint.

×