format ELF section ".text" executable public poly align 4 poly: a equ dword [ebp+8] b equ dword [ebp+12] c equ dword [ebp+16] x equ dword [ebp+20] ;a*x*x+b*x+c push ebp mov ebp , esp fld c fld x fld b fld x fld a fmulp st1 , st0 faddp st1 , st0 fmulp st1 , st0 faddp st1 , st0 pop ebp ret
For calculating polynomial used polish notation Wiki
In other words a*x*x+b*x+c to reduce operations changed to (a*x+b)*x+c and then writed out operation by prioreties [*,+,*,+].
Compiling this with lines
fasm poly.asm poly.o
#include <stdio.h>
extern float poly( float , float , float , float );
int main()
{
float res = poly( 1.0 , 2.0 , 3.0 , 3.0 );
printf( "%f\n" , res );
return 0;
}
Compiling this with lines
gcc -c main.c -o main.o
Combining
gcc main.o poly.o -o main
Update on 06.12.2009
After running dome C code with FPU calculations and -O2 flag
format ELF section ".text" executable public poly align 4 poly: a equ dword [ebp+8] b equ dword [ebp+12] c equ dword [ebp+16] x equ dword [ebp+20] ;a*x*x+b*x+c push ebp mov ebp , esp fld a fmul x fadd b fmul x fadd c pop ebp ret
Now only 5 instructions