views:

84

answers:

2

Hi All,

I was ready a article posted on wikipedia on Tail recursion: http://en.wikipedia.org/wiki/Tail_call

Now here at the end of the article, the example shows Stack Pointer being used to access the arguments passed to the function call in the assembly pseudo code. Isn't this wrong? I mean the arguments are accessed by the callee by using the frame pointer right rather than the stack pointer?

+3  A: 

A dedicated frame pointer register is definitely a more popular calling convention in common ABIs, but there's nothing intrinsically "wrong" in using a different (possibly simpler) calling convention when it's purely for illustrative purposes (adding a frame pointer register to those snippets would just make them a tad longer and change nothing substantial).

Alex Martelli
thanks for the information. This site is the best in the block right now
darkie15
+4  A: 

Using the stack pointer is fine. It always points to the stack after all. It's just a little difficult to keep track of offsets from the stack pointer to the function arguments if there are any push or pop instructions in the function. And it's really hard to walk the stack back in the debugger when there is no frame pointer.

Using a frame pointer make the job of the debugger and the compiler writer easier, but it's not necessary to have one.

Setting up the frame pointer takes an instruction, and it uses up a register that could potentially be used for other things. So using the stack pointer instead is a common technique for optimizing code. The Microsoft compilers even have a name for this optimization, they call it Frame Pointer Omission

John Knoeller
Precise and brief reply. Thank you for the information John
darkie15
John, just a quick question. When you say the job of a debugger gets easy with the Frame pointer, which debugger are you talking about?
darkie15
Any debugger. When you have frame pointers, it's easy to reconstruct the call stack and show it in a debug window. Without frame pointers, the debugger has to actually decompile the code in order to display a call stack.
John Knoeller
OOhhkie .. So basically debuggers on Windows will alwaya have more job to do. Any idea about Linux if it uses Frame pointer?
darkie15
I's not really an OS decision, the compiler chooses whether to use a frame pointer or not, and it could choose differently for each function if it wanted to. Use of the frame pointer is pretty standard when compiler optimizations are turned off. But I really don't know for sure.
John Knoeller