4.34 Aligning Fields Within a Record


4.34 Aligning Fields Within a Record

To achieve maximum performance in your programs, or to ensure that HLA's records properly map to records or structures in some high level language, you will often need to be able to control the alignment of fields within a record. For example, you might want to ensure that a double word field's offset is an even multiple of four. You use the align directive to do this, the same way you would use align in the static declaration section of your program. The following example shows how to align some fields on important boundaries:

 type      PaddedRecord:           record                c:char;                align(4);                d:dword;                b:boolean;                align(2);                w:word;           endrecord; 

Whenever HLA encounters the align directive within a record declaration, it automatically adjusts the following field's offset so that it is an even multiple of the value the align directive specifies. It accomplishes this by increasing the offset of that field, if necessary. In the example above, the fields would have the following offsets: c:0, d:4, b:8, w:10. Note that HLA inserts three bytes of padding between c and d and it inserts one byte of padding between b and w. It goes without saying that you should never assume that this padding is present. If you want to use those extra bytes, then declare fields for them.

Note that specifying alignment within a record declaration does not guarantee that the field will be aligned on that boundary in memory; it only ensures that the field's offset is a multiple of the value you specify. If a variable of type PaddedRecord starts at an odd address in memory, then the d field will also start at an odd address (because any odd address plus four is an odd address). If you want to ensure that the fields are aligned on appropriate boundaries in memory, you must also use the align directive before variable declarations of that record type, e.g.,

 static           .           .           .      align(4);      PRvar: PaddedRecord; 

The value of the align operand should be an even value that is evenly divisible by the largest align expression within the record type (four is the largest value in this case, and it's already evenly divisible by two).

If you want to ensure that the record's size is a multiple of some value, then simply stick an align directive as the last item in the record declaration. HLA will emit an appropriate number of bytes of padding at the end of the record to fill it in to the appropriate size. The following example demonstrates how to ensure that the record's size is a multiple of four bytes:

 type      PaddedRec:           record                << some field declarations >>                align(4);           endrecord; 

HLA provides some additional alignment directives for records that let you easily control the alignment of all fields within a record. We'll consider those directives in a later chapter when discussing the interface between HLA and high level languages. In the meantime, if you're interested in more information about record field alignment, please consult the HLA Reference Manual.




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