RosettaCodeData/Task/Fibonacci-sequence/8080-Assembly/fibonacci-sequence-2.8080

101 lines
2.5 KiB
Text
Raw Permalink Normal View History

2023-07-09 17:42:30 -04:00
;-------------------------------------------------------
; useful equates
;-------------------------------------------------------
bdos equ 5 ; BDOS entry
2023-10-02 18:11:16 -07:00
cr equ 13 ; ASCII carriage return
lf equ 10 ; ASCII line feed
space equ 32 ; ASCII space char
2023-07-09 17:42:30 -04:00
conout equ 2 ; BDOS console output function
2023-10-02 18:11:16 -07:00
putstr equ 9 ; BDOS print string function
nterms equ 20 ; number of terms (max=24) to display
2023-07-09 17:42:30 -04:00
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
; main code begins here
2023-07-09 17:42:30 -04:00
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
org 100h
lxi h,0 ; save CP/M's stack
dad sp
2023-07-09 17:42:30 -04:00
shld oldstk
2023-10-02 18:11:16 -07:00
lxi sp,stack ; set our own stack
lxi d,signon ; print signon message
mvi c,putstr
2023-07-09 17:42:30 -04:00
call bdos
2023-10-02 18:11:16 -07:00
mvi a,0 ; start with Fib(0)
mloop: push a ; save our count
2023-07-09 17:42:30 -04:00
call fib
call putdec
2023-10-02 18:11:16 -07:00
mvi a,space ; separate the numbers
2023-07-09 17:42:30 -04:00
call putchr
2023-10-02 18:11:16 -07:00
pop a ; get our count back
inr a ; increment it
cpi nterms+1 ; have we reached our limit?
jnz mloop ; no, keep going
lhld oldstk ; all done; get CP/M's stack back
2023-07-09 17:42:30 -04:00
sphl ; restore it
2023-10-02 18:11:16 -07:00
ret ; back to command processor
2023-07-09 17:42:30 -04:00
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
; calculate nth Fibonacci number (max n = 24)
2023-07-09 17:42:30 -04:00
; entry: A = n
; exit: HL = Fib(n)
;-------------------------------------------------------
fib: mov c,a ; C holds n
2023-10-02 18:11:16 -07:00
lxi d,0 ; DE holds Fib(n-2)
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
2023-07-09 17:42:30 -04:00
fib2: dcr c
jz fib3 ; we're done
push h ; save Fib(n-1)
2023-10-02 18:11:16 -07:00
dad d ; HL = Fib(n), soon to be Fib(n-1)
pop d ; DE = old F(n-1), now Fib(n-2)
jmp fib2 ; ready for next pass
2023-07-09 17:42:30 -04:00
fib3: ret
;-------------------------------------------------------
; console output of char in A register
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
putchr: push h
2023-07-09 17:42:30 -04:00
push d
push b
2023-10-02 18:11:16 -07:00
mov e,a
mvi c,conout
2023-07-09 17:42:30 -04:00
call bdos
pop b
pop d
pop h
ret
;---------------------------------------------------------
; Output decimal number to console
; HL holds 16-bit unsigned binary number to print
;---------------------------------------------------------
putdec: push b
push d
push h
2023-10-02 18:11:16 -07:00
lxi b,-10
lxi d,-1
2023-07-09 17:42:30 -04:00
putdec2:
2023-10-02 18:11:16 -07:00
dad b
inx d
jc putdec2
lxi b,10
dad b
2023-07-09 17:42:30 -04:00
xchg
2023-10-02 18:11:16 -07:00
mov a,h
ora l
cnz putdec ; recursive call
mov a,e
adi '0'
2023-07-09 17:42:30 -04:00
call putchr
pop h
pop d
pop b
ret
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
; data area
2023-07-09 17:42:30 -04:00
;-------------------------------------------------------
2023-10-02 18:11:16 -07:00
signon: db 'Fibonacci number sequence:',cr,lf,'$'
2023-07-09 17:42:30 -04:00
oldstk: dw 0
stack equ $+128 ; 64-level stack
;
end