1. As far as following inline assembly
#include <stdio.h>
int main()
{
float arg1, arg2, add, sub, mul, div ;
printf( "Enter two numbers : " );
scanf( "%f%f", &arg1, &arg2 );
/* Perform floating point Addition, Subtraction,
Multiplication & Division */
asm (
"fld %1;"
"fld %2;"
"fadd;"
"fstp %0;" : "=g" (add) : "g" (arg1), "g" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fsub;"
"fstp %0;" : "=g" (sub) : "g" (arg1), "g" (arg2)
);
asm (
"fld %1;"
"fld %2;"
"fmul;"
"fstp %0;" : "=g" (mul) : "g" (arg1), "g" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fdiv;"
"fstp %0;" : "=g" (div) : "g" (arg1), "g" (arg2)
);
printf( "%f + %f = %f\n", arg1, arg2, add );
printf( "%f - %f = %f\n", arg1, arg2, sub );
printf( "%f * %f = %f\n", arg1, arg2, mul );
printf( "%f / %f = %f\n", arg1, arg2, div );
return 0 ;
}
2. if you compile it with gcc, it will report following errors.
Error: operand type mismatch for `fld'
Error: operand type mismatch for `fld'
Warning: translating to `faddp'
Error: operand type mismatch for `fstp'
This is because that general register modifier 'g' will cause compiler choose one general register rather than
float point register, this will cause mismatch.
3. As far as following assembly
#include <stdio.h>
int main()
{
float arg1, arg2, add, sub, mul, div ;
printf( "Enter two numbers : " );
scanf( "%f%f", &arg1, &arg2 );
printf("%f, %f input...\n", arg1, arg2);
/* Perform floating point Addition, Subtraction,
Multiplication & Division */
asm (
"fld %1;"
"fld %2;"
"fadd;"
"fstp %0;" : "=m" (add) : "m" (arg1), "m" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fsub;"
"fstp %0;" : "=m" (sub) : "m" (arg1), "m" (arg2)
);
asm (
"fld %1;"
"fld %2;"
"fmul;"
"fstp %0;" : "=m" (mul) : "f" (arg1), "f" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fdiv;"
"fstp %0;" : "=m" (div) : "f" (arg1), "f" (arg2)
);
printf( "%f + %f = %f\n", arg1, arg2, add );
printf( "%f - %f = %f\n", arg1, arg2, sub );
printf( "%f * %f = %f\n", arg1, arg2, mul );
printf( "%f / %f = %f\n", arg1, arg2, div );
return 0 ;
}
4. if you compile above code with gcc,
you will compile it ok, but result output will not right when executed.
This is because compiler may choose float point register which will be overvided later.
5. if you run following codes, it will get right result.
#include <stdio.h>
int main()
{
float arg1, arg2, add, sub, mul, div ;
printf( "Enter two numbers : " );
scanf( "%f%f", &arg1, &arg2 );
printf("%f, %f input...\n", arg1, arg2);
/* Perform floating point Addition, Subtraction,
Multiplication & Division */
asm (
"fld %1;"
"fld %2;"
"fadd;"
"fstp %0;" : "=m" (add) : "m" (arg1), "m" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fsub;"
"fstp %0;" : "=m" (sub) : "m" (arg1), "m" (arg2)
);
asm (
"fld %1;"
"fld %2;"
"fmul;"
"fstp %0;" : "=m" (mul) : "m" (arg1), "m" (arg2)
);
asm (
"fld %2;"
"fld %1;"
"fdiv;"
"fstp %0;" : "=m" (div) : "m" (arg1), "m" (arg2)
);
printf( "%f + %f = %f\n", arg1, arg2, add );
printf( "%f - %f = %f\n", arg1, arg2, sub );
printf( "%f * %f = %f\n", arg1, arg2, mul );
printf( "%f / %f = %f\n", arg1, arg2, div );
return 0 ;
}
6. Appendix