stack pointer and frame pointer

Discussion in 'C Programming' started by code break, Oct 18, 2006.

  1. code break

    code break Guest

    Hi all,

    What is the difference between stack pointer and frame pointer ?

    Any suggestions are welcome ,,,
     
    code break, Oct 18, 2006
    #1
    1. Advertising

  2. In article <>,
    code break <> wrote:

    >What is the difference between stack pointer and frame pointer ?


    The C language standard does not know anything about stacks
    or frames. Stacks and frames are artifacts of particular implementations
    and could differ between implementations.

    [Off topic]

    A stack pointer usually changes during the execution of any one
    function, to hold temporary values or to hold variables whose
    lifetime is only the scope of a block. For example,

    int foo(void) {
    int bar;
    int totalbaz;
    totalbaz = 0;
    for (bar = 0; bar<10; bar++) {
    int baz;
    baz = 7*bar*bar*bar - 3*bar*bar + 2*bar + 5;
    totalbaz += baz;
    }
    return totalbaz;
    }

    In this routine, baz only needs to exist within the for() loop,
    so storage space for baz does not need to be allocated until the
    for() loop starts executing, and the storage space could be released
    at the end of the for() loop. baz would be a good candidate for being
    allocated on a stack (in an implementation that used stacks.)
    (Yes, it would be even better in a register, but the example can
    be extended into something too large or complex to hold in registers.)


    A frame pointer, on the other hand, usually points to the beginning
    of the storage space allocated for any one function, and does not
    change during the execution of that function. In the above example,
    there could be a frame pointer pointing to a block of storage,
    and at that block of storage there might be the address to return
    to followed by the storage for totalbaz followed by the storage for bar --
    so inside the for() loop, the compiler might refer to bar as being
    a certain distance relative to the frame pointer. A frame pointer
    usually points to the beginning of the fixed information about an
    invocation of a function, The stack pointer -might- start from
    the end of the fixed information for the frame, or the stack -might-
    be somewhere else completely in memory... that's an implementation
    decision. And some implementations don't use frame pointers at all.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Oct 18, 2006
    #2
    1. Advertising

  3. code break

    jmcgill Guest

    code break wrote:
    > Hi all,
    >
    > What is the difference between stack pointer and frame pointer ?
    >
    > Any suggestions are welcome ,,,
    >


    A stack pointer is an index into a given stack frame.
    A frame pointer represents a boundary of a stack frame.
    This is architecture stuff, not C stuff.
     
    jmcgill, Oct 18, 2006
    #3
  4. "code break" <> writes:
    > What is the difference between stack pointer and frame pointer ?
    >
    > Any suggestions are welcome ,,,


    This is not a C question.

    I suggest consulting a good book on computer architecture, or doing a
    Google search, or posting to comp.arch (probably in that order).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 18, 2006
    #4
  5. In article <G_sZg.1444$rS.1238@fed1read05>,
    jmcgill <> wrote:
    >code break wrote:


    >> What is the difference between stack pointer and frame pointer ?


    >A stack pointer is an index into a given stack frame.


    Sometimes. But it would be perfectly valid for an implementation
    to allocate the maximum possible local variable space all in
    a functions frame, and to have a stack that lives somewhere else
    completely that is used for things such as passing parameters into
    routines. The return address could be passed in a register,
    and the function prolog could store it in the frame (or just keep
    it in the register if the function was a leaf function); thus stacks
    need not have any control information, and can be completely divorced
    from frames (as long as functions are well-behaved about popping
    values off of the stack.)

    >A frame pointer represents a boundary of a stack frame.


    Sometimes. As explored above, stacks and frames go together like
    peanut butter and jam -- you might be accustomed to always putting
    them together, but that's not the only way.

    >This is architecture stuff, not C stuff.


    "architecture" in the "software architecture" sense. The hardware
    architecture limits or guides or optimizes intra-routine communications,
    but stacks and frames are more usually artificats of the
    ABI (Application Binary Interface) than of the hardware.
    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton
     
    Walter Roberson, Oct 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anonymous
    Replies:
    3
    Views:
    399
    S.Tobias
    Jan 13, 2005
  2. anonymous
    Replies:
    4
    Views:
    750
    S.Tobias
    Jan 28, 2005
  3. Sven
    Replies:
    2
    Views:
    5,108
    tragomaskhalos
    Nov 30, 2007
  4. Surinder Singh
    Replies:
    1
    Views:
    1,213
    Richard Bos
    Dec 20, 2007
  5. Replies:
    15
    Views:
    529
    Sjouke Burry
    Oct 30, 2011
Loading...

Share This Page