Flipping a Byte

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Flipping a Byte

Post by chysn »

Is this the best-possible subroutine for flipping a single byte? By "flip" I mean that ABCDEFGH becomes HGFEDCBA and by "best" I mean the smallest-possible code:

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
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Flipping a Byte

Post by tlr »

How about:

Code: Select all

; Flip A and return flipped value in A
Flip:   sta ZP
        lda #$01
-loop:  lsr ZP
        rol a
        bcc loop
        rts
This is 1 byte shorter, 16 cycles faster, and doesn't touch the X-register.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Flipping a Byte

Post by chysn »

tlr wrote: Sun Feb 14, 2021 2:53 am This is 1 byte shorter, 16 cycles faster, and doesn't touch the X-register.
That’s awesome, thank you!

And this technique, which is new for me, goes beyond this project; it can be used to constrain accumulator operations for lots of things.
groepaz
Vic 20 Scientist
Posts: 1188
Joined: Wed Aug 25, 2010 5:30 pm

Re: Flipping a Byte

Post by groepaz »

Wasnt there a compo with this theme a while ago.... on F64 perhaps? mmmmh
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
tlr
Vic 20 Nerd
Posts: 567
Joined: Mon Oct 04, 2004 10:53 am

Re: Flipping a Byte

Post by tlr »

chysn wrote: Sun Feb 14, 2021 6:14 am
tlr wrote: Sun Feb 14, 2021 2:53 am This is 1 byte shorter, 16 cycles faster, and doesn't touch the X-register.
That’s awesome, thank you!

And this technique, which is new for me, goes beyond this project; it can be used to constrain accumulator operations for lots of things.
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
This is two cycles slower than the previous version but leaves the result in ZP so the lda can be omitted in cases it would be stored there anyway.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Flipping a Byte

Post by chysn »

tlr wrote: Sun Feb 14, 2021 7:56 am This is two cycles slower than the previous version but leaves the result in ZP so the lda can be omitted in cases it would be stored there anyway.
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.
groepaz wrote: Sun Feb 14, 2021 7:26 am Wasnt there a compo with this theme a while ago.... on F64 perhaps? mmmmh
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.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
malcontent
Vic 20 Hobbyist
Posts: 129
Joined: Sun Dec 26, 2010 1:51 pm

Re: Flipping a Byte

Post by malcontent »

Not exactly the same thing, but has some similar requirements but flipping an 8x8 bit matrix along the diagonal is discussed here: http://forum.6502.org/viewtopic.php?f=2&t=6412
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Flipping a Byte

Post by Mike »

If flipping a byte formed a time-critical part of preparing a display, I'd still be willing to throw a 256 byte table against it. ;)
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.
This 'guard bit' technique is also used in the BASIC interpreter while multiplying two float mantissas, see the loop $DA5E..$DA89.
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Flipping a Byte

Post by chysn »

Mike wrote: Mon Feb 15, 2021 2:24 am If flipping a byte formed a time-critical part of preparing a display, I'd still be willing to throw a 256 byte table against it. ;)
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.
Mike wrote: Mon Feb 15, 2021 2:24 am
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.
This 'guard bit' technique is also used in the BASIC interpreter while multiplying two float mantissas, see the loop $DA5E..$DA89.
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's

Code: Select all

lda #$80
-loop tay
;yada yada
tya
lsr
bne loop
versus just regular old

Code: Select all

ldy #$08
-loop ;yada yada
dey
bne loop
Edit: Well, yeah, I get it on closer examination. Carry is checked first and the accumulator isn't always thrown out like I originally thought it was.
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
Post Reply