BitWise wrote:
Slightly smaller if you shuffle the instructions and put the initial value in A.
Nice, if memory economy is the goal. Of course the other potential priority is speed, which, judging from the lead post, is what Bogax was interested in. His algorithm has a very fast worst-case execution because there's
no looping.
Omegamatrix wrote:
In addition to Dr Jefyll's saving with using TAY and TYA, you can shave 1 cycle off worse case execution in the first part of the routine.
Good spot, Omegamatrix -- I missed that! The 2nd instruction tests the high 4 bits, but the test needn't be destructive as in my previous version. CMP and BIT are both capable of doing the test while at the same time preserving the contents of A. I've adopted your suggestion (below) but I prefer to use BIT since it's just as fast as CMP and IMO is slightly more readable.
Code:
HIBIT: ;previous version
lda num
and #$F0 ; <- destructive test
bne SKIP1
lda num
; [etc ]
Code:
HIBIT: ;improved version
lda num
bit #$F0 ; <- non-destructive test
beq SKIP1
and #$F0
SKIP1: ;if any of bits 7,6,5,4 is set then bits 3,2,1,0 have now been cleared
tay
and #$CC
bne SKIP2
tya
SKIP2: ;if any of bits 7,6,3,2 is set then bits 5,4,1,0 have now been cleared
tay
and #$AA
bne SKIP3
tya
SKIP3: ;if any of bits 7,5,3,1 is set then bits 6,4,2,0 have now been cleared
rts
-- Jeff
_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html