4.6 ENUM and HLA Enumerated Data Types


4.6 ENUM and HLA Enumerated Data Types

In a previous section discussing constants and constant expressions, you saw the following example:

 const      TapeDAT           :=      1; const      Tape8mm           :=      TapeDAT + 1; const      TapeQIC80         :=      Tape8mm + 1; const      TapeTravan        :=      TapeQIC80 + 1; const      TapeDLT           :=      TapeTravan + 1; 

This example demonstrates how to use constant expressions to develop a set of constants that contain unique, consecutive values. There are, however, a couple of problems with this approach. First, it involves a lot of typing (and extra reading when reviewing this program). Second, it's very easy make a mistake when creating long lists of unique constants and reuse or skip some values. The HLA enum type provides a better way to create a list of constants with unique values.

enum is an HLA type declaration that lets you associate a list of names with a new type. HLA associates a unique value with each name (that is, it enumerates the list). The enum keyword typically appears in the type section, and you use it as follows:

 type      enumTypeID:           enum { comma_separated_list_of_names }; 

The symbol enumTypeID becomes a new type whose values are specified by the specified list of names. As a concrete example, consider the data type TapeDrives and a corresponding variable declaration of type TypeDrives:

 type      TapeDrives: enum{ TapeDAT, Tape8mm, TapeQIC80, TapeTravan, TapeDLT}; static      BackupUnit: TapeDrives := TapeDAT;      .      .      .      mov( BackupUnit, al );      if( al = Tape8mm ) then           ...      endif;      // etc. 

By default, HLA reserves one byte of storage for enumerated data types. So the backupunit variable will consume one byte of memory, and you would typically use an 8-bit register to access it.[9] As for the constants, HLA associates consecutive uns8 constant values starting at zero with each of the enumerated identifiers. In the TapeDrives example, the tape drive identifiers would have the values TapeDAT=0, Tape8mm=1, TapeQIC80=2, TapeTravan=3, and TapeDLT=4. You may use these constants exactly as though you had defined them with these values in a const section.

[9]HLA provides a mechanism by which you can specify that enumerated data types consume two or four bytes of memory. See the HLA documentation for more details.




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