public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inline ASM and stack/base pointer
@ 2011-06-09 15:22 Daniel Mierswa
  2011-06-09 18:09 ` Ian Lance Taylor
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Mierswa @ 2011-06-09 15:22 UTC (permalink / raw)
  To: gcc-help

Hi list,

I was wondering how to overcome the issue that evolves when parameters
(inputs) to the inline assembly are passed relative to ebp/esp and
modifying those pointers in the inline assembly. Basically
__asm__("push %ecx\nmov %0, %edx\n"::"m"(variable)); could set '%0' to
4(%esp) or similar which would no longer represent the same location
once I use push/pop in my asm. Is it possible to explicitly state that
GCC passes those relative to esp _or_ ebp so I can safely modify at
least one pointer (i.e. reserve stack space)? Or are there any other
ways to overcome this issue? Thanks in advance.

-- 
Mierswa, Daniel

If you still don't like it, that's ok: that's why I'm boss. I simply
know better than you do.
               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 15:22 Inline ASM and stack/base pointer Daniel Mierswa
@ 2011-06-09 18:09 ` Ian Lance Taylor
  2011-06-09 18:47   ` kevin diggs
                     ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Ian Lance Taylor @ 2011-06-09 18:09 UTC (permalink / raw)
  To: Daniel Mierswa; +Cc: gcc-help

Daniel Mierswa <impulze@impulze.org> writes:

> I was wondering how to overcome the issue that evolves when parameters
> (inputs) to the inline assembly are passed relative to ebp/esp and
> modifying those pointers in the inline assembly. Basically
> __asm__("push %ecx\nmov %0, %edx\n"::"m"(variable)); could set '%0' to
> 4(%esp) or similar which would no longer represent the same location
> once I use push/pop in my asm. Is it possible to explicitly state that
> GCC passes those relative to esp _or_ ebp so I can safely modify at
> least one pointer (i.e. reserve stack space)? Or are there any other
> ways to overcome this issue? Thanks in advance.

Add "%esp" to your clobber list.  That should force the compiler to pass
any stack variables using an offset from %ebp.

Ian

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 18:09 ` Ian Lance Taylor
@ 2011-06-09 18:47   ` kevin diggs
  2011-06-09 19:53     ` Ian Lance Taylor
  2011-06-09 20:26   ` Daniel Mierswa
  2011-06-09 20:41   ` Daniel Mierswa
  2 siblings, 1 reply; 8+ messages in thread
From: kevin diggs @ 2011-06-09 18:47 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Daniel Mierswa, gcc-help

Hi,

On Thu, Jun 9, 2011 at 12:49 PM, Ian Lance Taylor <iant@google.com> wrote:
> Daniel Mierswa <impulze@impulze.org> writes:
>
>> I was wondering how to overcome the issue that evolves when parameters
>> (inputs) to the inline assembly are passed relative to ebp/esp and
>> modifying those pointers in the inline assembly. Basically
>> __asm__("push %ecx\nmov %0, %edx\n"::"m"(variable)); could set '%0' to
>> 4(%esp) or similar which would no longer represent the same location
>> once I use push/pop in my asm. Is it possible to explicitly state that
>> GCC passes those relative to esp _or_ ebp so I can safely modify at
>> least one pointer (i.e. reserve stack space)? Or are there any other
>> ways to overcome this issue? Thanks in advance.
>
> Add "%esp" to your clobber list.  That should force the compiler to pass
> any stack variables using an offset from %ebp.
>
> Ian
>

What happens if you use -fomit-frame-pointer?

If you muck with esp, do you have to put it back? In other words if
you include "%esp" in your clobber, does the compiler back up and
restore it?

Thanks!

kevin

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 18:47   ` kevin diggs
@ 2011-06-09 19:53     ` Ian Lance Taylor
  0 siblings, 0 replies; 8+ messages in thread
From: Ian Lance Taylor @ 2011-06-09 19:53 UTC (permalink / raw)
  To: kevin diggs; +Cc: Daniel Mierswa, gcc-help

kevin diggs <diggskevin38@gmail.com> writes:

> On Thu, Jun 9, 2011 at 12:49 PM, Ian Lance Taylor <iant@google.com> wrote:
>> Daniel Mierswa <impulze@impulze.org> writes:
>>
>>> I was wondering how to overcome the issue that evolves when parameters
>>> (inputs) to the inline assembly are passed relative to ebp/esp and
>>> modifying those pointers in the inline assembly. Basically
>>> __asm__("push %ecx\nmov %0, %edx\n"::"m"(variable)); could set '%0' to
>>> 4(%esp) or similar which would no longer represent the same location
>>> once I use push/pop in my asm. Is it possible to explicitly state that
>>> GCC passes those relative to esp _or_ ebp so I can safely modify at
>>> least one pointer (i.e. reserve stack space)? Or are there any other
>>> ways to overcome this issue? Thanks in advance.
>>
>> Add "%esp" to your clobber list.  That should force the compiler to pass
>> any stack variables using an offset from %ebp.
>>
>> Ian
>>
>
> What happens if you use -fomit-frame-pointer?

Doesn't matter, -fomit-frame-pointer doesn't mean never use a frame
pointer, it means only use one if you need one.  If an asm clobbers
%esp, then the function needs a frame pointer.

> If you muck with esp, do you have to put it back? In other words if
> you include "%esp" in your clobber, does the compiler back up and
> restore it?

You do have to keep it the same.  The compiler will not preserve it for
you.

Ian

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 18:09 ` Ian Lance Taylor
  2011-06-09 18:47   ` kevin diggs
