Re: [9fans] pointer to the last TOS



For the intel, the stack pointer is there. What's not there is the
frame pointer. The FP register is simulated, a positive offset from
the stack pointer. To do a back trace, for example, you have to use
the extra stuff in the *.out to get the distance from the stack
pointer to the virtual frame pointer to find the return address.

The call instruction will push the return address where SP is pointing,
so you can't get rid of the stack pointer.

When a function is entered, it allocates as much stack as it's going to need.
When it does a call, it sets the parameters and does a call. No need
to do anything when it gets back. The called funcion has to clean up
the stack so the RET will pop the return address.

The local variables are known by an offset from the stack pointer.
Check out man a.out.

See the following code.

#include <u.h>
#include <libc.h>

void
put(int x, int )
{
print("%d\n", x);
}

void
main(void)
{
char i;

i = 2;
put(i, 3);
}

Generates:

TEXT put+0(SB),0,$12
MOVL $.string<>+0(SB),AX
MOVL AX,(SP)
MOVL x+0(FP),AX
MOVL AX,4(SP)
CALL ,print+0(SB)
RET ,
TEXT main+0(SB),0,$16
MOVB $2,CX
MOVBLSX CX,AX
MOVL AX,(SP)
MOVL $3,AX
MOVL AX,4(SP)
CALL ,put+0(SB)
RET ,
DATA .string<>+0(SB)/8,$"%d\n\z\z\z\z\z"
GLOBL .string<>+0(SB),$8
END ,


Here's part of the symbol table: nm -as 8.out

1 z /usr/bwc/x.c
2 z /386/include/u.h
42 z
43 z /sys/include/libc.h
305 z
315 z
1020 T put
10 m .frame
0 p x
103c T main
14 m .frame
1 z /sys/src/libc/386/main9.s
16 z
1059 T _main
4c m .frame
0 p inargv
fffffffc p inargc

Notice the .frame values.

I have been doing some experiments and reading some code and I have arrived
to the (possibly wrong) conclusion that in the stack there is no pointer to the
TOS before a function was called (I am using 8c)
If this is true, how is the state of the stack
recovered after return?. Can anyone point me to a place where I can read
some documentation or something to solve this kind of doubt.

I read some acid and 8c code, but was too complicated for such a simple doubt.
(I will study 8c but not any time soon). I tried printing the stack of
a process and
didnt find it there. I also read asm.ps and comp.ps.

--
- curiosity sKilled the cat

.



Relevant Pages

  • Re: Is there stack associated when a executing an inline function?
    ... would save the current stack pointer and establish a new stack ... frame since there is no function call. ... moves the stack pointer by some delta to enlarge the current frame, ... termination to release the storage. ...
    (comp.lang.c)
  • Re: Difference Between Stack Pointer and Frame Pointer
    ... The stack pointer operates on the stack. ... >> operates on the frame. ... Most AVR compilers have a data stack separate from the hardware stack. ...
    (comp.arch.embedded)
  • Re: The machine stack and the C language
    ... All stacks have a stack pointer! ... pointer and increase the stack pointer by the size of the item. ... whether they are linear stacks or just logical stacks. ... you push a new frame like this: ...
    (comp.lang.c)
  • Re: StdCall vs. CDecl
    ... I don't understand why "leaving arguments on the stack" is benign. ... If the caller expects the callee to "clean up the stack" (that is, restore the stack pointer to the value it had before the caller pushed a call frame onto the stack), then I would expect the caller to be thoroughly messed up after the call returns and the caller tries to get to its own data on the stack. ... That's for calling a cdecl function as stdcall. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: TCB for pthread in linux kernel 2.4
    ... more detail exactly what you mean by garbage collection? ... > it should know other threads' current stack pointer to form a root-set ... > let the kernel module read relevant threads' stack-pointer and pass ...
    (comp.os.linux.development.system)