Skip to content

Instantly share code, notes, and snippets.

@jeandrek
Created November 19, 2017 08:06
Show Gist options
  • Save jeandrek/c81327bdd3554ef9d93465b070f27e45 to your computer and use it in GitHub Desktop.
Save jeandrek/c81327bdd3554ef9d93465b070f27e45 to your computer and use it in GitHub Desktop.
Fibonacci for S65
@0x00
ORIGIN = 0xFF8D
==Start==
LDA #1 ; start counting at 1
{
PHA ; save A
JSR Fib
CMP 0x0200 ; if fib(A)>fib(A-1), stop
BCC BR
STA 0x0200
JSR Printn
JSR Newline
PLA ; restore A
ADC #1 ; increase A by 1
JMP RE
}
{JMP RE}
==Fib==
LDX #1
LDY #0
{
CMP #2
BCC BR
SBC #1 ; decrement A
PHA ; save A
TXA ; set A to X
STY 0x0201 ; add Y to A
CLC
ADC 0x0201
STX 0x0201 ; set Y to X
LDY 0x0201
TAX ; set X to A
PLA ; restore A
JMP RE
}
TXA
RTS
==Printn==
{
PHA ; save A
LSR A ; divide A by 10...
STA 0x0201
LSR A
ADC 0x0201
ROR A
LSR A
LSR A
ADC 0x0201
ROR A
ADC 0x0201
ROR A
LSR A
LSR A
BEQ BR ; stop if A = 0
JSR Printn ; recursively call Printn
}
PLA ; restore A
{
CMP #10 ; break if A < 10
BCC BR
SBC #10 ; subtract 10 from A
JMP RE ; and loop
}
ORA #0x30 ; ASCII code for '0'
STA 0x7c00 ; print digit
RTS
==Newline==
LDA #13
STA 0x7c00
RTS
==Irq==
RTI
@0xFFFC
DATA Start
DATA Irq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment