| |||
| Home > Writing ARM and Thumb Assembly Language > Describing data structures with MAP and FIELD directives > Forcing correct alignment | |||
You are likely to have problems if you include some character variables in the data structure, as in Example 2.20. This is because a lot of words are misaligned.
Example 2.20.
StartOfData EQU 0x1000
EndOfData EQU 0x2000
MAP StartOfData
Char FIELD 1
Char2 FIELD 1
Char3 FIELD 1
Integer FIELD 4 ; alignment = 3
Integer2 FIELD 4
String FIELD MaxStrLen
Array FIELD ArrayLen*8
BitMask FIELD 4
EndOfUsedData FIELD 0
ASSERT EndOfUsedData <= EndOfData
You cannot use the ALIGN directive, because the ALIGN directive
aligns the current location within memory. MAP and FIELD directives
do not allocate any memory for the structures they define.
You could insert a dummy FIELD 1 after Char3
FIELD 1. However, this makes maintenance difficult if you
change the number of character variables. You must recalculate the
right amount of padding each time.
Example 2.21 shows
a better way of adjusting the padding. The example uses a FIELD directive
with a 0 operand to label the end of the character
data. A second FIELD directive inserts the correct
amount of padding based on the value of the label. An :AND: operator
is used to calculate the correct value.
The (-EndOfChars):AND:3 expression calculates
the correct amount of padding:
0 if EndOfChars is 0 mod 4;
3 if EndOfChars is 1 mod 4;
2 if EndOfChars is 2 mod 4;
1 if EndOfChars is 3 mod 4.
This automatically adjusts the amount of padding used whenever character variables are added or removed.
Example 2.21.
StartOfData EQU 0x1000
EndOfData EQU 0x2000
MAP StartOfData
Char FIELD 1
Char2 FIELD 1
Char3 FIELD 1
EndOfChars FIELD 0
Padding FIELD (-EndOfChars):AND:3
Integer FIELD 4
Integer2 FIELD 4
String FIELD MaxStrLen
Array FIELD ArrayLen*8
BitMask FIELD 4
EndOfUsedData FIELD 0
ASSERT EndOfUsedData <= EndOfData