www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/09/19:46:02

From: Endlisnis <s257m AT unb DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Not a bug in GCC. Was: Bug in GCC?
Date: Wed, 09 Sep 1998 18:01:29 -0300
Organization: NBTel Internet
Lines: 56
Message-ID: <35F6ECA9.2AD41252@unb.ca>
References: <m0zGjnM-000S4TC AT inti DOT gov DOT ar>
NNTP-Posting-Host: fctnts10c05.nbnet.nb.ca
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Salvador Eduardo Tropea (SET) wrote:

> int add(int a,int b)
> {
>  int ret;
>  asm ("movl %2,%0; addl %1,%0" : "rm="(ret) : "r"(b), "r"(a));
>  return ret;
> }
	But, your requires an extra move!  I actually found the problem in my code,
not the compiler.

> > int add(int a, int b)
> > {
> >  int Ret;
> >  asm ("addl %1, %0"  //this line compiles to "addl ecx,ecx".
> >  : "=&r" (Ret)
> >  : "0" (a), "r" (b)
> >  );
> >  return Ret;
> >  }
	As you can see I've allocated %0 to the output register (will be %eax), and
I'm using it as an input register as well.  My belief was that %0 would the
(Ret) & (a), and %1 would be (b).  But that is WRONG!  That code makes %0 =
(Ret), %1 = (a), %2 = (b).  This caused %0 to be the SAME as %1, and thus my
problem.  The solution was to change "addl %1, %0" to "addl %2, %0" and then
everything worked fine, but then I realized there was no need to load (b) into
a register at all, so here's the final working code:

int add(int a, int b)
{
 int Ret;
 asm ("addl %2, %0"
 : "=&r" (Ret)
 : "0" (a), "mr" (b)
 );
 return Ret;
 }

Which compiles (with -O2 -m486) to:
	pushl %ebp
	movl %esp,%ebp
	movl 8(%ebp),%eax
	addl 12(%ebp), %eax
	leave
	ret




-- 
     (\/) Endlisnis (\/)
          s257m AT unb DOT ca
          Endlisnis AT GeoCities DOT com
          Endlis AT nbnet DOT nb DOT ca


- Raw text -


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