RosettaCodeData/Task/Fibonacci-sequence/Z80-Assembly/fibonacci-sequence-2.z80

35 lines
468 B
Z80 Assembly

; 32 bit version
; IN : a = n (n <= 47, otherwise overflows)
; OUT: hlh'l' = FIB(n)
fib32: ld l,a ; lower bytes in the alt set
ld h,0
exx ; now in regular set
ld hl,0
cp 2
ret c ; if n < 2 then done
dec a ; loopcount = n - 1
ld bc,0
exx ; now in alt set
ld bc,0
ld hl,1
f32_l: ld d,h
ld e,l
add hl,bc
ld b,d
ld c,e
exx ; now in reg set
ld d,h
ld e,l
adc hl,bc
ld b,d
ld c,e
exx ; now in alt set
dec a
jr nz,f32_l
exx ; now in reg set
ret