A.6.3 Offset for Structure Members

A structure member is accessed after adding the offset to the structure address. Since a small offset is advantageous for the size, members often used should be declared at the top.

The most effective combination is within 32 bytes from the top for the signed char or unsigned char type, within 64 bytes from the top for the short or unsigned short type, or within 128 bytes from the top for the int, unsigned int, long, or unsigned long type.

[Example]

An example in which the code is changed because of the offset of the structure is shown below.

Source code before improvement

struct str {
     long L1[8];
     char C1;
};
struct str STR1;
char x;
void func()
{
     x = STR1.C1;
}

 

Assembly-language expansion code before improvement

_func: 
     MOV.L #_STR1,R4
     MOVU.B 20H[R4],R5
     MOV.L #_x,R4
     MOV.B R5,[R4]
     RTS

 

Source code after improvement

struct str {
     char C1;
     long L1[8];
};
struct str STR1;
char x;
void func()
{
     x = STR1.C1;
}

 

Assembly-language expansion code after improvement

_func:
     MOV.L #_STR1,R4
     MOVU.B [R4],R5
     MOV.L #_x,R4
     MOV.B R5,[R4]
     RTS

Note

When defining a structure, declare the members while considering the boundary alignment value. The boundary alignment value of a structure is the most largest boundary alignment value within the structure. The size of a structure becomes a multiple of the boundary alignment value. For this reason, when the end of a structure does not match the boundary alignment value of the structure itself, the size of the structure also includes the unused area that was created for guaranteeing the next boundary alignment.

 

Source code before improvement

/* Boundary alignment value is 4 because the maximum member is the int type */
struct str {
     char C1; /* 1 byte + 3 bytes of boundary alignment */
     long L1; /* 4 bytes */
     char C2; /* 1 byte */
     char C3; /* 1 byte */
     char C4; /* 1 byte + 1 byte of boundary alignment */
}STR1;

 

str size before improvement

     .SECTION B,DATA,ALIGN=4
     .glb _STR1
_STR1:      ; static: STR1
     .blkl 3 

 

Source code after improvement

/* Boundary alignment value is 4 because the maximum member is the int type */
struct str {
     char C1; /* 1 byte */
     char C2; /* 1 byte */
     char C3; /* 1 byte */
     char C4; /* 1 byte */
     long L1; /* 4 bytes */
}STR1;

 

str size after improvement

     .SECTION B,DATA,ALIGN=4
     .glb _STR1
_STR1:      ; static: STR1
     .blkl 2