Variable Declarations

Variable Declarations

All variable declarations must be contained within a data segment. They are used to declare variables for use in a code block such as a thread, subroutine, or macro function. A variable is declared using the following syntax:

 var_name type_name optional_initialization

The variable name may be any valid identifier. The type name must be a type or type alias already known by the compiler. The optional initialization format depends on the variable type, but for non-aggregate (scalar) types the format is simply a constant integer or constant expression (which may not contain whitespace). See the examples later in this section.

The NXT firmware supports several different types of variables which are grouped into two categories: scalar types and aggregate types. Scalar types are a single integer value which may be signed or unsigned and occupy one, two, or four bytes of memory. The keywords for declaring variables of a scalar type are listed in the following table:

Type NameInformation
byte, ubyte, db8 bit unsigned
sbyte8 bit signed
word, uword, dw16 bit unsigned
sword16 bit signed
dword, udword, dd32 bit unsigned
sdword32 bit signed
long, ulong32 bit unsigned (alias for dword, udword)
slong32 bit signed (alias for sdword)
float32 bit IEEE 754 floating point type
mutexSpecial type used for exclusive subroutine access
Table 1. Scalar Types

Examples of scalar variable declarations are as follow:

 dseg segment
   x byte            // initialized to zero by default
   y byte 12         // initialize to 12
   z sword -2048     // a signed value
   myVar big 0x12345 // use a type alias
   var1 dword 0xFF   // value is 255
   myMutex mutex     // mutexes ignore initialization, if present
   bTrue byte 1      // byte variables can be used as booleans
 dseg ends

Aggregate variables are either structures or arrays of some other type (either scalar or aggregate). Once a user-defined struct type has been defined it may be used to declare a variable of that type. Similarly, user-defined struct types can be used in array declarations. Arrays and structs may be nested (i.e., contained in other arrays or structures) as deeply as the needs of your program dictate, but nesting deeper than 2 or 3 levels may lead to slower program execution due to NXT firmware memory constraints.

Examples of aggregate variable declarations are as follow:

 dseg segment
   buffer byte[] // starts off empty
   msg byte[] 'Testing'
   // msg is an array of byte =
   // (0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x00)
   data long[] {0xabcde, 0xfade0} // two values in the array
   myStruct ComplexStruct // declare an instance of a struct
   Points MyPoint[] // declare an array of a structs
   msgs byte[][] // an array of an array of byte
 dseg ends

Byte arrays may be initialized either by using braces containing a list of numeric values ({val1, val2, ..., valN}) or by using a string constant delimited with single-quote characters ('Testing'). Embedded single quote characters must be escaped using the '\' character. The '\' character can be part of the string by using two forward slashes: '\'. Arrays of any scalar type other than byte should be initialized using braces. Arrays of struct and nested arrays cannot be initialized.


Generated by  doxygen 1.6.2