SPRU513Z August 2001 – October 2023 SM320F28335-EP
Declare C Structure Type
[stag] .cstruct|.cunion [expr]
[mem0] element [expr0]
[mem1] element [expr1]
. . .
. . .
[memn] .tag stag [exprn]
[memN] element [exprN]
[size] .endstruct|.endunion
label .tag stag
The .cstruct and .cunion directives have been added to support ease of sharing of common data structures between assembly and C code. The .cstruct and .cunion directives can be used exactly like the existing .struct and .union directives except that they are guaranteed to perform data layout matching the layout used by the C compiler for C struct and union data types.
In particular, the .cstruct and .cunion directives force the same alignment and padding as used by the C compiler when such types are nested within compound data structures.
The .endstruct directive terminates the structure definition. The .endunion directive terminates the union definition.
The .tag directive gives structure characteristics to a label, simplifying the symbolic representation and providing the ability to define structures that contain other structures. The .tag directive does not allocate memory. The structure tag (stag) of a .tag directive must have been previously defined.
Following are descriptions of the parameters used with the .struct, .endstruct, and .tag directives:
This example illustrates a structure in C that will be accessed in assembly code.
;typedef struct MYSTR1
;{ long l0; /* offset 0 */
; short s0; /* offset 2 */
;} MYSTR1; /* size 4, alignment 2 */
;typedef struct MYSTR2
;{ MYSTR1 m1; /* offset 0 */
; short s1; /* offset 4 */
;} MYSTR2; /* size 6, alignment 2 */
;
; The structure will get the following offsets once the C compiler lays out the structure
; elements according to C standard rules:
;
; offsetof(MYSTR1, l0) = 0
; offsetof(MYSTR1, s0) = 2
; sizeof(MYSTR1) = 4
;
; offsetof(MYSTR2, m1) = 0
; offsetof(MYSTR2, s1) = 4
; sizeof(MYSTR2) = 6
;
; Attempts to replicate this structure in assembly using .struct/.union directives will not
; create the correct offsets because the assembler tries to use the most compact arrangement:
MYSTR1.struct
l0.long ; bytes 0 and 1
s0.short ; byte 2
M1_LEN .endstruct ; size 4, alignment 2
MYSTR2.struct
m1.tag MYSTR1 ; bytes 0-3
s1.short ; byte 4
M2_LEN .endstruct ; size 6, alignment 2
.sect "data1"
.word MYSTR1.l0
.word MYSTR1.s0
.word M1_LEN
.sect "data2"
.word MYSTR2.m1
.word MYSTR2.s1
.word M2_LEN
; The .cstruct/.cunion directives calculate offsets the same as the C compiler. The resulting
; assembly structure can be used to access elements of the C structure. Compare differences
; in the offsets of those structures defined via .struct above and the offsets for C code.
CMYSTR1 .cstruct
l0 .long
s0 .short
MC1_LEN .endstruct
CMYSTR2 .cstruct
m1 .tag CMYSTR1
s1 .short
MC2_LEN .endstruct
.sect "data3"
.word CMYSTR1.l0, MYSTR1.l0
.word CMYSTR1.s0, MYSTR1.s0
.word MC1_LEN, M1_LEN
.sect "data4"
.word CMYSTR2.m1, MYSTR2.m1
.word CMYSTR2.s1, MYSTR2.s1
.word MC2_LEN, M2_LEN