4.23 HLA Array Constants


4.23 HLA Array Constants

The last few examples in the previous section demonstrate the use of HLA array constants. An HLA array constant is nothing more than a list of values surrounded by a pair of brackets. The following are all legal array constants:

 [ 1, 2, 3, 4 ] [ 2.0, 3.14159, 1.0, 0.5 ] [ 'a', 'b', 'c', 'd' ] [ "Hello", "world", "of", "assembly" ] 

(Note that this last array constant contains four double word pointers to the four HLA strings appearing elsewhere in memory.)

As you saw in the previous section you can use array constants in the static and readonly sections to provide initial values for array variables. Of course, the number of comma-separated items in an array constant must exactly match the number of array elements in the variable declaration. Likewise, the type of the array constant's elements must match the array variable's element type.

Using array constants to initialize small arrays is very convenient. Of course, if your array has several thousand elements, typing them all will not be very much fun. Most arrays initialized this way have no more than a couple hundred entries, and generally far less than a hundred. It is reasonable to use an array constant to initialize such variables. However, at some point it will become far too tedious and error-prone to initialize arrays in this fashion. You probably would not want to manually initialize an array with 1,000 different elements using an array constant. However, if you want to initialize all the elements of an array with the same value, HLA does provide a special array constant syntax for doing so. Consider the following declaration:

 BigArray: uns32[ 1000 ] := 1000 dup [ 1 ]; 

This declaration creates a 1,000-element integer array initializing each element of the array with the value one. The "1000 dup [1]" expression tells HLA to create an array constant by duplicating the single value "[ 1 ]" 1,000 times. You can even use the dup operator to duplicate a series of values (rather than a single value), as the following example indicates:

 SixteenInts: int32[16] := 4 dup [1,2,3,4]; 

This example initializes SixteenInts with four copies of the sequence "1, 2, 3, 4" yielding a total of sixteen different integers (i.e., 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4).

You will see some more possibilities with the dup operator when looking at multidimensional arrays a little later.




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