-
Notifications
You must be signed in to change notification settings - Fork 81
/
fibonacci.cpp
92 lines (87 loc) · 1.7 KB
/
fibonacci.cpp
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
//C++
#include <cstdio>
int fib( const int n )
{
if ( n <= 1 )
return n;
return fib( n - 1 ) + fib( n - 2 );
}
int main()
{
for ( auto i = 0; i < 20; ++i )
printf( "%d\n", fib( i ) );
}
//C++ e MASM X86 Fibonacci Algorithm
#include <iostream>
char format[] = "%d\n";
_declspec (naked) int fibonacci(const int n)
{
__asm
{
push ebp
mov ebp, esp
sub esp, 4
push ebx
push esi
mov ecx, 30
mov eax, 0xCCCCCCCC
cmp DWORD PTR[ebp + 8], 1
jg loop_1
mov eax, DWORD PTR [ebp+8]
jmp loop_2
loop_1:
mov eax, DWORD PTR[ebp + 8]
sub eax, 1
push eax
call fibonacci
add esp, 4
mov esi, eax
mov ecx, DWORD PTR[ebp + 8]
sub ecx, 2
push ecx
call fibonacci
add esp, 4
add eax, esi
loop_2:
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
}
}
int main()
{
_asm
{
push ebp
mov ebp, esp
sub esp, 4
push ebx
mov eax, 0xCCCCCCCC
mov DWORD PTR[ebp+8], 0
jmp loop_1
loop_3:
mov eax, DWORD PTR [ebp + 8]
add eax, 1
mov DWORD PTR[ebp+8], eax
loop_1:
cmp DWORD PTR[ebp+8], 20
je loop_2
mov eax, DWORD PTR [ebp+8]
push eax
call fibonacci
add esp, 4
push eax
push offset format
call dword ptr [printf]
add esp, 8
jmp loop_3
loop_2:
pop ebx
mov esp, ebp
pop ebp
leave
ret
}
}