summaryrefslogtreecommitdiff
path: root/md/writeup/assembler_calculate_polynomial.md
blob: 8d680a02b2f8b95a8408a1deaf7d9ff019d21623 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
title:Assembler calculate polynomial
keywords:assembler,c,math,polynomial

# Assembler calculate polynomial
Calculating polynomial with asm and C

```asm
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 written out operation by priorities [*,+,*,+].
Compiling this with lines

```
fasm poly.asm poly.o
```

```c
#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 

```asm
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


# Links
http://en.wikipedia.org/wiki/Polish_notation