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
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
Hi Herge,
Are you making mountains out of molehills?
Regards Roger
Hi Roger:
thanks!
Regards herge
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
Hi Roger:
Octal is not going to work you need 12 octal digits and the
program is designed for ten digits.
Regards 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
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;
Hi TASMUser:
I am getting a FPU stack overflow!
Also am not getting any fibonacci numbers.
Regards herge
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
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,
DoomyD's code works for me.
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)
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
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.
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
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.
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
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
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
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
Just for fun, check this out (click image for full view): http://fractalyst.deviantart.com/art/Fibonacci-Latticework-105924852
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.