ZHCU947E June 2015 – January 2023
声明 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 STRUCT1
; { int i0; /* offset 0 */
; short s0; /* offset 4 */
; } struct1; /* size 8, alignment 4 */
;
; typedef struct STRUCT2
; { struct1 st1; /* offset 0 */
; short s1; /* offset 8 */
; } struct2; /* size 12, alignment 4 */
;
; The structure will get the following offsets once the C compiler lays out the structure
; elements according to the C standard rules:
;
; offsetof(struct1, i0) = 0
; offsetof(struct1, s0) = 4
; sizeof(struct1) = 8
; offsetof(struct2, s1) = 0
; offsetof(struct2, i1) = 8
; sizeof(struct2) = 12
;
; Attempts to replicate this structure in assembly using the .struct/.union directives will not
; create the correct offsets because the assembler tries to use the most compact arrangement:
struct1 .struct
i0 .int ; bytes 0-3
s0 .short; bytes 4-5
struct1len .endstruct ; size 6, alignment 4
struct2 .struct
st1 .tag struct1 ; bytes 0-5
s1 .short ; bytes 6-7
endstruct2 .endstruct ; size 8, alignment 4
.sect "data1"
.word struct1.i0 ; 0
.word struct1.s0 ; 4
.word struct1len ; 6
.sect "data2"
.word struct2.st1 ; 0
.word struct2.s1 ; 6
.word endstruct2 ; 8
;
; The .cstruct/.cunion directives calculate offsets in the same manner as the C compiler.The resulting
; assembly structure can be used to access the elements of the C structure.Compare the difference
; in the offsets of those structures defined via .struct above and the offsets for the C code.
cstruct1 .cstruct
i0 .int ; bytes 0-3
s0 .short; bytes 4-5
cstruct1len .endstruct ; size 8, alignment 4
cstruct2 .cstruct
st1 .tag cstruct1 ; bytes 0-7
s1 .short; bytes 8-9
cendstruct2 .endstruct ; size 12, alignment 4
.sect "data3"
.word cstruct1.i0, struct1.i0 ; 0
.word cstruct1.s0, struct1.s0 ; 4
.word cstruct1len, struct1len ; 8
.sect "data4"
.word cstruct2.st1, struct2.st1 ; 0
.word cstruct2.s1, struct2.s1 ; 8
.word cendstruct2, endstruct2 ; 12