Shop OBEX P1 Docs P2 Docs Learn Events
alias into an array — Parallax Forums

alias into an array

ArchiverArchiver Posts: 46,084
edited 2002-01-16 22:49 in General Discussion
Group:

Is it possible to establish an alias into a byte array using the bs2? I
have read the
"Alias and Modifiers" section of the Basic Stamp manual, but it only
addresses
aliases into words, bytes, and nibs.
Basically, I want the following lines of code to work:

my_byte_array var byte(10) ' a 10 byte array
alias_byte var my_byte_array(3) ' establish an alias for the 4th
byte

This code results in error messages. Does anyone know if what I'm
trying to do
is possible, and how? Thanks.

--

Clark Hughes

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 19:25
    The Stamp variable memory can be treated like an array of any type -- even if
    not explicitly declared. Since variables of the same type are allocated in
    the order of their declaration, you can use the technique below to get around
    your situation. It takes a bit more typing, but works as you want it to.

    -- Jon Williams
    -- Parallax

    ' {$STAMP BS2}

    myArray VAR Byte ' declare array name
    array0 VAR myArray ' align (alias) first byte to array name
    array1 VAR Byte
    array2 VAR Byte
    array3 VAR Byte
    idx VAR Nib


    Test:
    FOR idx = 0 TO 3
    myArray(idx) = idx
    NEXT

    DEBUG DEC ?array2
    END


    In a message dated 1/16/02 1:15:18 PM Central Standard Time,
    jchughes@a... writes:


    > Group:
    >
    > Is it possible to establish an alias into a byte array using the bs2? I
    > have read the
    > "Alias and Modifiers" section of the Basic Stamp manual, but it only
    > addresses
    > aliases into words, bytes, and nibs.
    > Basically, I want the following lines of code to work:
    >
    > my_byte_array var byte(10) ' a 10 byte array
    > alias_byte var my_byte_array(3) ' establish an alias for the 4th
    > byte
    >
    > This code results in error messages. Does anyone know if what I'm
    > trying to do
    > is possible, and how? Thanks.
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 19:38
    Nice solution Jon

    Question from a stamp newbie:
    I read in the memory section that the BS2 allocated (I think) 26 bytes for
    variables. Does allocating an alias use memory allocated for variable
    storage. I understand that it's referencing the variable pointed to, but you
    still have to store the alias name and the address of the variable
    referenced.

    Cheers
    Peter Kerr

    Original Message
    From: jonwms@a... [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=H9WUc3TZ2Q9FFD7TSgiP7l3UdkRFnnGV2q1-iHcVkKPumnrcRa2oBFSmatH2prF5hqLHmuC2WbSu]jonwms@a...[/url
    Sent: Wednesday, January 16, 2002 11:25 AM
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] alias into an array


    The Stamp variable memory can be treated like an array of any type -- even
    if
    not explicitly declared. Since variables of the same type are allocated in
    the order of their declaration, you can use the technique below to get
    around
    your situation. It takes a bit more typing, but works as you want it to.

    -- Jon Williams
    -- Parallax

    ' {$STAMP BS2}

    myArray VAR Byte ' declare array name
    array0 VAR myArray ' align (alias) first byte to array
    name
    array1 VAR Byte
    array2 VAR Byte
    array3 VAR Byte
    idx VAR Nib


    Test:
    FOR idx = 0 TO 3
    myArray(idx) = idx
    NEXT

    DEBUG DEC ?array2
    END


    In a message dated 1/16/02 1:15:18 PM Central Standard Time,
    jchughes@a... writes:


    > Group:
    >
    > Is it possible to establish an alias into a byte array using the bs2? I
    > have read the
    > "Alias and Modifiers" section of the Basic Stamp manual, but it only
    > addresses
    > aliases into words, bytes, and nibs.
    > Basically, I want the following lines of code to work:
    >
    > my_byte_array var byte(10) ' a 10 byte array
    > alias_byte var my_byte_array(3) ' establish an alias for the 4th
    > byte
    >
    > This code results in error messages. Does anyone know if what I'm
    > trying to do
    > is possible, and how? Thanks.
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 19:41
    Jon:

    Thanks for the reply. I see what you're doing - reading "past" the declared
    variable. That will work fine for my application. It appears that multiple
    declarations require no more program space than the single declaration of
    the array, so that is good news as well. Thanks for your assistance.

    Clark

    jonwms@a... wrote:

    > The Stamp variable memory can be treated like an array of any type -- even if
    > not explicitly declared. Since variables of the same type are allocated in
    > the order of their declaration, you can use the technique below to get around
    > your situation. It takes a bit more typing, but works as you want it to.
    >
    > -- Jon Williams
    > -- Parallax
    >
    > ' {$STAMP BS2}
    >
    > myArray VAR Byte ' declare array name
    > array0 VAR myArray ' align (alias) first byte to array name
    > array1 VAR Byte
    > array2 VAR Byte
    > array3 VAR Byte
    > idx VAR Nib
    >
    > Test:
    > FOR idx = 0 TO 3
    > myArray(idx) = idx
    > NEXT
    >
    > DEBUG DEC ?array2
    > END
    >
    > In a message dated 1/16/02 1:15:18 PM Central Standard Time,
    > jchughes@a... writes:
    >
    > > Group:
    > >
    > > Is it possible to establish an alias into a byte array using the bs2? I
    > > have read the
    > > "Alias and Modifiers" section of the Basic Stamp manual, but it only
    > > addresses
    > > aliases into words, bytes, and nibs.
    > > Basically, I want the following lines of code to work:
    > >
    > > my_byte_array var byte(10) ' a 10 byte array
    > > alias_byte var my_byte_array(3) ' establish an alias for the 4th
    > > byte
    > >
    > > This code results in error messages. Does anyone know if what I'm
    > > trying to do
    > > is possible, and how? Thanks.
    > >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and Body
    of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

    --

    J. Clark Hughes
    Applied Research Laboratories
    The University of Texas
    Austin, Texas
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 19:59
    Peter -

    Experiment a little with the editor and you will see
    that the alias does not take up any more variable
    space. This makes sense, since it could just be stored
    in the program memory. But, lo and behold, it doesn't
    take up any more program/EEPROM space either. Why? The
    alias are present in the editor for the convenience of
    the programmer, but once tokenized, every occurence of
    a variable - whether as its original name or as an
    alias - need only be stored in a way that the BASIC
    interpreter can read it. The interpreter has no need
    to distiguish, it only needs to know the variable
    location to examine.

    Bob Pence


    --- Peter Kerr <peter.kerr@z...> wrote:
    > Nice solution Jon
    >
    > Question from a stamp newbie:
    > I read in the memory section that the BS2 allocated
    > (I think) 26 bytes for
    > variables. Does allocating an alias use memory
    > allocated for variable
    > storage. I understand that it's referencing the
    > variable pointed to, but you
    > still have to store the alias name and the address
    > of the variable
    > referenced.
    >
    > Cheers
    > Peter Kerr
    >
    [noparse][[/noparse]snip]

    __________________________________________________
    Do You Yahoo!?
    Send your FREE holiday greetings online!
    http://greetings.yahoo.com
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 20:06
    Thanks Bob
    So an alias is handled the same as a CON. Both are handled/substituted at
    the pre-processor stage?

    Peter
    Original Message
    From: Pence Bob [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=G8m22EIW2rGVuwtqse3nYCmuAaaNEm2WqJAhTOv5SSQUZmnuHg0IjUdH3wCT9NRCvzjaH-El_hb7w60LL5k]bobpence_2000@y...[/url
    Sent: Wednesday, January 16, 2002 12:00 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] alias into an array


    Peter -

    Experiment a little with the editor and you will see
    that the alias does not take up any more variable
    space. This makes sense, since it could just be stored
    in the program memory. But, lo and behold, it doesn't
    take up any more program/EEPROM space either. Why? The
    alias are present in the editor for the convenience of
    the programmer, but once tokenized, every occurence of
    a variable - whether as its original name or as an
    alias - need only be stored in a way that the BASIC
    interpreter can read it. The interpreter has no need
    to distiguish, it only needs to know the variable
    location to examine.

    Bob Pence


    --- Peter Kerr <peter.kerr@z...> wrote:
    > Nice solution Jon
    >
    > Question from a stamp newbie:
    > I read in the memory section that the BS2 allocated
    > (I think) 26 bytes for
    > variables. Does allocating an alias use memory
    > allocated for variable
    > storage. I understand that it's referencing the
    > variable pointed to, but you
    > still have to store the alias name and the address
    > of the variable
    > referenced.
    >
    > Cheers
    > Peter Kerr
    >
    [noparse][[/noparse]snip]

    __________________________________________________
    Do You Yahoo!?
    Send your FREE holiday greetings online!
    http://greetings.yahoo.com

    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 21:18
    Yes, it does seem to work like the prepocessor concept
    C/C++ coders are familiar with. There is a difference
    in that the variable value is stored in the variable
    space, with a handle for it in the program area, while
    the constant would presumably become a literal in the
    program area.

    The length of a variable's handle (a term here used
    loosely) in the program area is essentially fixed,
    while the length of a literal (and therefore a
    constant) depends on the size of the value. So the
    question arises, could a program with a nearly-full
    EEPROM program area and spare variable space have some
    of its program area freed up by replacing constants or
    repeated literals with variables? A little
    experimentation seems to indicate that the space
    consumed by each variable handle (whether the variable
    is a bit, nibble, byte, or word) is no less than the
    space used by a word-sized literal or constant, so the
    answer would be, no. However a similar setup might
    make sense for other processors that split variable
    and program space in a similar way.

    Bob

    --- Peter Kerr <peter.kerr@z...> wrote:
    > Thanks Bob
    > So an alias is handled the same as a CON. Both are
    > handled/substituted at
    > the pre-processor stage?
    >
    > Peter
    >
    Original Message
    > From: Pence Bob [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=s05kg7hvfRbdoha82QmZJgKxEWS8JicYXs3zz5mt1A9A9AejzYv7RCu_2sqZn4GiH790V4d9V4GiInjfy8ldgg]bobpence_2000@y...[/url
    > Sent: Wednesday, January 16, 2002 12:00 PM
    > To: basicstamps@yahoogroups.com
    > Subject: RE: [noparse][[/noparse]basicstamps] alias into an array
    >
    >
    > Peter -
    >
    > Experiment a little with the editor and you will see
    > that the alias does not take up any more variable
    > space. This makes sense, since it could just be
    > stored
    > in the program memory. But, lo and behold, it
    > doesn't
    > take up any more program/EEPROM space either. Why?
    > The
    > alias are present in the editor for the convenience
    > of
    > the programmer, but once tokenized, every occurence
    > of
    > a variable - whether as its original name or as an
    > alias - need only be stored in a way that the BASIC
    > interpreter can read it. The interpreter has no need
    > to distiguish, it only needs to know the variable
    > location to examine.
    >
    > Bob Pence
    [noparse][[/noparse]snip]

    __________________________________________________
    Do You Yahoo!?
    Send FREE video emails in Yahoo! Mail!
    http://promo.yahoo.com/videomail/
  • ArchiverArchiver Posts: 46,084
    edited 2002-01-16 22:49
    In a message dated 1/16/02 1:46:42 PM Central Standard Time,
    peter.kerr@z... writes:


    > I read in the memory section that the BS2 allocated (I think) 26 bytes for
    > variables. Does allocating an alias use memory allocated for variable
    > storage. I understand that it's referencing the variable pointed to, but you
    > still have to store the alias name and the address of the variable
    >

    Aliasing does not use any more memory -- it simply gives the same storage
    location another name. This can be useful (reuse of space with logical
    names), but it can lead to problems too.

    When using aliases, you must be careful that one part of the program doesn't
    stomp on a piece of data in use or expected somewhere else.

    -- Jon Williams
    -- Parallax


    [noparse][[/noparse]Non-text portions of this message have been removed]
Sign In or Register to comment.