How do I specify immediate floating point numbers with inline assembly?

poundifdef picture poundifdef · Jun 29, 2011 · Viewed 7.7k times · Source

When I try to compile this code:

#include <stdio.h>

main(int argc, char *argv[]) {
   double y = 0;

   __asm__ ("fldl $150;"
            "fsqrt;"
            "fstl %0;" : : "g" (y) );

   printf("%f\n", y);


   return 0;
}

I get this error:

sqrt.c: Assembler messages:
sqrt.c:6: Error: suffix or operands invalid for `fld'

Why doesn't this work? Why can't I push the number "150" onto the stack for floating point operations?

Answer

wallyk picture wallyk · Jun 29, 2011

I do not know of an assembly language which supports literal floating point constants for immediate use. The usual means is to declare initialized storage containing the floating point constant and referencing it:

const1:     dq  1.2345
...
     fldl    const1

For the example you give, it is possible to do this more directly:

printf ("%f\n", sqrt (150));

Otherwise, this must be an artificially complicated project, perhaps homework.