Code: Select all
; Flip A and return flipped value in A
Flip: sta ZP
ldx #$08
-loop: lsr ZP
rol a
dex
bne loop
rts
Moderator: Moderators
Code: Select all
; Flip A and return flipped value in A
Flip: sta ZP
ldx #$08
-loop: lsr ZP
rol a
dex
bne loop
rts
Code: Select all
; Flip A and return flipped value in A
Flip: sta ZP
lda #$01
-loop: lsr ZP
rol a
bcc loop
rts
There's also a variant of this:
Code: Select all
; Flip A and return flipped value in A
Flip: sec
rol a
-loop: ror ZP
asl a
bne loop
lda ZP
rts
In my game, It's likely that I will work in-place, probably via ROR absolute,X. Basically, the idea is to make a contiguous set of ten custom characters (80 bytes) turn the other direction. This construction lets me use X as a byte index instead of a bit count.
It would be an interesting read. I did a Google search before posting my code, and didn't find anything smaller than mine. And right now, I can't imagine anything being smaller than @tlr's.
This 'guard bit' technique is also used in the BASIC interpreter while multiplying two float mantissas, see the loop $DA5E..$DA89.chysn wrote:And this technique, which is new for me, goes beyond this project; it can be used to constrain accumulator operations for lots of things.
I see what you mean. The operation I have in mind will take .0168 seconds, which would be rough in some cases. For my project, the switch is happening between half-innings in a baseball game. It's the part of the game where the spectators get up to use the restroom and get a beer. I'll probably even have to add another second of artificial delay. If it was time-critical, I'd probably just store my flipped characters in memory.
I admire the usage here because it's totally organic. The iterator and the result are one value, and it's very elegant. I don't really understand this usage in the BASIC interpreter. It's storing the iterator accumulator in Y while the accumulator is used for other stuff, and then from Y back to accumulator to do the shift and the carry check. In other words, I don't understand why it'sMike wrote: ↑Mon Feb 15, 2021 2:24 amThis 'guard bit' technique is also used in the BASIC interpreter while multiplying two float mantissas, see the loop $DA5E..$DA89.chysn wrote:And this technique, which is new for me, goes beyond this project; it can be used to constrain accumulator operations for lots of things.
Code: Select all
lda #$80
-loop tay
;yada yada
tya
lsr
bne loop
Code: Select all
ldy #$08
-loop ;yada yada
dey
bne loop