Re: viewing memory stack?

Chris Dodd <>
11 Dec 2002 22:20:23 -0500

          From comp.compilers

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

From: Chris Dodd <>
Newsgroups: comp.compilers
Date: 11 Dec 2002 22:20:23 -0500
Organization: Compilers Central
References: <lyHH9.4255$> 02-12-047
Keywords: debug
Posted-Date: 11 Dec 2002 22:20:23 EST

"Sandy Dunlop" <sandyATsornDOTnet@nospam.please> 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:
>> ...
>> myvar = myfunc();
>> My problem is that myvar does not get the same value as GDB said r
>> has inside myfunc().
> shr $0x1f,%eax * shift right 31 bits.
> mov %eax,0xffffefd0(%ebp) * Moves the contents of eax onto stack
> test %eax,%eax * Corresponds to the if statement in this:
> * if (myvar=myfunc()<0){

Well, the question is, what is your actual source? Is it "myvar =
myfunc()" or is it "if (myvar=myfunc()<0)" ? I would guess its the
latter, as that's what the assembly code is doing. As comparisons
have higher precedence than assignments in C, this is the same as "if
(myvar = (myfunc() < 0))", so you're setting myvar to the result of
the comparison, not the return value from the function. That's what
the shift is doing -- its extracting the sign bit from the value,
which is what a "< 0" comparison does.

When compiling with gcc, you should ALWAYS use -Wall -- this will give
you warnings about this sort of thing and many other things as well.
You need to add extra parenthesis to make what you're doing very clear
in order to stop the warning.

Chris Dodd

Post a followup to this message

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