Attribute ‘pack_mode’¶
This pragma defines how a data structure is packed during the allocation. The attribute must be inserted above the data structure and affects the packing of the entire structure.
Syntax:
{attribute 'pack_mode' := '<Value>'}
Insert location: above the declaration of the data structure
pack_mode | Associated packing method | Description |
---|---|---|
0 | aligned, | All variables are allocated to byte addresses. There are no memory gaps. |
1 | 1-byte-aligned | |
2 | 2-byte-aligned | There are
|
4 | 4-byte-aligned | There are
|
8 | 8-byte-aligned | There are
|
Note
Depending on the structure, there may be no difference in the memory mapping of the individual modes. Therefore, the memory allocation of a structure with pack_mode =4
can correspond to that of pack_mode = 8
.
Note
Arrays of structures: If the structures are combined in arrays, then bytes are added at the end of the structure so that the next structure is aligned.
Example 1
Example
{attribute 'pack_mode' := '1'}
TYPE myStruct:
STRUCT
Enable: BOOL;
Counter: INT;
MaxSize: BOOL;
MaxSizeReached: BOOL;
END_STRUCT
END_TYPE
The memory range for a variable of the data type myStruct
is allocated ‘aligned’: If the storage address of its component Enable
is 0x0100
, for example, then the component Counter
follows at the address 0x0101
, MaxSize
at address 0x0103
and MaxSizeReached
at address 0x0104
. In the case of 'pack_mode':=2
, Counter
would be at 0x0102
, MaxSize
at 0x0104
and MaxSizeReached
at 0x0106
.
Example 2
STRUCT
Var1 : BOOL := 16#01;
Var2 : BYTE := 16#11;
Var3 : WORD := 16#22;
Var4 : BYTE := 16#44;
Var5 : DWORD := 16#88776655;
Var6 : BYTE := 16#99;
Var7 : BYTE := 16#AA;
Var8 : DWORD := 16#AA;
END_TYPE
pack_mode = 0 | pack_mode = 1 | pack_mode = 2 | pack_mode = 4 | pack_mode = 8 | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Variable | Value | Variable | Value | Variable | Value | Variable | Value | Variable | Value | |
0 | Var1 | 01 | Var1 | 01 | Var1 | 01 | Var1 | 01 | Var1 | 01 |
1 | Var2 | 11 | Var2 | 11 | Var2 | 11 | Var2 | 11 | Var2 | 11 |
2 | Var3 | 22 | Var3 | 22 | Var3 | 22 | Var3 | 22 | Var3 | 22 |
3 | … | 00 | … | 00 | … | 00 | … | 00 | … | 00 |
4 | Var4 | 44 | Var4 | 44 | Var4 | 44 | Var4 | 44 | Var4 | 44 |
5 | Var5 | 55 | Var5 | 55 | ||||||
6 | … | 66 | … | 66 | Var5 | 55 | ||||
7 | … | 77 | … | 77 | … | 66 | ||||
8 | … | 88 | … | 88 | … | 77 | Var5 | 55 | Var5 | 55 |
9 | Var6 | 99 | Var6 | 99 | … | 88 | … | 66 | … | 66 |
10 | Var7 | AA | Var7 | AA | Var6 | 99 | … | 77 | … | 77 |
11 | Var8 | AA | Var8 | AA | Var7 | AA | … | 88 | … | 88 |
12 | … | 00 | … | 00 | Var8 | AA | Var6 | 99 | Var6 | 99 |
13 | … | 00 | … | 00 | … | 00 | Var7 | AA | Var7 | AA |
14 | … | 00 | … | 00 | … | 00 | ||||
15 | … | 00 | ||||||||
16 | Var8 | AA | Var8 | AA | ||||||
17 | … | 00 | … | 00 | ||||||
18 | … | 00 | … | 00 | ||||||
19 | … | 00 | … | 00 | ||||||
20 | ||||||||||
21 | ||||||||||
22 | ||||||||||
23 | ||||||||||
24 | ||||||||||
25 | ||||||||||
26 | ||||||||||
27 | ||||||||||
28 | ||||||||||
29 | ||||||||||
30 | ||||||||||
31 |
Example 3
STRUCT
Var1 : BYTE := 16#01;
Var2 : LWORD := 16#11;
Var3 : BYTE := 16#22;
Var4 : BYTE := 16#44;
Var5 : DWORD := 16#88776655;
Var6 : BYTE := 16#99;
Var7 : BYTE := 16#AA;
Var8 : WORD := 16#AA;
END_TYPE
pack_mode = 0 | pack_mode = 1 | pack_mode = 2 | pack_mode = 4 | pack_mode = 8 | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Variable | Value | Variable | Value | Variable | Value | Variable | Value | Variable | Value | |
0 | Var1 | 01 | Var1 | 01 | Var1 | 01 | Var1 | 01 | Var1 | 01 |
1 | Var2 | 11 | Var2 | 11 | ||||||
2 | … | 00 | … | 00 | Var2 | 11 | ||||
3 | … | 00 | … | 00 | … | 00 | ||||
4 | … | 00 | … | 00 | … | 00 | Var2 | 11 | ||
5 | … | 00 | … | 00 | … | 00 | … | 00 | ||
6 | … | 00 | … | 00 | … | 00 | … | 00 | ||
7 | … | 00 | … | 00 | … | 00 | … | 00 | ||
8 | … | 00 | … | 00 | … | 00 | … | 00 | Var2 | 11 |
9 | Var3 | 22 | Var3 | 22 | … | 00 | … | 00 | … | 00 |
10 | Var4 | 44 | Var4 | 44 | Var3 | 22 | … | 00 | … | 00 |
11 | Var5 | 55 | Var5 | 55 | Var4 | 44 | … | 00 | … | 00 |
12 | … | 66 | … | 66 | Var5 | 55 | Var3 | 22 | … | 00 |
13 | … | 77 | … | 77 | … | 66 | Var4 | 44 | … | 00 |
14 | … | 88 | … | 88 | … | 77 | … | 00 | ||
15 | Var6 | 99 | Var6 | 99 | … | 88 | … | 00 | ||
16 | Var7 | AA | Var7 | AA | Var6 | 99 | Var5 | 55 | Var3 | 22 |
17 | Var8 | AA | Var8 | AA | Var7 | AA | … | 66 | Var4 | 44 |
18 | … | 00 | … | 00 | Var8 | AA | … | 77 | ||
19 | … | 00 | … | 88 | ||||||
20 | Var6 | 99 | Var5 | 55 | ||||||
21 | Var7 | AA | … | 66 | ||||||
22 | Var8 | AA | … | 77 | ||||||
23 | … | 00 | … | 88 | ||||||
24 | Var6 | 99 | ||||||||
25 | Var7 | AA | ||||||||
26 | Var8 | AA | ||||||||
27 | … | 00 | ||||||||
28 | ||||||||||
29 | ||||||||||
30 | ||||||||||
31 |