2.4.1. Struct size

When declaring structs, keep overall size to less than 64 words.

In direct addressing mode, the 6-bit offset value is concatenated with the 16-bit DP register. The offset value enables 0 to 63 words to be addressed relative to the current DP register value. Structs larger that 64 words will require DP to the updated before accessing fields that are 64-words apart, resulting in less efficient code due to the extra DP update instructions.

Table 2.1 compares 2 structs - Test1 is larger than 64 words and Test2 is smaller than 64 words. Table 2.2 compares the generated assembly for functions with accesses to each struct. The larger struct requires an extra MOVW instruction to set the DP before accessing field b.

Table 2.1 Structure size and efficiency of generated code

struct Test1 is larger than 64 words

struct Test2 is smaller than 64 words

typedef struct
{
    int a;
    int array[63];
    int b;
} Test1;

Test1 t1;

void test1()
{
    t1.a = t1.b;
    t1.b = 42;
}
typedef struct
{
    int a;
    int array[32];
    int b;
} Test2;


Test2 t2;

void test2()
{
    t2.a = t2.b;
    t2.b = 42;
}
Table 2.2 Structure size and efficiency of generated code
||test1||:
        MOVW      DP,#||t1||+64
        MOV       AL,@||t1||+64
        MOVW      DP,#||t1||
        MOV       @||t1||,AL
        MOVW      DP,#||t1||+64
        MOVB      @||t1||+64,#42,UNC
        LRETR
||test2||:
        MOVW      DP,#||t2||+33
        MOV       AL,@||t2||+33
        MOV       @||t2||,AL
        MOVB      @||t2||+33,#42,UNC
        LRETR