Shop OBEX P1 Docs P2 Docs Learn Events
Nibble-swap Pasm2 code — Parallax Forums

Nibble-swap Pasm2 code

evanhevanh Posts: 16,032
edited 2024-07-13 14:16 in PASM2/Spin2 (P2)

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

  • Wuerfel_21Wuerfel_21 Posts: 5,107
    edited 2024-07-13 15:06

    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.

  • evanhevanh Posts: 16,032

    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.

  • evanhevanh Posts: 16,032

    Handy tool! Thanks Gary. I was struggling visualising the actions of those instructions too.

  • ElectrodudeElectrodude Posts: 1,660
    edited 2024-07-17 12:17

    Shave off the implicit AUGS:

            mergeb  val
            rol     val, #16
            splitb  val
    
  • There is no AUGS there, #%%1032 Is a regular 9 bit immediate

  • @Wuerfel_21 said:
    There is no AUGS there, #%%1032 Is a regular 9 bit immediate

    Sorry, I can't read.

  • evanhevanh Posts: 16,032
    edited 2024-07-17 12:34

    @Electrodude said:

            mergeb  val
            rol     val, #16
            splitb  val
    

    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.

Sign In or Register to comment.