Shop OBEX P1 Docs P2 Docs Learn Events
VAR Aliases... — Parallax Forums

VAR Aliases...

DToolanDToolan Posts: 11
edited 2011-08-04 17:12 in Propeller 1
If I create a word var (or any other kind of var)... can I somehow create a byte array that resides on top of that var so I can access each byte without having to mask and shift?

Example:

Word MyWordVar
Byte MyByteArray[2] @MyWordVar '<- MyByteArray actually lives on top of the memory locations of MyWordVar

MyWordVar := $AA55

'MyByteArray[0] should equal $55 and MyByteArray[1] should equal $AA without having to mask and shift MyWordVar into seperate bytes

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-08-04 16:13
    You can access the bytes this way.
    VAR
      word MyWordVar
      byte MyByteArray[2] 
    PUB Main
     
      MyByteArray[0] := byte[@MyWordVar] 
      MyByteArray[1] := byte[@MyWordVar + 1]
     
    

    I doubt it's what you're looking for but you don't have to shift the bits.

    In this example MyByteArray[1] will hold the least significant byte (I'm pretty sure).

    Also (as I'm sure you know) MyByteArray wont change automatically when you change MyWordVar. Which, if I understand you correctly, is your main objective (which my example fails to do).

    Maybe some else has a better idea?

    Duane
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-04 16:35
    DToolan wrote: »
    If I create a word var (or any other kind of var)... can I somehow create a byte array that resides on top of that var so I can access each byte without having to mask and shift?
    That won't work for various reasons (reordering of long/word/byte etc). What you can do is access smaller units of a long like value.word[0] or value.byte[2] or of a word like value.byte[1].
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2011-08-04 16:42
    If you use the DAT section, here is one way to alias the BYTE,WORD, and LONG variables without any program overhead...

    http://forums.parallax.com/showthread.php?112970-Word-Sized-constant-in-Byte-array&p=808493&viewfull=1#post808493

    Main Reference Thread:
    http://forums.parallax.com/showthread.php?112970-Word-Sized-constant-in-Byte-array


    Pertinent information:
    DAT
    L0            long
    W0            word
    W0_lowbyte    byte      0
    W0_highbyte   byte      0
    W1            word
    W1_lowbyte    byte      0
    W1_highbyte   byte      0
    
  • DToolanDToolan Posts: 11
    edited 2011-08-04 17:12
    Thank you all very much for your information. It was very helpful.
Sign In or Register to comment.