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

半世苍凉 提交于 2019-11-28 11:11:28

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.

Try something like this

push $0x????????
push $0x????????
fldl (%esp)
addl $8,%esp

Where the ????????'s are replaced by the IEEE representation of the double constant. This method has the advantage that it works equally well in normal and position-independent (PIC, i.e. shared library) code.

The only valid operands for the fld instruction are memory or a floating-point stack register.

(Also, you have specified y as an input operand for the asm block, whereas it should be an output. Probably safer to constrain that to being memory ("m", rather than "g") as well.)

If you really want to do this with inline assembly:

#include <stdio.h>

int main(void)
{
   double y;
   const double k = 150.0;

   __asm__ ("fldl %1;"
            "fsqrt;"
            "fstl %0;" : "=m" (y) : "m" (k) );

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

   return 0;
}

t constraint

According to the GCC docs https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints

t

Top of 80387 floating-point stack (%st(0)).

So we can do:

#include <assert.h>

int main(void) {
    double io = 4.0;
    __asm__ (
        "fsqrt"
        : "+t" (io)
        :
        :
    );
    assert(io == 2.0);
    return 0;
}

GitHub upstream.

Refresher: + means that io will be used both as input and output.

Tested in Ubuntu 19.04.

GNU GAS ARM assembly supports it

E.g. in ARMv8:

main.c

#include <assert.h>

int main(void) {
    float my_float = 1.5;
    __asm__ (
        "fmov s0, 1.0;"
        "fadd %s[my_float], %s[my_float], s0;"
        : [my_float] "+w" (my_float)
        :
        : "s0"
    );
    assert(my_float == 2.5);
}

GitHub upstream.

Compile and run:

aarch64-linux-gnu-gcc -o main.out -static -std=gnu99 main.c
qemu-aarch64 ./main.out

The %s modifier is mentioned at: ARMv8 floating point output inline assembly

It also works on ARMv7.

However, for some reason, it only works for floating point instructions such as fmov, e.g. the following ARMv7 attempt fails to assemble:

mov r0, 1.5

with error:

Error: garbage following instruction -- `mov r0,1.5'

presumably because it uses the mov instruction, which acts on general purpose registers instead of floating point ones.

However perhaps this doesn't matter much, as for the most part you just want to do your floating operations on your floating point registers, and then do an fcmp followed by vmrs as in:

vmov s0, 1.5
vmov s1, 2.5
fadds s2, s0, s1
vmov s3, 4.0
/* Compare two floating point registers. Stores results in fpscr:
 * (floating point status and control register).
 */
vcmp.f32 s2, s3
/* Move the nzcv bits from fpscr to apsr */
vmrs apsr_nzcv, fpscr
/* This branch uses the Z bit of apsr, which was set accordingly. */
beq theyre_equal

GitHub upstream.

It never ceases to amuse me how GNU GAS has subtly different syntax for every arch!

I could not however find a hex float literal syntax: How to use hexadecimal floating point literals in GNU GAS?

Tested on Ubuntu 18.04.

You can bypass many assemblers refusing to support float literals by having PHP preprocess the literal for you. (rawSingleHex taken from here). In a perfect world C preprocessor would be enough, but that's not true at the moment.

<?php
function rawSingleHex($num) {
    return '0x' . strrev(unpack('h*', pack('f', $num))[1]);
}
?>

#include <stdio.h>

int main(int argc, char **argv) {
   float y = 0;

   __asm__ ("pushl $<?php echo rawSingleHex(150);?>\n"
            "fsqrt\n"
            "fstl %0\n" : : "g" (y));

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


   return 0;
}

run php to generate the c file, and run c compiler to compile your program :P

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!