Attribute 'pack_mode'

The 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 whole structure.

Syntax: {attribute 'pack_mode' := '<pack mode value>'}

Possible values for <pack mode value>:

<pack mode value>

Associated package type

Description

0

aligned

All variables are assigned to byte addresses; no memory gaps occur.

1

1-byte aligned

2

2-byte aligned

There are

  • 1-byte variables assigned to byte addresses
  • 2-byte variables assigned to addresses that are divisible by 2. The maximum gap that may occur is 1 byte
  • 4-byte variables assigned to addresses that are divisible by 2. The maximum gap that may occur is 1 byte
  • 8-byte variables assigned to addresses that are divisible by 2. The maximum gap that may occur is 1 byte
  • Strings always at byte addresses. No gap is created.

4

4-byte aligned

There are

  • 1-byte variables assigned to byte addresses
  • 2-byte variables assigned to addresses that are divisible by 2. The maximum gap that may occur is 1 byte
  • 4-byte variables assigned to addresses that are divisible by 4. The maximum gap that may occur is 3 byte
  • 8-byte variables assigned to addresses that are divisible by 4. The maximum gap that may occur is 3 byte
  • Strings always at byte addresses. No gap is created.

8

8-byte aligned

There are

  • 1-byte variables assigned to byte addresses
  • 2-byte variables assigned to addresses that are divisible by 2. The maximum gap that may occur is 1 byte
  • 4-byte variables assigned to addresses that are divisible by 4. The maximum gap that may occur is 3 byte
  • 8-byte variables assigned to addresses that are divisible by 8. The maximum gap that may occur is 7 byte
  • Strings always at byte addresses. No gap is created.

Insertion location: Line above the declaration of the data structure

Attribute 'pack_mode' 1:

Depending on the configuration of the structure, there may be no difference in the memory split between the individual modes. In this case the memory distribution of a structure with pack_mode = 4 can correspond to that of pack_mode = 8.

Attribute 'pack_mode' 2:

Arrays of structures: If the structures are grouped into arrays, bytes are inserted at the end of the structure so that the next structure is aligned again.

Example 1

{attribute 'pack_mode' := '1'} 
TYPE ST_MyStruct:  
STRUCT 
    bEnable         : BOOL;
    nCounter        : INT; 
    nMaxSize        : INT;
    bMaxSizeReached : BOOL;
    bReset          : BOOL;
END_STRUCT
END_TYPE 

The memory area for a variable of data type ST_MyStruct is allocated "aligned: If the memory address of your component bEnable is 0x0100 for example, then the component nCounter follows at address 0x0101, nMaxSize at address 0x0103, bMaxSizeReached at address 0x0105 and bReset at address 0x0106. If 'pack_mode' := '2', then nCounter would be at 0x0102, nMaxSize at 0x0104, bMaxSizeReached at 0x0106 and bReset at 0x0107.

Example 2

STRUCT 
    bVar1 : BOOL  := 16#01;
    nVar2 : BYTE  := 16#11;
    nVar3 : WORD  := 16#22;
    nVar4 : BYTE  := 16#44;
    nVar5 : DWORD := 16#88776655;
    nVar6 : BYTE  := 16#99;
    nVar7 : BYTE  := 16#AA;
    nVar8 : 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 
    nVar1 : BYTE  := 16#01;
    nVar2 : LWORD := 16#11;
    nVar3 : BYTE  := 16#22;
    nVar4 : BYTE  := 16#44;
    nVar5 : DWORD := 16#88776655;
    nVar6 : BYTE  := 16#99;
    nVar7 : BYTE  := 16#AA;
    nVar8 : 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