visit stack, often use BP, why not use SP ?

Programming, for all ages and all languages.
Post Reply
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

visit stack, often use BP, why not use SP ?

Post by david »

i know SP only could visit the top value of stack, BP could visit each value of stack.
many code often use BP visit stack, use SP shortly, why ?
Just For Fun
Meor
Posts: 13
Joined: Fri Mar 14, 2008 11:29 am

Post by Meor »

C calling convention typically uses bp to store the last value for sp. The space between sp and bp is the local variable stack frame. They usually use bp for stack accesses since it defaults to using the SS segment instead of DS. Otherwise you'd have to use a segment override prefix.
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post by Wave »

There is no reason whatsoever at all to use ebp instead of esp in a normal 32-bit flat address space.
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Actually there is - the enter and leave instructions are designed to interact between ebp and esp. It is part of the calling convention of almost all languages. And you need a pointer into the stack if you want to be able to unwind it. Hence, BP/EBP/RBP
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Combuster wrote:Actually there is - the enter and leave instructions are designed to interact between ebp and esp. It is part of the calling convention of almost all languages. And you need a pointer into the stack if you want to be able to unwind it. Hence, BP/EBP/RBP
Correct in that it does make stack frames possible to unwind, but incorrect in that it is not implemented in all ABIs. MIPS doesn't have a dedicated frame pointer, and when it does (-fno-omit-frame-pointer) it doesn't point at the top (high address) of your stack frame, but at the same place where the stack pointer would normally point, the difference being that variadic increases in the stack frame (e.g. use of alloca()) changes the stack pointer and not the frame pointer.

So if you're coding for multiple architectures, it's useful to be able to unwind the stack without relying on a high-water-mark frame pointer, perhaps by the use of debugging symbols (e.g. DWARF CFI unwinding tables), and in which case, the frame pointer is no longer needed.

c.f. Linux 2.4 at least compiles with -fomit-frame-pointer.
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

Post by david »

i think the only reason is that push and pop instruction would be OK if you changed BP value.
Just For Fun
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

david wrote:i think the only reason is that push and pop instruction would be OK if you changed BP value.
Exactly - the frame pointer is useful in situations where you have a dynamically sized stack frame (i.e. use of alloca). It's mainly useful for hand-crafted assembler though, or for nice backtraces. (as I mentioned earlier)
User avatar
david
Member
Member
Posts: 93
Joined: Tue Aug 21, 2007 4:22 am
Location: Beijing.China
Contact:

Post by david »

i know the default segment register is SS if you use bp.
Just For Fun
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post by Wave »

Combuster wrote:Actually there is - the enter and leave instructions are designed to interact between ebp and esp. It is part of the calling convention of almost all languages. And you need a pointer into the stack if you want to be able to unwind it. Hence, BP/EBP/RBP
The enter and leave instructions are not necessary if you don't use EBP... There is no reason to use them if you're not using EBP as a stack frame pointer.
Why you'd want to embed debugging info (stack unwinding) in a production executable is beyond me. And as JamesM said, debugging is often better handled by proper debugging info.
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
exkor
Member
Member
Posts: 111
Joined: Wed May 23, 2007 9:38 pm

Post by exkor »

Wave wrote:The enter and leave instructions are not necessary if you don't use EBP... There is no reason to use them if you're not using EBP as a stack frame pointer.
But you still want LEAVE on x86 because its faster than standard epilogue (Hey thats what AMD optimization manual says).
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Wave wrote:The enter and leave instructions are not necessary if you don't use EBP... There is no reason to use them if you're not using EBP as a stack frame pointer.
Enter and leave imply the use of EBP and ESP. If you were to use EDX instead, then enter and leave become pretty pointless. Together with the automatic use of SS, this forms the reason why EBP is used rather than some random other register.
Why you'd want to embed debugging info (stack unwinding) in a production executable is beyond me.
To be able to debug errors random users give you. With the help of a stacktrace, execution log and error message other people gave me, I have over time worked out lots of problems I never thought would happen.
And as JamesM said, debugging is often better handled by proper debugging info.
Since when is a stacktrace a bad way of diagnosing problems?

Actually, a quick google revealed stack unwinding (and hence, EBP) is used for structured exception handling as well (i.e, non-debugging purposes)

edit:
JamesM wrote:... but incorrect in that it is not implemented in all ABIs. MIPS doesn't have a dedicated frame pointer
I said languages, and I doubt MIPS has a register named (E)BP.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

standard epilogue
Standard epilogue includes a frame pointer change. If you didn't need that, that would be one memory access down. Memory access times are large, you what you lose by using several instructions instead of one macroinstruction you gain by having one less memory access.

Two consecutive instructions are less likely to cause a cache miss than one (heavily microcoded) instruction and one memory fetch at the top of the current stack frame.
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post by Wave »

exkor wrote:
Wave wrote:The enter and leave instructions are not necessary if you don't use EBP... There is no reason to use them if you're not using EBP as a stack frame pointer.
But you still want LEAVE on x86 because its faster than standard epilogue (Hey thats what AMD optimization manual says).
Well, the manual is wrong then. LEAVE is faster than updating EBP manually, but it's not faster than dropping EBP altogether.
Combuster wrote:
Why you'd want to embed debugging info (stack unwinding) in a production executable is beyond me.
To be able to debug errors random users give you. With the help of a stacktrace, execution log and error message other people gave me, I have over time worked out lots of problems I never thought would happen.
When they have problems, send them the debug build. Why send the debug build to everyone???
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

Wave wrote:When they have problems, send them the debug build. Why send the debug build to everyone???
LOL, I can see Microsoft sending a debug build to everyone experiencing a crash of say IE instead of the "report problem" button dialog.


JAL
Post Reply