News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Fibonacci Series

Started by herge, October 17, 2008, 02:18:19 PM

Previous topic - Next topic

DoomyD

herge, the implementation I offered was only tested with the original code posted in the first post.
As for what my code does - It returns the N'th number of the fibonacci series (n is passed as parameter):
fibX   proc n
      xor   eax,eax       ;eax = 0 (n = 0)
      lea   ecx,[eax+1]   ;ecx = 1 (n = 1)
      xchg   edx, n       ;edx = target n
      .repeat
         dec   edx        ;n--
         js    @F         ;break if (n < 0)
         xadd  ecx,eax    ;eax <-> ecx, ecx <- ecx+eax
      .until overflow?
      or      eax,0FFFFFFFFh ;if the n'th number is too large (larger than 32 bits), return -1
      @@:
      xchg   edx,n
   ret
fibX endp

herge


Hi Greg:
Fibonacci number 0 = 0
Fibonacci number 1 = 1
Fibonacci number 2 = 1
Fibonacci number 3 = 2
Fibonacci number 4 = 3
Fibonacci number 5 = 5
Fibonacci number 6 = 8
Fibonacci number 7 = 13
Fibonacci number 8 = 21
Fibonacci number 9 = 34
Fibonacci number 10 = 55
Fibonacci number 11 = 89
Fibonacci number 12 = 144
Fibonacci number 13 = 233
Fibonacci number 14 = 377
Fibonacci number 15 = 610
Fibonacci number 16 = 987
Fibonacci number 17 = 1597
Fibonacci number 18 = 2584
Fibonacci number 19 = 4181
Fibonacci number 20 = 6765
Fibonacci number 21 = 10946
Fibonacci number 22 = 17711
Fibonacci number 23 = 28657
Fibonacci number 24 = 46368
Fibonacci number 25 = 75025
Fibonacci number 26 = 121393
Fibonacci number 27 = 196418
Fibonacci number 28 = 317811
Fibonacci number 29 = 514229
Fibonacci number 30 = 832040
Fibonacci number 31 = 1346269
Fibonacci number 32 = 2178309
Fibonacci number 33 = 3524578
Fibonacci number 34 = 5702887
Fibonacci number 35 = 9227465
Fibonacci number 36 = 14930352
Fibonacci number 37 = 24157817
Fibonacci number 38 = 39088169
Fibonacci number 39 = 63245986
Fibonacci number 40 = 102334155

Press any key to exit ...

We have lift off!

Don't use recursive methods to find fib numbers,
you can actual see the numbers print!


Thank you Greg.

Regards herge.
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

herge

 Hi All:
a slow c program uses recursion to find fibonacci
numbers. A wee bit slow!

/* FIBR.CPP Tuesday, November 18, 2008 9:11 AM 
Herge Belgian Cartoonist of Tintin and Snowy
aka Georges Remi, born Brussels, Belgium
May 22, 1907 - March 3, 1983 */
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
using std::cout;
using std::endl;
unsigned int FibR ( unsigned int n )
    {
    if ( n < 2 ) return n;
     else
    return FibR ( n - 1 ) + FibR ( n - 2 );
}
int main ( int )
    {
    cout << dec << "\t\t\tThe Fibonacci Series\n";
    int X = 0;
    for ( int i = 0; i < 7; i ++) { /* row*/
     for ( int j = 0; j < 6; j++, X++) { /* column */
      if ( ( j & 1 ) == 1 ) cout << left;
       else cout << right;
      cout << setw ( 10 ) << FibR ( X ) << " ";
   }
     if ( i == 6 ) break; /* last row ? : Exit */
     cout << endl;
}
    getch(); // Pause Wait for Keyboard
    return 0;
}


Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

herge

 Hi  DoomyD:


fibX   proc n
      xor   eax,eax
      lea   ecx,[eax+1]
      xchg   edx, n
      dec edx
      js fibX_z
      .repeat
         dec   edx
         js    @F
         xadd  ecx,eax
      .until overflow?
      or      eax,0FFFFFFFFh
@@:
      xchg   edx, n
      ret
fibX_z:
      mov ecx, 0
      ret     
fibX endp




C:\masm32\bin>fibt
                        The Fibonacci Series
         0 1                   1 2                   3 5
         8 13                 21 34                 55 89
       144 233               377 610               987 1597
      2584 4181             6765 10946           17711 28657
     46368 75025          121393 196418         317811 514229
    832040 1346269       2178309 3524578       5702887 9227465
  14930352 24157817     39088169 63245986    102334155 165580141
Press any key to continue ...


Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

DoomyD

Hi,
I'm glad to see it works. :U
Regarding the new code: your main code treats 1 as the first number number of the series, hence I had to replace:
dec   edx
jz    @F ;original
xadd  ecx,eax
withdec   edx
js    @F ;current
xadd  ecx,eax
To make it fit to the output you originally posted; changing it back should result with the current output.

~DoomyD

herge


Hi DoomyD:

It was only very recently I went to Wikipedia and
discovered that zero was the first number in the
Fibonacci seies.

A more complicated way of finding the Fibonacci number
with out adding them up and comparison to normal way.


/* CFIB.CPP  Monday, November 17, 2008 8:03 AM */
#include<iostream>
#include<conio>
using std::cin;
using std::cout;
using std::endl;
int FibX( int n )
    {
    static int LastBut_1 = 1;
    static int last = 0;
    if ( n == 0 ) return n;
    int next = last + LastBut_1;
    LastBut_1 = last;
    last = next;
    return last;
    }
int main( int )
{
    int n = 0;
    double S5 = sqrt ( 5.0 );
    double PH = 1.618034;
    double PHi = -0.618034;
    cout << " Calculate Fibonacci(n) \n";
    for ( n = 0; n < 42; n++)
    {
    double FN = ( pow ( PH, n ) - pow ( PHi, n) ) / S5 + .5;
    int ifn =  FN;
    cout << " " << ifn << " " << FibX ( n ) << "\t";
    }
    getch;
    return 0;
    }
   


Regards herge
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

herge

 Hi Mark Jones:

Can You click a mouse , do you like abstract art?
You too can be an abstract artist goto
http://www.jacksonpollock.org/
If you can click a mouse you too can be an artist
You have to move the mouse and click.
Enjoy!

Reagrds herge.
// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy