• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The Stack Frame
 

The Stack Frame

on

  • 1,862 views

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

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

Statistics

Views

Total Views
1,862
Views on SlideShare
1,788
Embed Views
74

Actions

Likes
0
Downloads
18
Comments
0

2 Embeds 74

http://tsarstva.bg 73
http://www.slashdocs.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Stack Frame The Stack Frame Presentation Transcript

    • The Stack FrameAn Ivaylo Marinkov presentation ivaylo@tsarstva.bg
    • 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.
    • 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 the point where the callhas been made.Routines may be nested to any leveland recursion is possibe – hence thestack structure.
    • 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.
    • 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.
    • 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.
    • 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.
    • Structure A call stack is composed of stack frames, machine and application banary interface- dependant data structures containing subroutine state information.
    • The Stack FrameGenerally speaking, aprocedures stack frame containsall the information necessary tosave and restore the state of theprocedure.
    • 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.
    • 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.
    • 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
    • 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
    • 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.
    • 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.
    • 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.
    • 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
    • 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 first breakpoint.
    • 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).
    • 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.
    • 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.