User Tools

Site Tools


playground:reversing_bits_in_a_byte

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
playground:reversing_bits_in_a_byte [2020-02-12 16:42]
icepic created
playground:reversing_bits_in_a_byte [2020-03-12 13:05] (current)
tlr
Line 19: Line 19:
 using X and Y for temporary storage and only short 2-cycle instructions in the loop. using X and Y for temporary storage and only short 2-cycle instructions in the loop.
 Of course a table lookup will be faster if you do this a lot in your code. Of course a table lookup will be faster if you do this a lot in your code.
 +
 +--
 +this would be equally fast, at 100cycl +rts (enter with the value in .A, result in .A, .X will be 0)
 +<​code>​
 +        ldx #8
 +loop    asl 
 +        ror $2
 +        dex
 +        bne loop
 +        lda $2
 +
 +        rts
 +</​code>​
 +
 +or unrolled (56 cycl +rts):
 +<​code>​
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        ror $2
 +        asl 
 +        lda $2
 +        ror
 +        rts
 +</​code>​
 +
 +--
 +Optimized version, at 84cycl +rts (enter with the value in .A, result in .A)
 +<​code>​
 + sta $02 ; 3
 + lda #1 ; 2
 +lp:
 + ror $02 ; 5
 + rol ; 2
 + bcc lp ; 3(2)
 + ; =84
 + rts
 +</​code>​
 +
 +or slightly unrolled:
 +<​code>​
 + sta $02 ; 3
 + lda #1 ; 2
 +lp:
 + ror $02 ; 5
 + rol ; 2
 + ror $02 ; 5
 + rol ; 2
 + bcc lp ; 3(2)
 + ; =72
 + rts
 +</​code>​
  
playground/reversing_bits_in_a_byte.1581522177.txt.gz ยท Last modified: 2020-02-12 16:42 by icepic