This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: undefined reference to... when using intel inline asm
- From: Lennyk <lennyk430 at gmail dot com>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 06 Jul 2009 10:40:45 +0300
- Subject: Re: undefined reference to... when using intel inline asm
- References: <4A4C69F0.7040201@gmail.com> <m3eisyrhgj.fsf@google.com>
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!