ZHCU875Z August 2001 – October 2023 SM320F28335-EP
声明 C 结构类型
[stag] .cstruct|.cunion [expr]
[mem0] element [expr0]
[mem1] element [expr1]
...
...
[memn] .tag stag [exprn]
[memN] element [exprN]
[size] .endstruct|.endunion
label .tag stag
添加了 .cstruct 和 .cunion 指令,以支持在汇编代码和 C 代码之间轻松共享通用数据结构。.cstruct 和 .cunion 指令的使用方法与现有 .struct 和 .union 指令相同,还可确保执行与 C 编译器用于 C 结构体和联合体数据类型的布局匹配的数据布局。
特别是,当这些类型嵌套在复合数据结构中时,.cstruct 和 .cunion 指令会强制执行与 C 编译器所用相同的对齐和填充方式。
.endstruct 指令用于终止结构定义。.endunion 指令用于终止联合体定义。
.tag 指令为 label 提供结构特征,简化符号表示,还可定义包含其他结构的结构。.tag 指令不会分配存储器空间,必须先定义 .tag 指令的结构标签 (stag)。
以下是要与 .struct、.endstruct 和 .tag 指令一同使用的参数的说明:
此示例演示了以 C 表示、将使用汇编代码访问的结构体。
;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