I didn't run it, but it looks like it should work right. Just a few notes:
- BNE branches if Z flag is 0. A 0 result sets the Z flag. (Your code is right, but the "!" should not be in the comment.)
- Normally you'll want the SEC or CLC right before the operation, after the preceeding loads and transfers. It doesn't matter to the computer, but it's easier to keep it straight mentally.
- In a routine like this, you would normally want the input in A at the beginning, so then you can run it several times with different inputs without re-assembling.
- You could also start with the input in Y, and DEY 3 times instead of subtracting 3 from A and then transferring to Y. It saves a byte.
- For 9 or less, decimal and hex numbers are the same, so you don't have to specify. (This is just to make the code a little less cluttered.)
- Moving the comments farther out and lining up the semicolons also makes it easier to factor visually.
- After you get familiar with the instruction set, it will be better to not clutter the code with comments saying for example "TYA ; Transfer Y to A" (since that's what TYA says anyway).
- Instead of referencing numerical addresses (in this case, the variables), give them descriptive names at the beginning and then use the names in the code and have the assembler substitute the actual addresses.
I changed it a little like this to help me understand what it's doing:
Code:
; Enter the routine with the desired fibonacci number in A.
LDX #1
STX 0
SEC
SBC #3
TAY ; Y becomes the loop counter.
LDA #2
STA 1
CLC
; Y=4 Y=3 Y=2 Y=1
loop: LDX 1 ; x = a X=2, A=2 X=3, A=3 X=5, A=5 X=8, A=8
ADC 0 ; a += x X=2, A=3 X=3, A=5 X=5, A=8 X=8, A=13
STA 1 ;
STX 0 ;
DEY
BNE loop ; Repeat loop until Y=0.
; At the end, the result is in A.
We don't normally do people's homework for them, but in this case you already did it and asked for someone to look it over, which is fine.