4.33 Controlling Field Offsets Within a Record


4.33 Controlling Field Offsets Within a Record

By default, whenever you create a record, HLA automatically assigns the offset zero to the first field of that record. This corresponds to record offsets in a high level language and is the intuitive default. In some instances, however, you may want to assign a different starting offset to the first field of the record. HLA provides a mechanism that lets you set the starting offset of the first field in the record.

The syntax to set the first offset is

 name:      record := startingOffset;           << Record Field Declarations >>      endrecord; 

Using the syntax above, the first field will have the starting offset specified by the startingOffset int32 constant expression. Because this is an int32 value, the starting offset value can be positive, zero, or negative.

One circumstance where this feature is invaluable is when you have a record whose base address is actually somewhere within the data structure. The classic example is an HLA string. An HLA string uses a record declaration similar to the following:

 record      MaxStrLen: dword;      length: dword;      charData: char[xxxx]; endrecord; 

As you're well aware by now, HLA string pointers do not contain the address of the MaxStrLen field; they point at the charData field. The str.strRec record type found in the HLA Standard Library string module uses a record declaration similar to the following:

 type      strRec:           record := -8;                MaxStrLen: dword;                length: dword;                charData: char;           endrecord; 

The starting offset for the MaxStrLen field is -8. Therefore, the offset for the length field is -4 (four bytes later), and the offset for the charData field is zero. Therefore, if EBX points at some string data, then "(type str.strRec [ebx]).length" is equivalent to "[ebx-4]" because the length field has an offset of -4.

Generally, you will not use HLA's ability to specify the starting field offset when creating your own record types. Instead, this feature is most useful when you are mapping an HLA data type over the top of some other predefined data type in memory (strings are a good example, but there are many other examples as well).




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