delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/07/30/18:01:19

Message-Id: <5.0.2.1.2.20020730223902.009e37d0@pop.gmx.net>
X-Sender: martinSteuer AT gmx DOT de@pop.gmx.net
X-Mailer: QUALCOMM Windows Eudora Version 5.0.2
Date: Tue, 30 Jul 2002 23:01:26 +0200
To: djgpp AT delorie DOT com
From: Martin Steuer <martinSteuer AT gmx DOT de>
Subject: Inline Assembly and return value of a function
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Hello Guys!

I've the following problem: I have written a small byte-swapping inline asm 
routine which simple executes the i386-bswap on a longword. The best to 
explain is i show the code:

//byte swapping func
static inline unsigned bswap(unsigned v)
{
    unsigned register swapped;
    __asm__ __volatile(
            "bswapl %1\n"
            : "=r"(swapped)
            : "r"(v)
            );
    return swapped;
}

/* a crc-division function, which returns the reminder of the division at 
the end (endian swapped)
*/

[..]
return bswap(r);

The code is optimized with -O2, the compiler inlines bswap nicely. In my 
case r is located in ecx so the swapping code takes just 1 instruction.

Now the problem: the return statement doesnt move the result of bswap() 
into eax, thus some garbage is returned.
However if i explicitly use eax as the input and output register in the 
inline assembly, it works. Or if i alternatively do:

[..]
r = bswap(r);
return r;

this also works perfectly. So what have i done wrong, can anyone help?

Thanks,
Martin 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019