Monday, November 24, 2014

Understanding 6502 assembly on the Commodore 64 - (14) Space and cycle optimization

With the C64 and other 8 bit computers, limited in speed and storage space, it important to optimize code to be a small and as fast as possible.  While our simple binary conversion requires neither, its good to see how much we can reduce the size of the code and reduce the amount of cycles required to execute it.  I did not include every single possible optimization the world has to offer, so do not contact me with your idea.  This is primarily about optimizing there flow of the program, and not about   optimization through ways which beginning 6502 programmers might be confused.  


Click here for Chapter 14

2 comments:

  1. I love 6502 optimizations. It's been 20 years since I've coded in 6502, but here's my version that avoids memory reading/writing. I didn't run the simulator, but I calculate the timing by hand to be around 163 cycles.

    ; 163 cycles

    ourhexvalue = #$55
    bit7 = $708

    init:

    lda ourhexvalue ; 2
    ldx #7 ; 2

    conversion:

    tay ; 2*8
    and #1 ; 2*8
    adc #$30 ; 2*8
    sta bit7,x ; 5*8
    tya ; 2*8
    lsr a ; 2*8
    dex ; 2*8
    bpl conversion ; 3*7 + 2

    brk

    ReplyDelete
  2. Whoops... this won't quite work as I forgot a CLC before the ADC... but! Simply change the ADC to ORA and it will work and still be the same cycle time.

    ReplyDelete