Fibonacci sequence: Difference between revisions
Content deleted Content added
imported>Arakov |
imported>KayproKid m →{{header|8080 Assembly}}: made 2nd example start from 0 instead of 1 |
||
Line 251:
;-------------------------------------------------------
bdos equ 5 ; BDOS entry
cr
lf
space equ
conout equ 2 ; BDOS console output function
putstr equ
nterms equ 20 ; number of terms (max=24) to display
;-------------------------------------------------------
;
;-------------------------------------------------------
org
lxi
dad
shld oldstk
lxi
lxi
mvi
call bdos
mvi a,
mloop: push a ; save our count
call fib
call putdec
mvi
call putchr
pop
inr
cpi
jnz
lhld oldstk ;
sphl ; restore it
ret
;-------------------------------------------------------
; calculate nth Fibonacci number (max n = 24)
; entry: A = n
; exit: HL = Fib(n)
;-------------------------------------------------------
fib: mov c,a ; C holds n
lxi d,0 ;
lxi h,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
jz fib3 ; we're done
push h ; save Fib(n-1)
dad d ; HL
pop d ;
jmp fib2 ;
fib3: ret
;-------------------------------------------------------
; console output of char in A register
;-------------------------------------------------------
putchr: push h
push d
push b
mov
mvi
call bdos
pop b
Line 316 ⟶ 320:
push d
push h
lxi
lxi
putdec2:
dad
inx
jc
lxi
dad
xchg
mov
ora
cnz
mov
adi
call putchr
pop h
Line 336 ⟶ 340:
ret
;-------------------------------------------------------
;
;-------------------------------------------------------
signon: db '
oldstk: dw 0
stack equ $+128 ; 64-level stack
Line 346 ⟶ 350:
{{out}}
<pre>
Fibonacci number sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
</pre>
|