gcc and asm keyword

aloksethi alok.sethi1@gmail.com
Tue Sep 22 07:23:00 GMT 2009

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
  pushl %ebp
  movl  %esp, %ebp
  subl  $16, %esp
  movl  $2, -4(%ebp)
  movl  8(%ebp), %eax
# 6 "asm.c" 1
  movl $100, %eax;test %eax, %eax;cmovl %eax, %eax;
# 0 "" 2
  movl  %eax, -4(%ebp)
  movl  -4(%ebp), %eax

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
View this message in context: http://www.nabble.com/gcc-and-asm-keyword-tp25530602p25530602.html
Sent from the gcc - Help mailing list archive at Nabble.com.

More information about the Gcc-help mailing list