Shop OBEX P1 Docs P2 Docs Learn Events
Change Endianness of a Word in SPIN2 — Parallax Forums

Change Endianness of a Word in SPIN2

Is there a way to change the endianness of a WORD in SPIN2?
I do not believe that a ROR= 8 will work, because I suspect it would shift though the adjacent WORD.

Should I just load the information into a LONG, perform the appropriate ROR and forget about trying to save memory?

Comments

  • No such thing happens - all values are computed in 32 bit, if you have a word variable, that just means that it only stores the bottom half of values stored into it.

    But just RORing is not what you want in such a case. What you want is some equivalent of the MOVBYTS instruction. Inexplicably, that doesn't exist, so you need to create a local variable and do something like

    org
    movbyts value,#%%3201 ' word-size endian swap
    end
    

    Flexspin also has an intrinsic: value := __builtin_bswap16(value)

  • Works fine if you combine a rol and ror:

    value := value rol 8 | value ror 8
    
  • ke4pjwke4pjw Posts: 1,155

    @ChrisGadd said:
    Works fine if you combine a rol and ror:

    value := value rol 8 | value ror 8
    

    That worked well. Would be nice if there was a hub version of movbyts or and spin2 equivalent.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2024-02-16 15:58

    That worked well. Would be nice if there was a hub version of movbyts or and spin2 equivalent.

    I'd like that, too. Until it happens (how about it, @cgracey?) you can encapsulate it in a method (assuming you're not compiling Spin2 [where I think you can just do it inline])

    pub move_bytes(value, arrangment) : result
    
      org
                            mov       result, value
                            movbyts   result, arrangment
      end
    
Sign In or Register to comment.