public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* undefined reference to... when using intel inline asm
@ 2009-07-02  8:04 Lennyk
  2009-07-02 19:52 ` Ian Lance Taylor
  0 siblings, 1 reply; 5+ messages in thread
From: Lennyk @ 2009-07-02  8:04 UTC (permalink / raw)
  To: gcc-help

Hi,

I'm having difficulties translating Windows-style intel assembler to 
Linux (with GCC).

Code (Window-style):
int bswap(int n)
{
    __asm
    {
       mov eax, n
       bswap eax
       mov n, eax
    }
    return n;
}

After looking at some guides/tutorials I've translated this code to Linux:
int bswap(int n)
{
    asm(".intel_syntax noprefix");
    asm("mov eax, n");
    asm("bswap eax");
    asm("mov n, eax");
    asm(".att_syntax noprefix");
    return n;
}

Compilation passes - but the linker shouts: "undefined reference to `n'"

What am I doing wrong? Shouldn't it be straight forward to translate 
these simple commands to Linux?

GCC version: 4.1.2 20080704 (Red Hat 4.1.2-44)
Target: i386-redhat-linux

Thanks!

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

* Re: undefined reference to... when using intel inline asm
  2009-07-02  8:04 undefined reference to... when using intel inline asm Lennyk
@ 2009-07-02 19:52 ` Ian Lance Taylor
  2009-07-06  7:40   ` Lennyk
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2009-07-02 19:52 UTC (permalink / raw)
  To: Lennyk; +Cc: gcc-help

Lennyk <lennyk430@gmail.com> writes:

> Code (Window-style):
> int bswap(int n)
> {
>    __asm
>    {
>       mov eax, n
>       bswap eax
>       mov n, eax
>    }
>    return n;
> }
>
> After looking at some guides/tutorials I've translated this code to Linux:
> int bswap(int n)
> {
>    asm(".intel_syntax noprefix");
>    asm("mov eax, n");
>    asm("bswap eax");
>    asm("mov n, eax");
>    asm(".att_syntax noprefix");
>    return n;
> }
>
> Compilation passes - but the linker shouts: "undefined reference to `n'"
>
> What am I doing wrong? Shouldn't it be straight forward to translate
> these simple commands to Linux?

gcc inline assembler does not work like that.  You can't simply refer to
local variables in the assembler code.  I recommend the friendly manual:

http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Extended-Asm.html

In this case, though, you shouldn't use inline assembler at all, just
use __builtin_bswap32.

http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Other-Builtins.html

Ian

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

* Re: undefined reference to... when using intel inline asm
  2009-07-02 19:52 ` Ian Lance Taylor
@ 2009-07-06  7:40   ` Lennyk
  2009-07-06 15:07     ` Ian Lance Taylor
  0 siblings, 1 reply; 5+ messages in thread
From: Lennyk @ 2009-07-06  7:40 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Ian Lance Taylor wrote:
> Lennyk <lennyk430@gmail.com> writes:
>
>   
>> Code (Window-style):
>> int bswap(int n)
>> {
>>    __asm
>>    {
>>       mov eax, n
>>       bswap eax
>>       mov n, eax
>>    }
>>    return n;
>> }
>>
>> After looking at some guides/tutorials I've translated this code to Linux:
>> int bswap(int n)
>> {
>>    asm(".intel_syntax noprefix");
>>    asm("mov eax, n");
>>    asm("bswap eax");
>>    asm("mov n, eax");
>>    asm(".att_syntax noprefix");
>>    return n;
>> }
>>
>> Compilation passes - but the linker shouts: "undefined reference to `n'"
>>
>> What am I doing wrong? Shouldn't it be straight forward to translate
>> these simple commands to Linux?
>>     
>
> gcc inline assembler does not work like that.  You can't simply refer to
> local variables in the assembler code.  I recommend the friendly manual:
>
> http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Extended-Asm.html
>
> In this case, though, you shouldn't use inline assembler at all, just
> use __builtin_bswap32.
>
> http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Other-Builtins.html
>
> Ian
>
>   
Thanks Ian!

