From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28227 invoked by alias); 6 Jul 2009 07:40:59 -0000 Received: (qmail 28219 invoked by uid 22791); 6 Jul 2009 07:40:58 -0000 X-SWARE-Spam-Status: No, hits=-1.2 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_53,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail-ew0-f225.google.com (HELO mail-ew0-f225.google.com) (209.85.219.225) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 06 Jul 2009 07:40:51 +0000 Received: by ewy25 with SMTP id 25so4780504ewy.8 for ; Mon, 06 Jul 2009 00:40:48 -0700 (PDT) Received: by 10.210.35.10 with SMTP id i10mr5190089ebi.52.1246866048913; Mon, 06 Jul 2009 00:40:48 -0700 (PDT) Received: from ?192.168.43.19? ([212.143.139.215]) by mx.google.com with ESMTPS id 23sm8128383eya.59.2009.07.06.00.40.47 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 06 Jul 2009 00:40:48 -0700 (PDT) Message-ID: <4A51AA7D.4060809@gmail.com> Date: Mon, 06 Jul 2009 07:40:00 -0000 From: Lennyk User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) MIME-Version: 1.0 To: Ian Lance Taylor CC: gcc-help@gcc.gnu.org Subject: Re: undefined reference to... when using intel inline asm References: <4A4C69F0.7040201@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2009-07/txt/msg00082.txt.bz2 Ian Lance Taylor wrote: > Lennyk 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!