From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <308c5cd1e1d85ace80c202f90e490d44@coraid.com> To: 9fans@cse.psu.edu Subject: Re: [9fans] pointer to the last TOS From: Brantley Coile Date: Thu, 2 Mar 2006 10:23:27 -0500 In-Reply-To: <599f06db0603020657u19643c27nad00cbf2998bf877@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: 09ea506e-ead1-11e9-9d60-3106f5b1d025 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 #include 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