After, reading further the extended asm guide I managed to implement the 
above function - but I still have difficulties with dereferenced pointers.

I would like to translate the following function (Windows-style):

void bswap(int* n)
{
    __asm
    {
       mov eax, [n]
       bswap eax
       mov [n], eax
    }
}

According to the guide - the corresponding GCC translation - should look 
like this:

void bswap(int* n)
{
    asm("movl       %0, %%eax" : : "g" (*n));
    asm("bswap    %eax");
    asm("movl      %%eax, %0" : "=g" (*n));
}

But, for some reason, the result is not a "bswapped" n.
Why?

Thanks!

BTW, __builtin_bswap32 is not available in the GCC version I'm currently 
using - but thanks for the tip!

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

* Re: undefined reference to... when using intel inline asm
  2009-07-06  7:40   ` Lennyk
@ 2009-07-06 15:07     ` Ian Lance Taylor
  2009-07-07 10:51       ` Lennyk
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Lance Taylor @ 2009-07-06 15:07 UTC (permalink / raw)
  To: Lennyk; +Cc: gcc-help

Lennyk <lennyk430@gmail.com> writes:

> According to the guide - the corresponding GCC translation - should
> look like this:
>
> void bswap(int* n)
> {
>    asm("movl       %0, %%eax" : : "g" (*n));
>    asm("bswap    %eax");
>    asm("movl      %%eax, %0" : "=g" (*n));
> }
>
> But, for some reason, the result is not a "bswapped" n.
> Why?

* What does the generated code look like?
* You shouldn't break up the asm statements like that--write one asm
  with three assembler statements.
* Let the compiler do the data movement for you; it's good at it.
* If you're going to clobber %eax in an asm, you need to say so
  explicitly; that's probably why this code sequence is breaking.
  asm("bswap %%eax" : : : "eax");

Anyhow, I would write it like this:

void bswap(int* n)
{
  asm("bswap %0" : "=r" (*n) : "0" (*n));
}

Ian

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

* Re: undefined reference to... when using intel inline asm
  2009-07-06 15:07     ` Ian Lance Taylor
@ 2009-07-07 10:51       ` Lennyk
  0 siblings, 0 replies; 5+ messages in thread
From: Lennyk @ 2009-07-07 10:51 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: gcc-help

Ian Lance Taylor wrote:
> Lennyk <lennyk430@gmail.com> writes:
>
>   
>> According to the guide - the corresponding GCC translation - should
>> look like this:
>>
>> void bswap(int* n)
>> {
>>    asm("movl       %0, %%eax" : : "g" (*n));
>>    asm("bswap    %eax");
>>    asm("movl      %%eax, %0" : "=g" (*n));
>> }
>>
>> But, for some reason, the result is not a "bswapped" n.
>> Why?
>>     
>
> * What does the generated code look like?
> * You shouldn't break up the asm statements like that--write one asm
>   with three assembler statements.
> * Let the compiler do the data movement for you; it's good at it.
> * If you're going to clobber %eax in an asm, you need to say so
>   explicitly; that's probably why this code sequence is breaking.
>   asm("bswap %%eax" : : : "eax");
>
> Anyhow, I would write it like this:
>
> void bswap(int* n)
> {
>   asm("bswap %0" : "=r" (*n) : "0" (*n));
> }
>
> Ian
>
>   
Thanks Ian!

After going over the GCC-Inline Assembly guides - I think I'm starting 
to get the hang of it.

Thanks for your assistance!

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

end of thread, other threads:[~2009-07-07 10:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-02  8:04 undefined reference to... when using intel inline asm Lennyk
2009-07-02 19:52 ` Ian Lance Taylor
2009-07-06  7:40   ` Lennyk
2009-07-06 15:07     ` Ian Lance Taylor
2009-07-07 10:51       ` Lennyk

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).