Shop OBEX P1 Docs P2 Docs Learn Events
How to alias word variables — Parallax Forums

How to alias word variables

phil kennyphil kenny Posts: 233
edited 2007-12-24 20:13 in BASIC Stamp
I'm familiar with using the alias feature of the Stamp Editor to
reuse variable names on a byte basis. What I'd like to know is
if this can be extended to word variables.

For instance, if I declare a 10 byte array as

Buffer VAR BYTE(10)

How can I use an alias for the first two bytes which will be a word
variable?

Temp Word Buffer(0)

where Temp is a two byte variable referring to Buffer(0) and Buffer(1).

The Help file only seems to discuss aliasing on a byte, or less, basis.

phil

Comments

  • ZootZoot Posts: 2,227
    edited 2007-12-24 19:38
    You have to do it the other way around -- you create a word variable, *then* reference it as the start of the array. By definition, any variable can be an "array" if you just start accessing it as if it were the first element in a series:

    someWordVal VAR Word ' a word
    someOtherWordVal VAR Word ' next word
    yetAnotherWordVal VAR Word ' another word
    
    wordArr VAR someWordVal ' alias to first word -- just to have a more descriptive name when using as an array
    
    ' now you can use wordArr *as if* it were the first element of a 3 byte array
    ' (actually, you can go beyond 3 words, but if you do, you may overwriting/using other variable if your var spaces are not defined)
    
    FOR i = 0 TO 2
       RANDOM wordArr(i) ' put random val into this element
    NEXT
    
    'wordArr(0) is same as someWordVal
    'wordArr(1) is same as someOtherWordVal
    'wordArr(2) is same as yetAnotherWordVal
    
    ' here is another common example for timekeeping bytes -- you can access individual elements by name, or write to the array:
    time VAR Byte
    secs VAR time  ' alias to first "element", aka time(0)
    mins VAR Byte ' aka time(1)
    hours VAR Byte ' aka time(2)
    day VAR Byte ' aka time(3)
    month VAR Byte ' aka time(4)
    year VAR Byte ' aka time(5)
    
    ' read byte stream into "array"
    I2CIN SDA,devAddr, Addr, [noparse][[/noparse] time\6 ] ' reads stream into first 6 bytes of "time"
    ' now I can reference mins by name or by index:
    DEBUG HEX2 mins, CLREOL, CR
    DEBUG HEX2 time(1), CLREOL, CR ' same thing
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    Post Edited (Zoot) : 12/24/2007 8:17:12 PM GMT
  • ZootZoot Posts: 2,227
    edited 2007-12-24 19:43
    P.S. -- if you need to mix words/bytes, then use the LOWBYTE(x) modifier on an implied word array, e.g.:

    
    firstElem VAR Word
    myArr VAR firstElem
    secElem VAR Word
    thirdElem VAR Word
    fourthElem VAR Word
    
    ' 4 word array use -- myArr(x)
    
    ' now "imply" a Byte array:
    firstByteelem VAR firstElem.LOWBYTE 
    secondByteelem VAR firstElem.HIGHBYTE 
    myByteArr VAR firstByteelem
    '
    ' myByteArr(0) eq. firstElem.LOWBYTE
    ' myByteArr(1) eq. firstElem.HIGHBYTE
    ' etc
    ' or if you need by name and not as indexed array:
    ' firstByteelem eq. firstElem.LOWBYTE
    ' secondByteelem eq. firstElem.HIGHBYTE
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • phil kennyphil kenny Posts: 233
    edited 2007-12-24 20:10
    Hi Zoot,

    Thanks.

    Both of your answers helped greatly to clarify this aliasing usage.

    I wish that the Stamp Help file had included some extended
    discussion along these lines.

    phil
  • ZootZoot Posts: 2,227
    edited 2007-12-24 20:13
    The Basic Stamp manual covers this topic, actually (not sure if it's in the help file, but the discussion on variable assignment and allocation in the Manual is pretty good regarding implied arrays). Still, it's one of those things that even with documentation, you have to wrap your head around.

    parallax.com/Portals/0/Downloads/docs/prod/stamps/web-BSM-v2.2.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
Sign In or Register to comment.