ZBasic Language Reference
78
ZX Microcontroller Family
3.21 Aliases
Occasionally, it is useful to be able to access a variable or parts of a variable as different types at different
times. Although this can be accomplished by using the System Library routines BlockMove() or
RamPeek()/RamPoke() it is simpler and more efficient to use the concept of an alias. Simply stated,
defining an alias tells the compiler to generate code to access a variable or part of a variable as if it were
a different type. To be clear, no new data space is allocated by defining an alias. It simply provides a
different way of accessing previously defined space.
The syntax for defining an alias is similar to that for defining a variable. For example, the syntax for
defining an alias at the module level is shown below.
{Public | Private | Dim} <name>[(<dim-list>)] As <type> Alias <var-ref>
As with normal variables, Dim has exactly the same effect as Private. Within a subroutine, a function
or any block structure, a local alias may be defined using the syntax shown below.
Dim <name>[(<dim-list>)] As <type> Alias <var-ref>
In both cases the <var-ref> element is the name of a RAM variable or the name of another alias
optionally including a parenthesized set of one or more constant index expressions. The parenthesized
index list is only allowed, of course, if the referent item is an array.
Examples
Dim ival as Integer, fval as Single
Dim buf(1 to 20) as Byte
Dim b As Byte Alias fval
Dim c As Byte Alias buf(2)
Dim c2(1 to 3) As Byte Alias buf(3)
Dim bval(1 to 5) As Byte Alias ival
The first alias definition allows you to read/write the least significant byte of the Single value fval. The
second definition allows direct access to the second byte of the buf variable. The third example shows
how to define a sub-array within an array. The fourth example shows an alias being defined that spans
more than one variable. Although the compiler allows this form its use is discouraged because the effect
depends on the order in which the compiler chooses to allocate data items.
Recursive alias definitions are not allowed; an error message will be issued by the compiler when a
recursive definition is detected. Note that is not allowed to define an alias that is a String type. You
may, however, define an alias that overlays a String variable although this is not often useful.
One interesting use for an alias is when your application requires that a series of data items be arranged
in a particular order in memory. Consider a situation where, for whatever reason, it would be convenient
to have an Integer value, a Byte value and a Single value that are guaranteed to be arranged in
sequence in memory. This can be accomplished with the definitions shown below.
Dim host(1 to 7) As Byte
Dim ival As Integer Alias host(1)
Dim bval As Byte Alias host(3)
Dim fval As Single Alias host(4)
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