Shop OBEX P1 Docs P2 Docs Learn Events
Alias for an array element? — Parallax Forums

Alias for an array element?

DonDon Posts: 34
edited 2007-05-10 12:15 in BASIC Stamp
Is it possible to create an alias for an element of an array?· For example, say that I have a byte array defined thusly:

status VAR BYTE(4)


I'd like to create an alias that refers to the high nibble of the second element of the array.

dataReady VAR status(1).HIGHNIB


This, however, is not acceptable to the compiler.

I understand that I can refer to the high nibble of the second byte by using a modifier:

i = status.LOWNIB(3)


But this arcane code would need to appear wherever I need the reference instead of in one place like it would be if I could define an alias as I initially tried to.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Don Kinzer

·

Comments

  • NewzedNewzed Posts: 2,503
    edited 2004-08-10 20:44
    Does status have to be an array?· You could write

    stat1 var byte

    stat2 var byte

    stat1H var stat1.highnib

    stat1L var stat1.lownib

    Stat1H and stat1L do not consume any memory space.

    Would this work for you?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    New item !· The Stamp Tester !

    http://hometown.aol.com/newzed/index.html
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-08-10 21:16
    No, it isn't possible to alias into an array -- and I've asked for it too. Nobody's saying that it's impossible, yet, but our [noparse][[/noparse]very hard working] compiler engineer hasn't been able to figure out how to cleanly implement the feature you're looking for. It does become a bit more complex, as you can imagine, when you want to alias a type other than the base (alias a nib into an array of bytes, for example).

    It takes a bit more typing of definitions, but you can get the aliasing you want:

    colors· VAR· Byte
    red···· VAR··colors
    green·· VAR· Byte
    blue··· VAR· Byte
    redHi·· VAR· red.HIGHBYTE
    redLo···VAR· red.LOWBYTE


    You can access colors as an array(actually, any variable can be treated like an array and it doens't have to be declared -- declaring an array simply reserves RAM), individual elements as red, green, and blue, and you can get to the high and low nibbles of red.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • DonDon Posts: 34
    edited 2004-08-10 23:35
    Jon,

    I don't think that you can do that. With '{$PBASIC 2.5} I get an error on red.HIGHBYTE. It says that it expects a smaller size variable modifier.

    I'm using V2.1 of the IDE. Is it different with other versions?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don Kinzer

    ·
  • NewzedNewzed Posts: 2,503
    edited 2004-08-10 23:39
    I think Jon meant red.highnib and red.lownib

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-08-11 06:40
    Yes, I certainly did mean HIGHNIB and LOWNIB. Sorry, I was in a rush to get out to a meeting.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2004-08-11 07:27
    Hi Don,

    The sneaky but much more flexible way to do what you want on the Stamp is to use implied arrays.
    status         VAR Byte         ' implicitly same as status(0)
    status1        VAR Byte         ' status(1)
    status2        VAR Byte          ' status(2)
    status3         VAR Byte          'status(3)
    dataready    VAR status1.highnib
    
    


    Now, you can still refer to the array elements just like you would if you had explicitly declared an array:

    DEBUG HEX2 status(1)
    is the same thing as
    DEBUG HEX2 status1
    You don't have to do anything to make that happen--It is built into the memory structure of the Stamp that elements defined in sequence are automatically considered an array.

    You could define an additional element like this:

    statusNibs VAR status.nib0

    Then,
    statusNibs(3)=$C
    would have the same effect as
    dataready=$C
    because dataready is the 4th nibble in memory up from status.nib0.

    In my bigger projects, I hardly ever define arrays explicitly, rather take advantage of the much more flexible implicit arrays.

    -- Tracy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-05-09 22:45
    Hello Tracy, after almost 2 1/2 years ...

    I tried your example and that works fine.
    Why is the in following case Arr1 not equal to Arr(1)

    Arr VAR Bit
    Tst VAR Byte
    Arr1 VAR Tst.BIT3

    I had expected that this could work since Arr1 (Tst.Bit3) is the next bit memory after Arr.

    thx,
    Ed
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-05-09 23:21
    I checked the memory map and it looks like the bit and byte memory variables do not show up in memory in the same
    sequence as the sequence in which they were declared ... must be memory usage optimisation ?!

    Ed
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-10 05:36
    >Why is the in following case Arr1 not equal to Arr(1)
    >Arr VAR Bit
    >Tst VAR Byte
    >Arr1 VAR Tst.BIT3

    Space for variables in memory is always allocated in order of the size of the variables. So in the physical RAM, all the words you define come first, then bytes, then nibs, then bits. Within each size, the space is in fact allocated in the order the variables are declared in your program. In your example, Tst will be allocated the first byte in memory, and Arr will follow that. So in the following diagram, Z is your bit variable, Arr, and Y is Tst.bit3, and X is Tst.bit1.

    XxxYxxxxZz
    



    Arr(1) is z, the bit that follows Arr.

    > the bit and byte memory variables do not show up in memory in the same sequence as the sequence in which they were declared

    Right, they appear in order of size, and within each size, they are in fact allocated in order as declared. You can count on that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-05-10 12:15
    Tracy,

    thank you very much. This helps to build nice data definitions.

    Ed
Sign In or Register to comment.