@ 2011-06-09 20:26   ` Daniel Mierswa
  2011-06-09 20:41   ` Daniel Mierswa
  2 siblings, 0 replies; 8+ messages in thread
From: Daniel Mierswa @ 2011-06-09 20:26 UTC (permalink / raw)
  To: gcc-help

On 09.06.2011 17:49, Ian Lance Taylor wrote:
> Add "%esp" to your clobber list.  That should force the compiler to pass
> any stack variables using an offset from %ebp.
I assume I should use "m" as constraint, right? If so, that didn't do
the trick, the generated assembly still contains 0xbc(%%esp) where it
should use %%ebp.

-- 
Mierswa, Daniel

If you still don't like it, that's ok: that's why I'm boss. I simply
know better than you do.
               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 18:09 ` Ian Lance Taylor
  2011-06-09 18:47   ` kevin diggs
  2011-06-09 20:26   ` Daniel Mierswa
@ 2011-06-09 20:41   ` Daniel Mierswa
       [not found]     ` <BANLkTikg+N_E=OYU-wU2Od=2rsh=K=Ktsg@mail.gmail.com>
  2 siblings, 1 reply; 8+ messages in thread
From: Daniel Mierswa @ 2011-06-09 20:41 UTC (permalink / raw)
  To: gcc-help

On 09.06.2011 17:49, Ian Lance Taylor wrote:
> Add "%esp" to your clobber list.  That should force the compiler to pass
> any stack variables using an offset from %ebp.

Ok it does seem to have an effect after all, as long as I don't compile
it with -O2, as soon as -O2 is passed, it uses esp instead of ebp and
does not even build up a base pointer at all in the generated assembly.

-- 
Mierswa, Daniel

If you still don't like it, that's ok: that's why I'm boss. I simply
know better than you do.
               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22

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

* Re: Inline ASM and stack/base pointer
       [not found]     ` <BANLkTikg+N_E=OYU-wU2Od=2rsh=K=Ktsg@mail.gmail.com>
@ 2011-06-09 23:12       ` Daniel Mierswa
  2011-06-10  1:21         ` Ian Lance Taylor
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Mierswa @ 2011-06-09 23:12 UTC (permalink / raw)
  To: gcc-help

On 09.06.2011 20:02, Paul Römer wrote:
> Hi,
> 
> What happens if you compile only with -fomit-frame-pointer? I'm not sure
> if it will be activated without adding an -Ox option. Can you add an
> example code?
impulze@istari ~ $ gcc-4.4 -S -fomit-frame-pointer test.c && grep -m1
lea test.s
	lea 12(%esp), %edi
impulze@istari ~ $ gcc-4.5 -S -fomit-frame-pointer test.c && grep -m1
lea test.s
	lea 12(%esp), %edi
impulze@istari ~ $ gcc-4.6 -S -fomit-frame-pointer test.c && grep -m1
lea test.s
	lea 12(%esp), %edi
impulze@istari ~ $ cat test.c
void f()
{
    int i;
    __asm__ __volatile__("lea %0, %%edi\n"::"m"(i):"%esp");
}

> What gcc you use?
4.4.5, 4.5.2, 4.6.0

-- 
Mierswa, Daniel

If you still don't like it, that's ok: that's why I'm boss. I simply
know better than you do.
               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22

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

* Re: Inline ASM and stack/base pointer
  2011-06-09 23:12       ` Daniel Mierswa
@ 2011-06-10  1:21         ` Ian Lance Taylor
  0 siblings, 0 replies; 8+ messages in thread
From: Ian Lance Taylor @ 2011-06-10  1:21 UTC (permalink / raw)
  To: Daniel Mierswa; +Cc: gcc-help

Daniel Mierswa <impulze@impulze.org> writes:

>> What happens if you compile only with -fomit-frame-pointer? I'm not sure
>> if it will be activated without adding an -Ox option. Can you add an
>> example code?
> impulze@istari ~ $ gcc-4.4 -S -fomit-frame-pointer test.c && grep -m1
> lea test.s
> 	lea 12(%esp), %edi
> impulze@istari ~ $ gcc-4.5 -S -fomit-frame-pointer test.c && grep -m1
> lea test.s
> 	lea 12(%esp), %edi
> impulze@istari ~ $ gcc-4.6 -S -fomit-frame-pointer test.c && grep -m1
> lea test.s
> 	lea 12(%esp), %edi
> impulze@istari ~ $ cat test.c
> void f()
> {
>     int i;
>     __asm__ __volatile__("lea %0, %%edi\n"::"m"(i):"%esp");
> }
>
>> What gcc you use?
> 4.4.5, 4.5.2, 4.6.0

Bother, you're right, I was looking at an invalid example.  I don't know
of a way to do this.  Sorry.

Ian

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

end of thread, other threads:[~2011-06-09 20:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-09 15:22 Inline ASM and stack/base pointer Daniel Mierswa
2011-06-09 18:09 ` Ian Lance Taylor
2011-06-09 18:47   ` kevin diggs
2011-06-09 19:53     ` Ian Lance Taylor
2011-06-09 20:26   ` Daniel Mierswa
2011-06-09 20:41   ` Daniel Mierswa
     [not found]     ` <BANLkTikg+N_E=OYU-wU2Od=2rsh=K=Ktsg@mail.gmail.com>
2011-06-09 23:12       ` Daniel Mierswa
2011-06-10  1:21         ` Ian Lance Taylor

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 read-only IMAP folder(s) and NNTP newsgroup(s).