Alias for an array element?
Don
Posts: 34
Is it possible to create an alias for an element of an array?· For example, say that I have a byte array defined thusly:
I'd like to create an alias that refers to the high nibble of the second element of the array.
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:
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
·
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
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
·
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
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
·
Sid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
The sneaky but much more flexible way to do what you want on the Stamp is to use implied arrays.
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
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
sequence as the sequence in which they were declared ... must be memory usage optimisation ?!
Ed
>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.
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
thank you very much. This helps to build nice data definitions.
Ed