Sum of two numbers in floating point by inline function asm in C

0

I am working with gcc in linux, I have an implementation of an asm inline function, which adds two numbers in floating point and returns them by reference in the variable r also passed as a parameter, compiles but the result of the sum is 0.0 , someone could help me, thanks :) here my function:

   #define suma(r, a, b)\
   __asm__("fldz \n\t fld %1\n\t fadd %2\n\t fstp %0":"=&t"(r):"f"(a),"f"(b));
    
asked by Alexmaister 12.01.2018 в 03:30
source

1 answer

2

With a couple of changes I've made it work, at least on my machine:

#define suma(r, a, b)\
  __asm__("FLD %1 \n FADD %2 \n FSTP %0 \n" : "=m"(r) : "m"(f1), "m"(f2) : );

#include <stdio.h>

int main()
{
  float f;
  float f1 = 4, f2 = 5;
  suma(f,f1,f2);
  printf("%f, %f, %f",f,f1,f2);
}

The restrictions used for the variables did not seem to be correct and you were also doing unnecessary operations: fldz

    
answered by 12.01.2018 / 15:45
source