HCS08SG8-code optimisation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HCS08SG8-code optimisation

1,207 Views
TDKamalAru
Contributor I

Structure inside a structure implementation takes more code memory i think so.

 

I am just assigning an enum value for a nested structure variable. It takes hell number of assembly lines for the single statement. I have a doubt like implementation of a structure variable takes more code memory than a normal variable. Any how the structure implemenattion helps me since different datatypes have been used in my code.

 

Any suggestion please.

Labels (1)
0 Kudos
4 Replies

439 Views
CompilerGuru
NXP Employee
NXP Employee

Can you provide a sample of what the question is about? I'm not sure I understand the question.

The code needed for an access depends on many things, not just the type of the variable but

also of how the variable is accessed.

In most cases it does not matter if a variable is a part of a (global) structure or allocated on its own.

 

What in my experience creates quite a bit of code are bitfields, especially non 1 bit and not unsigned bitfields/

They should only be used if the storage gain is significant (and if portability is not a concern).

 

Daniel

0 Kudos

439 Views
TDKamalAru
Contributor I

/* Decleration Part */ 

typedef struct

{   

    uint16 Variable1_ru16;
 
    union
   {

       uint8 Flags_ru8;
       struct
       {  
           uint8 BitVar1_rb1:1;
           uint8 BitVar2_rb1:1;
           uint8 BitVar3_rb1:1;
           uint8 BitVar4_rb1:1;
           uint8 BitVar5_rb1:1;
           uint8 BitVar6_rb1:1;
           uint8 BitVar7_rb1:1;
       }Bits;
    }BitVars;
}S_Data;

typedef enum

{

    ENUM_0;

    ENUM_1;

}E_ENUMS;

E_ENUMS EnumVar;

S_Data DataBuffer[2]; 

/* Access Part */

 DataBuffer[0].BitVars.Bits.BitVar3_rb1 = ENUM_1;

 

The above is my decleration and access of a structure inside a union inside a structure.

 

The one line statement is converting into many assembly instructions when i observed in the disassembler. So i have a fear that this structure and union implementation takes more memory when compare to simple variable usage.

0 Kudos

439 Views
pgo
Senior Contributor V

Dear TDKamalAru,

 

The efficiency of the code generated depends (obviously) on the architecture of the processor.  The HCS08 has very limited bit addressing instructions.  If the bit field in your structure is not located in the direct page then a read-modify-write sequence of at least three instructions is required.

 

If you can locate your bitfield in the (very limited) direct memory space then the code will be much more efficient. 

 

#pragma DATA_SEG __SHORT_SEG myShortSegment
S_Data DataBuffer[2];
#pragma DATA_SEG DEFAULT

 

 DataBuffer[0].BitVars.Bits.BitVar3_rb1 = ENUM_1;

 

Generates the following:

 

  0001 1402     [5]             BSET  2,DataBuffer:2

 

 This is a 2-byte instruction executing in 5 cycle.

 

 

 This is compared to below when not in the direct page (4 instructions, 7 bytes, 10 cycles).

 

  0001 450002   [3]             LDHX  @DataBuffer:2
  0004 f6       [3]             LDA   ,X
  0005 aa04     [2]             ORA   #4
  0007 f7       [2]             STA   ,X

 

For comparison - If you try your code with a HCS12 chip it generates a single (4-byte,4-cycle) instruction in both case.  The HCS12 has more general bit manipulation instructions.

 

bye

 

PS. Apologies - this overlaps with Bigmac's post. Composed last night but posted this morning.

 

 

 

0 Kudos

439 Views
bigmac
Specialist III

Hello,

 

Evaluating the expression

DataBuffer[0].BitVars.Bits.BitVar3_rb1 = 1;

would seem to generate a realistic amount of code for a bit field manipulation.  The following assembly code was generated.

 

ldhx #0x01DD  // bit field variable address 

lda  ,x

ora  #0x04    // set bit-2

sta  ,x

 

This represents seven bytes of code.

 

Regards,

Mac

0 Kudos