4.22 Declaring Arrays in Your HLA Programs


4.22 Declaring Arrays in Your HLA Programs

Before you access elements of an array, you need to set aside storage for that array. Fortunately, array declarations build on the declarations you've seen thus far. To allocate n elements in an array, you would use a declaration like the following in one of the variable declaration sections:

 ArrayName: basetype[n]; 

ArrayName is the name of the array variable and basetype is the type of an element of that array. This sets aside storage for the array. To obtain the base address of the array, just use ArrayName.

The "[n]" suffix tells HLA to duplicate the object n times. Now let's look at some specific examples:

 static      CharArray: char[128];         // Character array with elements 0..127.      IntArray: integer[ 8 ];       // "integer" array with elements 0..7.      ByteArray: byte[10];          // Array of bytes with elements 0..9.      PtrArray: dword[4];           // Array of double words with elements 0..3. 

The second example, of course, assumes that you have defined the integer data type in the type section of the program.

These examples all allocate storage for uninitialized arrays. You may also specify that the elements of the arrays be initialized to a single value using declarations like the following in the static and readonly sections:

 RealArray: real32[8] := [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]; IntegerAry: integer[8] := [ 1, 1, 1, 1, 1, 1, 1, 1 ]; 

These definitions both create arrays with eight elements. The first definition initializes each four-byte real value to 1.0; the second declaration initializes each integer element to one. Note that the number of constants within the square brackets must exactly match the size of the array.

This initialization mechanism is fine if you want each element of the array to have the same value. What if you want to initialize each element of the array with a (possibly) different value? No sweat; just specify a different set of values in the list surrounded by the square brackets in the preceding example:

 RealArray: real32[8] := [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; IntegerAry: integer[8] := [ 1, 2, 3, 4, 5, 6, 7, 8 ]; 




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net