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

herge

 Hi All:

Here is a Fibonacci Program.
This is why we spent so much time
on Odd & Even.


; FIB.ASM Friday, October 17, 2008 9:59 AM 
; By Herge
  include \masm32\include\masm32rt.inc
.data
last dd 0
lbut dd 1
next dd 0
X    dd 0
I    dd 6
J    dd 7
buffer db 20 dup(32)
       db 0
space  db 32,0
CrLf   db 13, 10 ,0
Fib  db 9,9,9,"The Fibonacci Series",13,10,0
.code
FibX proc
     mov eax, last
     mov ebx, lbut
     add eax, ebx
     mov next, eax
     mov eax, last
     mov lbut, eax
     mov eax, next
     mov last, eax
     ret
FibX endp
printEbx proc
     push ebx
     push ecx
     push edi
     invoke crt__ultoa, next, offset buffer, 10
     mov esi, offset buffer
@@:     
      mov al, byte ptr [esi]
      inc esi
      and al, al
      jnz @B
      sub esi, offset buffer+1
      mov ecx, 10
      sub ecx, esi
      mov ebx, X
      test ebx, 1
      jz Left
@@:; Odd - Right Justify     
      push ecx
      invoke StdOut, offset space
      pop ecx
      dec ecx
      jnz @B
      invoke StdOut, offset buffer
      invoke StdOut, offset space
      pop edi
      pop ecx
      pop ebx
      ret
Left:; Even - Left Justity
      push ecx
      invoke StdOut, offset buffer
      pop ecx
@@:
      push ecx      
      invoke StdOut, offset space
      pop ecx
      dec ecx
      jnz @B
      invoke StdOut, offset space
      pop edi
      pop ecx
      pop ebx
      ret
printEbx endp
Start:
      invoke StdOut, offset Fib
NextX:
      inc X 
      call FibX
      call printEbx
      dec I
      jnz NextX
      mov I, 6
      invoke StdOut, offset CrLf
      dec J
      jnz NextX
      inkey
      exit
      end Start


Regards herge

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

herge

 Hi All:

Same Program in Borland C++ ie bcc55 compiler the free
one.


