This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: gcc and asm keyword
- From: Andrew Haley <aph at redhat dot com>
- To: aloksethi <alok dot sethi1 at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 22 Sep 2009 09:23:41 +0100
- Subject: Re: gcc and asm keyword
- References: <25530602.post@talk.nabble.com>
aloksethi wrote:
> Hi,
> i am writing a test code using asm keyword.
>
> int fun(int in)
> {
> int b=2;
> asm("movl $100, %0;"
> "test %1, %1;"
> "cmovl %1, %0;"
> : "=r" (b)
> : "r" (in)
> );
> return b;
> }
>
> int main()
> {
> int a=8;
> int b;
>
> b = fun(-1);
> printf("%d %d\n",a,b);
> }
> the output of above code should be "8 -1", ie the value of "b" in main after
> calling "fun" should be "-1". instead the output is "8 100"
>
>
> the assemble output is
> fun:
> pushl %ebp
> movl %esp, %ebp
> subl $16, %esp
> movl $2, -4(%ebp)
> movl 8(%ebp), %eax
> #APP
> # 6 "asm.c" 1
> movl $100, %eax;test %eax, %eax;cmovl %eax, %eax;
> # 0 "" 2
> #NO_APP
> movl %eax, -4(%ebp)
> movl -4(%ebp), %eax
> leave
> ret
>
> my main motto of writing this code was to test the "cvmol" instruction.
> so basically whenever the test instruction will show that the result is less
> than 0, the "cmov" instruction should move the data
>
> i used the mov instruction so that initially 100 gets moved into variable
> "b" and if the test ins indicate the input value is -ve, cmov instruction
> should move the input value to the variable "b"
>
> just check the assembly code that gcc is generating, gcc is using eax for
> both %0 and %1
> due to which the code is giving wrong result
gcc is allowed to use the same registers for inputs and outputs. if you want
to use different ones, you must use a constraint of "=&r" not "=r" on the
output.
Andrew.