Start Back Next End
  
ZBasic Language Reference
81
ZBasic Microcontrollers
This technique may be useful, for example, for reading and writing data packets to an external device.
One aspect of using aliases that requires careful thought and possibly some experimentation is that an
alias of a fundamental type (e.g. Byte, Integer, etc.) must be defined so that it aligns on a byte
boundary.  If the target variable for the alias is also a fundamental type this will not be an issue because
the fundamental types are always byte-aligned.  On the other hand, sub-byte types may or may not be
byte-aligned so defining an alias to a sub-byte type may result in a compiler error message indicating that
it is not byte-aligned.  See Section 3.24 for more information on this topic.
BasicX Compatibility Note
Aliases are not available in BasicX compatibility mode.
3.21 Based Variables
Based variables are a very powerful tool and their use is recommended for advanced programmers only. 
If used carelessly or without a complete understanding of their characteristics they may cause your
program to malfunction in ways that are quite difficult to diagnose.
A based variable is similar to a procedure parameter that is passed ByRef in the respect that no space is
allocated for the data item.  Rather, the location (i.e. the addess) of the based variable is specified using
an integral expression that can be constant or computed at run-time.  The effect that can be achieved
using a based variable is similar to using an alias but a based variable is even more powerful because of
the ability of the address to change at run time.
The syntax for defining a based variable at the module level is shown below.
{Public | Private | Dim} <name>[(<dim-list>)] As <type> Based <base-expr>
As with normal variables, Dim has exactly the same effect as Private.  Within a subroutine, a function
or any block structure, a local based variable may be defined using the syntax shown below.
Dim <name>[(<dim-list>)] As <type> Based <base-expr>
In both cases the <base-expr> element is an integral expression that gives the base address of the
variable.  Some examples will help clarify the concept.
Dim bv as Byte Based &H100
This defines a Byte variable whose address is a constant value.
Dim addr as Integer
Dim bv as Byte Based addr
This defines a Byte variable whose address is given by the value of the Integer variable addr.
Dim addr as Integer
Dim sel as Byte
Dim fv as Single Based addr + CInt(sel * 3)
This defines a Single variable whose address is given by the value of an expression.
Dim addr as Integer
Dim bv as Byte Based addr.DataAddress
This defines a Byte variable whose address is constant - the same as the address of the variable addr
Except for one important aspect, this has exactly the same effect as the following code.
Previous page Top Next page