/* Herge FIB.CPP  Thursday, October 16, 2008 2:33 AM
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;
int FibX( int n )
    {
    static int last = 0;
    static int LastBut_1 = 1;
    long next = last + LastBut_1;
    LastBut_1 = last;
    last = next;
    return last;
}
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) << FibX(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

Roger

Hi Herge,

Are you making mountains out of molehills?

Regards Roger

herge


Hi Roger:

thanks!

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

Roger

Hi Herge,

It occurs to me that if you used hex your mountains would  be taller whereas if you used octal they would not be so tall. Binary would never make more than molehills.

Regards Roger

herge


Hi Roger:

Octal is not going to work you need 12 octal digits and the
program is designed for ten digits.

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

herge

Hi Roger:


The Fibonacci Series Octal
           1 1                       2 3                       5 10           
          15 25                     42 67                    131 220         
         351 571                  1142 1733                 3075 5030         
       10125 15155               25302 42457               67761 132440       
      222421 355061             577502 1154563           1754265 3131050     
     5105335 10236405         15343742 25602347         43146311 70750660     
   134117171 225070051       361207242 606277313      1167506555 1776006070   
Press any key to continue ...


You have to change the width from ten to twelve and
make sure ecx is NOT zero or negative otherwise
ecxSpace is going to run away ie Crash.

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

TASMUser

Why this effort?
I made a fibonacci algorithm in BorlandPascal/ASM about ten years ago:
function fibasm(n:word):extended;assembler;
asm mov cx,n
fld1;jcxz @@e
fldz
@@1:    fadd st(1),st;fxch
loop @@1
fstp st(1)
@@e:
end;

herge

Hi TASMUser:

I am getting a FPU stack overflow!
Also am not getting any fibonacci numbers.

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

DoomyD

Hi herge, here's another variation for fibX:fibX   proc   n
      xor   eax,eax
      lea   ecx,[eax+1]
      xchg   edx,n
      .repeat
         dec   edx
         js    @F
         xadd  ecx,eax
      .until overflow?
      or      eax,0FFFFFFFFh
      @@:
      xchg   edx,n
   ret
fibX endp

herge

 Hi DoomyD:

                        The Fibonacci Series
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
4294967295 4294967295 4294967295 4294967295 4294967295 4294967295
Press any key to continue ...


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

GregL

herge,

DoomyD's code works for me.




DoomyD

Hi herge,
I implemented my procedure in purpose of being independent; adding the following lines to your code should resolve this issue:
(inc X)
push  X
call  fibX
mov   next,eax
(call printEbx)

herge

 Hi:



; FIBT.ASM Friday, November 14, 2008 3:48 PM 
; By Herge
  include \masm32\include\masm32rt.inc
  radix equ 10
.data
last dd 0
lbut dd 1
I    dd 6
J    dd 7
X    dd 0
next dd 0
PrtStk dd buffer
       dd Space
       dd 0
buffer db 20 dup(32)
       db 0
Space  db 32,0
CrLf   db 13, 10 ,0
Fib  db 9,9,9,"The Fibonacci Series",13,10,0
.code
PrtMsZ proc; SI > Table of String Addresses
     mov ebx, dword ptr[esi]
     and ebx, ebx
     jz PrtMsZX
     invoke StdOut, ebx
     add esi,4
     jmp PrtMsZ
PrtMsZX:     
     ret
PrtMsZ endp
fibX   proc n
      xor   eax,eax
      lea   ecx,[eax+1]
      xchg   edx, n
      .repeat
         dec   edx
         js    @F
         xadd  ecx,eax
      .until overflow?
      or      eax,0FFFFFFFFh
      @@:
      xchg   edx,n
   ret
fibX endp
ecxSpace proc
     and ecx, ecx
     jz ecxSpaceX     
     push ecx
     invoke StdOut, offset Space
     pop ecx
     dec ecx
     jnz ecxSpace
ecxSpaceX:
     ret
ecxSpace endp
printNex proc uses ebx ecx edi
     invoke crt__ultoa, next, offset buffer, radix
     mov esi, offset buffer
@@:     
     mov al, byte ptr [esi]
     inc esi
     and al, al
     jnz @B
     sub esi, offset buffer+1
     mov ecx, 10 ; width
     sub ecx, esi
     and X, 1 ; Is it Odd
     jz Left
     call ecxSpace ; Odd - Right Justify
     mov esi, offset PrtStk
     call PrtMsZ
     ret
Left:; Even - Left Justity
     push ecx
     invoke StdOut, offset buffer
     pop ecx
     inc ecx
     call ecxSpace
     ret
printNex endp
Start:
     invoke StdOut, offset Fib
NextX:
     inc X
     push X
     call fibX
     mov next, eax
     call printNex
     dec I
     jnz NextX
     mov I, 6
     invoke StdOut, offset CrLf
     dec J
     jnz NextX
     inkey
     exit
     end Start



It is not working. I am not sure what fibx is
doing.

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

GregL

#14
TASMUser's code converted to MASM.


.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

INCLUDE \masm32\include\windows.inc

INCLUDE \masm32\include\kernel32.inc
;INCLUDE \masm32\include\user32.inc
INCLUDE \masm32\include\msvcrt.inc
INCLUDE \masm32\include\masm32.inc

INCLUDE \masm32\macros\macros.asm

INCLUDELIB \masm32\lib\kernel32.lib
;INCLUDELIB \masm32\lib\user32.lib
INCLUDELIB \masm32\lib\msvcrt.lib
INCLUDELIB \masm32\lib\masm32.lib

.DATA

    x DWORD 46

.CODE

Fibonacci PROC n:DWORD
    ; max n = 46
    ; else overflows DWORD
    LOCAL tmp:DWORD
    .IF n == 0
        mov eax, 0
    .ELSEIF (n == 1) || (n == 2)
        mov eax, 1
    .ELSE
        mov ecx, n
        fld1
        fldz
      @@:
        fadd st(1),st(0)
        fxch
        loop @B
        fistp tmp
        fstp st(0)
        mov eax, tmp
    .ENDIF
    ret
Fibonacci ENDP

  start:

    INVOKE Fibonacci, x
    INVOKE crt_printf, chr$("Fibonacci number %d = %d",13,10),  x, eax

    inkey chr$(13,10,"Press any key to exit ... ")
    print chr$(13,10)

    INVOKE ExitProcess, 0

END start


[edit] Removed the redundant jumps. It's funny how you can come back and look at some code later on and the things that aren't right just jump right out at you. It pays to go away and do something else for a while and then come back and look at the code later.