5.9.6. Forcing correct alignment

You are likely to have problems if you include some character variables in the data structure, as in Example 5.20. This is because a lot of words are misaligned.

Example 5.20. 

StartOfData				EQU		0x1000
EndOfData				EQU		0x2000
				MAP		StartOfData
Char				#		1
Char2				#		1
Char3				#		1
Integer				#		4			; alignment = 3
Integer2				#		4
String				#		MaxStrLen
Array				#		ArrayLen*8
BitMask				#		4
EndOfUsedData				#		0
				ASSERT  EndOfUsedData <= EndOfData

You cannot use the ALIGN directive, because the ALIGN directive aligns the current location within memory. MAP and # directives do not allocate any memory for the structures they define.

You could insert a dummy # 1 after Char3 # 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 5.21 shows a better way of adjusting the padding. The example uses a # directive with a 0 operand to label the end of the character data. A second # 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 5.21. 

StartOfData				EQU		0x1000
EndOfData				EQU		0x2000
				MAP		StartOfData
Char				#		1
Char2				#		1
Char3				#		1
EndOfChars				#		0
Padding				#		(-EndOfChars):AND:3
Integer				#		4
Integer2				#		4
String				#		MaxStrLen
Array				#		ArrayLen*8
BitMask				#		4
EndOfUsedData				#		0
				ASSERT 		EndOfUsedData <= EndOfData
Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D
Non-Confidential