Perform several operations with interface between assembler and C

1

I am trying to perform different operations with assember for the FPU unit of the Intel processors, in the middle of an interface with C.

The operations to be carried out are:

43.001 * 0.00751
0.00000001 * 1.4142135623730951
0.1 + 0.1 + 0.1 − 0.3

/* Este es el código de prueba: */

#include <stdio.h>

int main(void) 
{
double r;
double s;
const double a = 1;
const double b = 0.1;
const double c = 43.001; 
const double d = 0.00751;
const double e = 0.00000001; 
const double f = 1.4142135623730951;	
const double g = 0.3;	

__asm__ ("fldl %1;" //cargo a
		"fldl %2;"  //cargo b
		"fldl %3;"  //cargo c
		"fldl %4;"  //cargo d
		"faddp;"   // suma a con b y lo guarda en st(0)
		"fmul st2,st3" // c*d y lo guarda en st(2)
		"fstl %0;" : "=m" (r) : "m" (a), "m" (b)
		"fstl %1;" : "=m" (s) : "m" (c), "m" (d)   
		);

printf("%.16e\n", r);
printf("%.16e\n", s);	
return 0;
}

The idea is that the result of each operation is stored in a stack position, but I have not been able to do it because I'm not sure if the syntax of for example fmul st2, st3 is correct. How do you store each operation in a stack position and then print your results?

    
asked by jsuarezbaron 19.10.2018 в 07:40
source

1 answer

0

I think I solved the problem.

I wrote the following code to perform the operations:

#include <stdio.h>

int main(void)
{
double result_mult1;
double result_mult2;
double result_resta;
const double a = 43.001;
const double b = 0.00751;
const double c = 0.00000001; 
const double d = 1.4142135623730951;
const double e = 0.1;
const double f = e+e+e;	
const double g = 0.3;

__asm__ ("fldl %1;" //Carga a
		 "fldl %2;"	//Carga b
		 "fmulp;"
		 "fstl %0;" : "=m" (result_mult1) : "m" (a), "m" (b) );
		 
__asm__ ("fldl %1;" //Carga c
		 "fldl %2;" //Carga d
		 "fmulp;"
		 "fstl %0;" : "=m" (result_mult2) : "m" (c), "m" (d) );
		 
__asm__("fldl %1;" //Carga g
		"fldl %2;" //Carga f
		"fsubp;"   //Resta g con f y lo guarda en st(0)
		"fstl %0;" : "=m" (result_resta) : "m" (g), "m" (f));	

printf("%.16e\n", result_mult1);
printf("%.16e\n", result_mult2);
printf("%.16e\n", result_resta);
return 0;
}
    
answered by 21.10.2018 / 18:10
source