Fibonacci in ASM : Optimized

Share your advanced PureBasic knowledge/code with the community.
michaeled314
Enthusiast
Enthusiast
Posts: 340
Joined: Tue Apr 24, 2007 11:14 pm

Fibonacci in ASM : Optimized

Post 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
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

What is it supposed to do?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post 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
irc://irc.freenode.org/#purebasic
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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...
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
Post Reply