6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 10:26 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Oct 14, 2011 5:18 am 
Offline

Joined: Fri Oct 14, 2011 5:15 am
Posts: 7
Hi, I started today learning more about 6502 and I've just write this code to calculate a Nth fibonacci number (limited to the processor's capacity).

Could you please take a look and tell me if I'm doing it right?

Code:
; 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)


Thanks,

Pedro.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 14, 2011 7:59 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8543
Location: Southern California
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Oct 14, 2011 10:54 am 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
Hmm I would have gone with...
Code:
 LDY #7    ; Load required number
 LDA #0    ; Clear last value
 STA LAST
 LDA #1    ; Set initial increment
Loop:
 TAX       ; Save current value
 CLC       ; Compute the next one
 ADC LAST
 STX LAST  ; Save new last value
 DEY
 BNE Loop
 NOP       ; Yth value is now in A

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: