Skip to content

Instantly share code, notes, and snippets.

@pedrofranceschi
Created October 14, 2011 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrofranceschi/1285964 to your computer and use it in GitHub Desktop.
Save pedrofranceschi/1285964 to your computer and use it in GitHub Desktop.
Fibonacci calculation in 6502 processor assembly
; Fibonacci calculator in 6502 asm
; by Pedro Franceschi (pedrohfranceschi@gmail.com)
; test it in http://www.6502asm.com/beta/index.html
; the accumulator in the end will hold the Nth fibonacci number
LDX #$01; x = 1
STX $00; stores x
SEC; clean carry;
LDY #$07; calculates 7th fibonacci number (13 = D in hex) (CHANGE HERE IF YOU WANT TO CALCULATE ANOTHER NUMBER)
TYA; transfer y register to accumulator
SBC #$03; handles the algorithm iteration counting
TAY; transfer the accumulator to the y register
CLC; clean carry
LDA #$02; a = 2
STA $01; stores a
loop: LDX $01; x = a
ADC $00; a += x
STA $01; stores a
STX $00; stores x
DEY; y -= 1
BNE loop; jumps back to loop if Z bit != 0 (y's decremention isn't zero yet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment