public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ian Lance Taylor <iant@google.com>
To: Lennyk <lennyk430@gmail.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: undefined reference to... when using intel inline asm
Date: Thu, 02 Jul 2009 19:52:00 -0000	[thread overview]
Message-ID: <m3eisyrhgj.fsf@google.com> (raw)
In-Reply-To: <4A4C69F0.7040201@gmail.com> (Lennyk's message of "Thu\, 02 Jul 2009 11\:04\:00 +0300")

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

  reply	other threads:[~2009-07-02 19:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-02  8:04 Lennyk
2009-07-02 19:52 ` Ian Lance Taylor [this message]
2009-07-06  7:40   ` Lennyk
2009-07-06 15:07     ` Ian Lance Taylor
2009-07-07 10:51       ` Lennyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3eisyrhgj.fsf@google.com \
    --to=iant@google.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=lennyk430@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).