Shop OBEX P1 Docs P2 Docs Learn Events
Endian swap in less than 12 instructions? — Parallax Forums

Endian swap in less than 12 instructions?

scanlimescanlime Posts: 106
edited 2008-08-10 19:40 in Propeller 1
I need to swap the bytes in a long, for big-endian/little-endian conversion. So far, this is the best I can some up with- it's pretty much entirely naive and straightforward... "spi_reg" is the input and output, r0/r1 are temporaries, and "hFF00" is pre-initialized with $FF00. It's 12 instructions, or 3 instructions per swapped byte.

mov     r1, spi_reg
shr     r1, #24
mov     r0, spi_reg
shl     r0, #24
or      r1, r0
mov     r0, spi_reg
and     r0, hFF00
shl     r0, #8
or      r1, r0                        
shr     spi_reg, #8
and     spi_reg, hFF00
or      spi_reg, r1




Have any PASM experts out there come up with a faster way of doing the same? I thought about doing something recursive with the 'rev' instruction, but the fact that it clears the non-reversed bits kind of gets in the way.

Thanks!
--Micah

Comments

Sign In or Register to comment.