9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Brantley Coile <brantley@coraid.com>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] pointer to the last TOS
Date: Thu,  2 Mar 2006 10:23:27 -0500	[thread overview]
Message-ID: <308c5cd1e1d85ace80c202f90e490d44@coraid.com> (raw)
In-Reply-To: <599f06db0603020657u19643c27nad00cbf2998bf877@mail.gmail.com>

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



  reply	other threads:[~2006-03-02 15:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-02 14:57 Gorka guardiola
2006-03-02 15:23 ` Brantley Coile [this message]
2006-03-02 15:34   ` Gorka guardiola
2006-03-03  1:54   ` geoff
2006-03-02 15:24 ` Russ Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=308c5cd1e1d85ace80c202f90e490d44@coraid.com \
    --to=brantley@coraid.com \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).