SPRUI03E June 2015 – January 2023
Declare Union Type
[utag] .union [expr]
[mem0
] element [expr0]
[mem1 ] element [expr1]
. . .
[memn ] .tag
utag [exprn ]
. . .
[memN ] element [exprN]
[size] .endunion
label .tag utag
The .union directive assigns symbolic offsets to the elements of alternate data structure definitions to be allocated in the same memory space. This enables you to define several alternate structures and then let the assembler calculate the element offset. This is similar to a C union. The .union directive does not allocate any memory; it merely creates a symbolic template that can be used repeatedly.
A .struct definition can contain a .union definition, and .structs and .unions can be nested.
The .endunion directive terminates the union definition.
The .tag directive gives structure or union characteristics to a label to simplify the symbolic representation and provide the ability to define structures or unions that contain other structures or unions. The .tag directive does not allocate memory. The structure or union tag of a .tag directive must have been previously defined.
Parameters used with the .struct, .endstruct, and .tag directives are:
These examples show unions with and without tags.
1 .global employid
2 xample .union ; utag
3 0000 ival .word ; member1 = int
4 0000 fval .float ; member2 = float
5 0000 sval .string ; member3 = string
6 0002 real_len .endunion ; real_len = 2
7
8 000000 .bss employid, real_len ;allocate memory
9
10 employid .tag xample ; name an instance
11 000000 0000- ADD employid.fval, A ; access union element
1
2 ; utag
3 0000 x .long ; member1 = long
4 0000 y .float ; member2 = float
5 0000 z .word ; member3 = word
6 0002 size_u .endunion ; real_len = 2
7