Fibonacci sequence: Difference between revisions
Content added Content deleted
imported>Arakov |
imported>KayproKid m (→{{header|8080 Assembly}}: made 2nd example start from 0 instead of 1) |
||
Line 251: | Line 251: | ||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
bdos equ 5 ; BDOS entry |
bdos equ 5 ; BDOS entry |
||
cr |
cr equ 13 ; ASCII carriage return |
||
lf |
lf equ 10 ; ASCII line feed |
||
space equ |
space equ 32 ; ASCII space char |
||
conout equ 2 ; BDOS console output function |
conout equ 2 ; BDOS console output function |
||
putstr equ |
putstr equ 9 ; BDOS print string function |
||
nterms equ 20 ; number of terms (max=24) to display |
|||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
; |
; main code begins here |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
org |
org 100h |
||
lxi |
lxi h,0 ; save CP/M's stack |
||
dad |
dad sp |
||
shld oldstk |
shld oldstk |
||
lxi |
lxi sp,stack ; set our own stack |
||
lxi |
lxi d,signon ; print signon message |
||
mvi |
mvi c,putstr |
||
call bdos |
call bdos |
||
mvi a, |
mvi a,0 ; start with Fib(0) |
||
mloop: push a ; save our count |
mloop: push a ; save our count |
||
call fib |
call fib |
||
call putdec |
call putdec |
||
mvi |
mvi a,space ; separate the numbers |
||
call putchr |
call putchr |
||
pop |
pop a ; get our count back |
||
inr |
inr a ; increment it |
||
cpi |
cpi nterms+1 ; have we reached our limit? |
||
jnz |
jnz mloop ; no, keep going |
||
lhld oldstk ; |
lhld oldstk ; all done; get CP/M's stack back |
||
sphl ; restore it |
sphl ; restore it |
||
ret |
ret ; back to command processor |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
; calculate nth Fibonacci number |
; calculate nth Fibonacci number (max n = 24) |
||
; entry: A = n |
; entry: A = n |
||
; exit: HL = Fib(n) |
; exit: HL = Fib(n) |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
fib: mov c,a ; C holds n |
fib: mov c,a ; C holds n |
||
lxi d,0 ; |
lxi d,0 ; DE holds Fib(n-2) |
||
lxi h,1 ; |
lxi h,1 ; HL holds Fib(n-1) |
||
ana a ; Fib(0) is a special case |
|||
jnz fib2 ; n > 0 so calculate normally |
|||
xchg ; otherwise return with HL=0 |
|||
ret |
|||
fib2: dcr c |
fib2: dcr c |
||
jz fib3 ; we're done |
jz fib3 ; we're done |
||
push h ; save Fib(n-1) |
push h ; save Fib(n-1) |
||
dad d ; HL |
dad d ; HL = Fib(n), soon to be Fib(n-1) |
||
pop d ; |
pop d ; DE = old F(n-1), now Fib(n-2) |
||
jmp fib2 ; |
jmp fib2 ; ready for next pass |
||
fib3: ret |
fib3: ret |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
; console output of char in A register |
; console output of char in A register |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
putchr: |
putchr: push h |
||
push h |
|||
push d |
push d |
||
push b |
push b |
||
mov |
mov e,a |
||
mvi |
mvi c,conout |
||
call bdos |
call bdos |
||
pop b |
pop b |
||
Line 316: | Line 320: | ||
push d |
push d |
||
push h |
push h |
||
lxi |
lxi b,-10 |
||
lxi |
lxi d,-1 |
||
putdec2: |
putdec2: |
||
dad |
dad b |
||
inx |
inx d |
||
jc |
jc putdec2 |
||
lxi |
lxi b,10 |
||
dad |
dad b |
||
xchg |
xchg |
||
mov |
mov a,h |
||
ora |
ora l |
||
cnz |
cnz putdec ; recursive call |
||
mov |
mov a,e |
||
adi |
adi '0' |
||
call putchr |
call putchr |
||
pop h |
pop h |
||
Line 336: | Line 340: | ||
ret |
ret |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
; |
; data area |
||
;------------------------------------------------------- |
;------------------------------------------------------- |
||
signon: db ' |
signon: db 'Fibonacci number sequence:',cr,lf,'$' |
||
oldstk: dw 0 |
oldstk: dw 0 |
||
stack equ $+128 ; 64-level stack |
stack equ $+128 ; 64-level stack |
||
Line 346: | Line 350: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Fibonacci number sequence: |
|||
First 20 Fibonacci numbers: |
|||
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 |
||
</pre> |
</pre> |
||