Re: viewing memory stack?

"Sandy Dunlop" <sandyATsornDOTnet@nospam.please>
7 Dec 2002 20:05:48 -0500

          From comp.compilers

Related articles
Re: viewing memory stack? sandyATsornDOTnet@nospam.please (Sandy Dunlop) (2002-12-07)
Re: viewing memory stack? chrisd@reservoir.com (Chris Dodd) (2002-12-11)
| List of all articles for this month |

From: "Sandy Dunlop" <sandyATsornDOTnet@nospam.please>
Newsgroups: comp.compilers
Date: 7 Dec 2002 20:05:48 -0500
Organization: Compilers Central
References: <lyHH9.4255$I91.32549000@news-text.cableinet.net>
Keywords: debug, question
Posted-Date: 07 Dec 2002 20:05:48 EST

Sandy Dunlop wrote:


> I have a program I'm debugging and I call a function which returns an
> integer r. The code that calls it assigns this value to a variable:
>
> int myvar;
> ...
> myvar = myfunc();
>
> My problem is that myvar does not get the same value as GDB said r has
> inside myfunc().


I was debugging it on an Athlon MP (with one CPU). Here's some of the ASM
code that GCC 2.96 made, with my comments next to it. I haven't done ASM
for a long time, and have never done it on a real computer, so if I've got
something wrong, please point it out:


eip=0x8054b11
myfunc+


593 mov 0xffffffec(%ebp),%eax * moves return value into register eax
                                                            * variables get stored in eax while
                                                            * being passed back from functions
596 mov %eax,%eax *
598 leave *
599 ret *


*** at this point register eax contains the return value ***


eip=0x8048dda
caller_func+


222 add $0x10,%esp * increase stack pointer
225 mov %eax,%eax * silly thing that GCC does. EFLAGS=0x382
227 shr $0x1f,%eax * shift right 31 bits. EFLAGS=0x365
                                                            * at this point register eax contains zero
                                                            * last 16 bits of EFLAGS regiester change to:
                                                            * 0000 0011 0101 0110 (0x365)
230 mov %eax,0xffffefd0(%ebp) * Moves the contents of eax onto stack
236 mov 0xffffefd0(%ebp),%eax * Moves top of stack into register eax
242 test %eax,%eax * Corresponds to the if statement in this:
                                                            * if (myvar=myfunc()<0){


I think the instruction at caller_func+227 should come after the one at
+230. The shr instruction at +227 removes everything but the bit
representing the sign of the signed integer in eax, so the <0 test can be
made. Why does GCC then think that the new value of eax (zero) should be
pushed onto the stack? It disregards the old value (that was passed back
from myfunc()). Something doesn't seem right here.


FYI, eax contained 90 before the shr instruction.


--
sandy dunlop
www.sandyd.org.uk


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.