Start Back Next End
  
ZBasic Language Reference
87
ZBasic Microcontrollers
After the reference is initialized, all uses of the name of the reference variable (without the
.DataAddress qualifier) access the memory to which the reference refers.  Note that the address
reference operator (@) can be used with the variable name instead of the DataAddress property both for
setting the reference address and for obtaining the address of an ordinary variable.  The last line in the
preceding example could thus be rewritten with equivalent effect in any of the three following ways.
@fval = @f
fval.DataAddress = @f
@fval = f.DataAddress
The similarities of the effects of a reference variable and a based variable are likely obvious.  There are,
however, some important differences that may recommend the use of one over the other depending on
the circumstances.  In particular, it is important to note that a reference variable may be used as a
member of a structure or class whereas a based variable cannot.
3.24 Sub-byte Types
In addition to the fundamental data types described in Section 2.2, ZBasic also supports Bit and Nibble
data types.  These are referred to as sub-byte types because they occupy less than a whole byte – 1 bit
and 4 bits, respectively.  In certain circumstances, these types may help reduce the total RAM usage of
an application.  As compared to packing and unpacking bits in your source code, using these types will be
more efficient in both execution time and code space.
Bit and Nibble types may be used in most places where one of the fundamental data types may be
used.  You can define arrays of them, you can define Bit and Nibble constants, you can pass them as
parameters and you can define functions returning these types.  One limitation is that you cannot pass a
Bit or Nibble variable to a subroutine/function by reference unless the variable is byte-aligned (see
further discussion below).  This implies, of course, that you may not be able to pass an array of sub-byte
types as a parameter since arrays are always passed by reference.  The reason for this limitation is that
sub-byte variables do not necessarily begin on a byte boundary and there is no way for the called routine
to know what the alignment might be.  One way to work around this limitation is to define an integral-byte
alias to the sub-byte type, pass the alias by reference and then define a new sub-byte alias in the called
routine.  This works because integral-byte aliases are required to be byte aligned.  Note that when
passed by value as a parameter, a sub-byte type parameter occupies an entire byte on the stack.
Special type conversion functions, CBit() and CNibble() are provided to facilitate the use of these
types in combination with other types.  See the ZBasic System Library Reference Manual for more
information on the conversion functions.
In Section 3.20 Aliases, it was mentioned that there may an issue with aliases of a fundamental type
overlaying sub-byte types.  The issue arises because fundamental data types must be byte-aligned and a
particular Bit or Nibble variable may or may not be byte-aligned.  In most cases it will be simpler to
define a sub-byte alias to overlay variables of fundamental type.  Doing the converse may require some
experimentation to achieve the required byte-alignment.  The alias defined below may or may not be
accepted by the compiler depending on what other sub-byte types are defined at the same scope level
and the order in which the are defined.
Dim ba(1 to 20) as Bit
Dim bval as Byte Alias ba(3)
Internally, the compiler collects together all of the sub-byte types at a given scope level (module,
procedure, block) and allocates space for them collectively.  The Nibble variables are allocated first in
the order that they are defined followed by the Bit variables in the order that they are defined.  The map
file will show a special variable with a name like @BitNib00.  This is the host variable that contains the
individual Bit and Nibble values for a particular scope level.
Previous page Top Next page