I am writing a project for the MSP430F5419 using CCS v4.1.2.00027.
I have a C header file containing structure definitions which include unions used to provide bit and byte access to a flag:
struct SYSVARS_FIXED
{
unsigned char ucCommsAlarmState;
unsigned char ucBattThresh;
/// (FLAGS) General flags showing status of various events
union STATUSFLAGS_BB
{
/// Byte access to flag
unsigned char ucByte;
/// Bit access to flag
struct STATUSFLAGS_BITS
{
unsigned char b0 :1;
unsigned char b1 :1;
unsigned char b2 :1;
unsigned char b3 :1;
unsigned char b4 :1;
unsigned char b5 :1;
unsigned char b6 :1;
unsigned char b7 :1;
} bits;
} flStatusFlags;
unsigned char zdepVectors[6];
unsigned char ucTest;
....etc...
};
An instance of this struct is declared and used ok in C code but when I try to access it in an assembler file (using .cdecls to include the header file) the addresses of the later elements of the struct are slightly incorrect:
e.g. "MOV.B #4, &sv_fix.ucTest" is assembled as "MOV.B #4, 0x1c0a" when the actual address of the element is 0x1c09.
The problem appears to be that the C compiler aligns the flag union as a single byte however the asm .cdecls interpretation aligns it as a word - adding an extra byte of blank space. I have several similar unions in this (and other) structure(s) and the assembler access displays progressively larger errors when accessing the later elements. One way to remove the problem is to declare the union element as ints, thus forcing the alignment to be the same for the C code and the asm code however this obviously fills up variables with undesirable padding so I consider it more of a bodge than a fix.
The MSP430 Assembly Language User Guide (slau131d sect 12.2.13) states " C/C++ structures and unions are converted to assembly .struct and .union elements. Padding and ending alignments are added as necessary to make the resulting assembly structure have the same size and member offsets as the C/C++ source." suggesting to me that the structure should be interpreted correctly by the .cdecls...
Am I missing something? Are there any assembler/compiler/other commands that I can use to force the assembler and compiler interpretations to match up?
Thanks in advance.
Chris.