Nibble-swap Pasm2 code
Has anyone got a smaller solution than this for performing a nibble swap?: $12345678 -> $21436587
splitb val
splitw val
movbyts val, #%01_00_11_10
mergew val
mergeb val
Has anyone got a smaller solution than this for performing a nibble swap?: $12345678 -> $21436587
splitb val
splitw val
movbyts val, #%01_00_11_10
mergew val
mergeb val
Comments
split/merge instructions are circular (5x mergew will bring you back to the same value you started with. splitw moves in the opposite direction. splitb/mergeb are just 2x splitw/mergew), so perhaps
mergeb val movbyts val, #%%1032 splitb val
Not tested, may be nonsense Yep, works.
Damn! That's cool! Thanks.
There is a need for documentation of how to perform useful operations.
This is how this p2 gem works.
1. mergeb val (Merge bits of 4 bytes into 8 nibbles.) $12345678 = 0001_0010_0011_0100_0101_0110_0111_1000 0 0 0 0 0 0 0 1 1 3 0 1 0 1 5 1 1 1 1 F 0 0 0 1 1 0 1 1 0 6 1 0 1 0 A 0 0 0 0 0
2. movbyts val,%01_00_11_10 (Swap order of 16 bit words.) 1 0 3 2 (This causes a change of nibble order in step 3.) -- -- -- -- $035F16A0 --> 16 A0 03 5F
3. splitb val (Split bits of 8 nibbles into 4 bytes.) $16A0035F = 0001_0110_1010_0000_0000_0011_0101_1111 0 0 1 0 0 0 0 1 21 0 1 0 0 0 0 1 1 43 0 1 1 0 0 1 0 1 65 1 0 0 0 0 1 1 1 87
@wuerfel_21 Great question.
@evanh Great answer.
Handy tool! Thanks Gary. I was struggling visualising the actions of those instructions too.
Shave off the implicit
AUGS
:mergeb val rol val, #16 splitb val
There is no AUGS there,
#%%1032
Is a regular 9 bit immediateSorry, I can't read.
That is more readable though. MOVBYTS always has to be read carefully to know what gets moved where.
Another readable form:
```
mergeb val rolword val, val, #1 splitb val
```
EDIT: Grr, formatting is messed. It won't clean up.