9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] pointer to the last TOS
@ 2006-03-02 14:57 Gorka guardiola
  2006-03-02 15:23 ` Brantley Coile
  2006-03-02 15:24 ` Russ Cox
  0 siblings, 2 replies; 5+ messages in thread
From: Gorka guardiola @ 2006-03-02 14:57 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] pointer to the last TOS
  2006-03-02 14:57 [9fans] pointer to the last TOS Gorka guardiola
@ 2006-03-02 15:23 ` Brantley Coile
  2006-03-02 15:34   ` Gorka guardiola
  2006-03-03  1:54   ` geoff
  2006-03-02 15:24 ` Russ Cox
  1 sibling, 2 replies; 5+ messages in thread
From: Brantley Coile @ 2006-03-02 15:23 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] pointer to the last TOS
  2006-03-02 14:57 [9fans] pointer to the last TOS Gorka guardiola
  2006-03-02 15:23 ` Brantley Coile
@ 2006-03-02 15:24 ` Russ Cox
  1 sibling, 0 replies; 5+ messages in thread
From: Russ Cox @ 2006-03-02 15:24 UTC (permalink / raw)
  To: 9fans

> 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.

In the Plan 9 C compiler each stack frame has a constant size.
The beginning of a function does SUBL $XXX, SP
and then the end of a function does ADDL $XXX, SP
to restore the stack pointer before returning.
If you compile and then disassemble using acid
you will see the instructions.  They don't appear in the
8c -S output explicitly.  Instead the TEXT line specifies
the frame size (TEXT f(SB), 0, $XXX) and the linker
inserts the necessary instructions.

Russ



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] pointer to the last TOS
  2006-03-02 15:23 ` Brantley Coile
@ 2006-03-02 15:34   ` Gorka guardiola
  2006-03-03  1:54   ` geoff
  1 sibling, 0 replies; 5+ messages in thread
From: Gorka guardiola @ 2006-03-02 15:34 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Thanks to all, very clarifying.
--
- curiosity sKilled the cat


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] pointer to the last TOS
  2006-03-02 15:23 ` Brantley Coile
  2006-03-02 15:34   ` Gorka guardiola
@ 2006-03-03  1:54   ` geoff
  1 sibling, 0 replies; 5+ messages in thread
From: geoff @ 2006-03-03  1:54 UTC (permalink / raw)
  To: 9fans

Another way to see the stack-manipulation instructions is to look at
the output of 8l -a; here's the start of it using Brantley's example:

: cpu; 8l -a t.8|p
001020 	(776)	TEXT	put+0(SB),$12
001020 83ec0c	(776)	SUBL	$12,SP
001023 b80c690000	(778)	MOVL	$.string<1>+0(SB),AX
001028 890424	(778)	MOVL	AX,(SP)
00102b 8b442410	(778)	MOVL	x+16(FP),AX
00102f 89442404	(778)	MOVL	AX,4(SP)
001033 e86e010000	(778)	CALL	,11a6+print
001038 83c40c	(778)	ADDL	$12,SP
00103b c3	(778)	RET	,
00103c 	(782)	TEXT	main+0(SB),$16
00103c 83ec10	(782)	SUBL	$16,SP
00103f b102	(786)	MOVB	$2,CX
001041 0fbec1	(787)	MOVBLSX	CX,AX
001044 890424	(787)	MOVL	AX,(SP)
001047 b803000000	(787)	MOVL	$3,AX
00104c 89442404	(787)	MOVL	AX,4(SP)
001050 e8cbffffff	(787)	CALL	,1020+put
001055 83c410	(787)	ADDL	$16,SP
001058 c3	(787)	RET	,



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-03-03  1:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-02 14:57 [9fans] pointer to the last TOS Gorka guardiola
2006-03-02 15:23 ` Brantley Coile
2006-03-02 15:34   ` Gorka guardiola
2006-03-03  1:54   ` geoff
2006-03-02 15:24 ` Russ Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).