Page 1 of 1

Fibonacci in ASM : Optimized

Posted: Mon Apr 13, 2009 9:12 pm
by michaeled314

Code: Select all

XOR eax,eax
MOV ebx,1
XOR ecx,ecx

!_1:
 INC ecx
 ADD eax,ebx
 XCHG eax,ebx
 CMP ecx,5
 JL _1

Posted: Mon Apr 13, 2009 9:32 pm
by Fluid Byte
What is it supposed to do?

Posted: Tue Apr 14, 2009 7:16 am
by Deeem2031
But this looks faster :wink:

Code: Select all

Global result
DisableDebugger

XOR eax,eax 
MOV ebx,1 
MOV ecx, 5

!_1: 
 XADD ebx, eax
 DEC ecx
 JNZ _1
 
MOV dword[v_result], ebx
 
EnableDebugger
 
Debug result

Posted: Tue Apr 14, 2009 12:16 pm
by luis
Fluid Byte wrote:What is it supposed to do?
If I understand correctly should return the n-th number in the fibonacci's serie.

For example, the first 10 numbers are:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89

if you put a 5 in

CMP ecx, 5

the result loaded in ebx will be 8 (the 5th number)

if you write

CMP ecx, 10

the result will be 89

and so on...

Posted: Tue Apr 14, 2009 1:57 pm
by Fluid Byte
Thanks luis. Didn't know Fibonacci was actually a person. :)

Here's the Wiki article: http://en.wikipedia.org/wiki/Fibonacci_number

Posted: Tue Apr 14, 2009 2:19 pm
by luis
Yup !

Another fascinating thing is the ratio of two consecutive numbers tends very quickly to the "golden ratio" http://en.wikipedia.org/wiki/Golden_ratio

2:1 = 2
3:2 = 1.5
5:3 = 1,6666666667
...
21:13 = 1,6153846154
...
89/55 = 1,6181818182

etc. etc.