stack pointer and frame pointer

C

code break

Hi all,

What is the difference between stack pointer and frame pointer ?

Any suggestions are welcome ,,,
 
W

Walter Roberson

code break said:
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.
 
J

jmcgill

code said:
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.
 
K

Keith Thompson

code break said:
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).
 
W

Walter Roberson

code break wrote:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top