Assembly Language Help (alang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
Declare Union as Data Type
 Example                                   Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
  Syntax:   name UNION [alignment] [, NONUNIQUE]
              fielddeclarations
            [name] ENDS
 
  See also: ENDS, STRUCT, RECORD, Dot Operator (.), LABEL, /Zp,
            MASM 5.1 Compatibility
 
  Description:
 
     Declares a union as a new data type. Allows the same memory
     locations to be overlapped with different types. Identical to
     STRUCT except that each member has a 0 offset. The size of the
     union is the size of the largest member of the union.
 
     Unions can be used to store one type of data in some circumstances
     and another type in a different situation. Unions also allow you
     to store data as one data type and then read it as another. Unions
     are initialized by using the union name as a data declaration
     directive. You only need to initialize a union for one data type.
 
     You must refer to union members by their member names. The LABEL
     directive allows you to refer to the entire union as a unit.
 
     The first-level definition of a union must include the <name>
     label preceding the ENDS. In nested unions, terminate the block
     with a ENDS statement without the <name> label.
 
     The <alignment> field determines the byte alignment of any
     structures. This field can be 1, 2, or 4. The default is 1, unless
     overridden by the /Zp command-line option or the
     ALIGN or EVEN directives.
 
     The NONUNIQUE parameter allows elements of the union to be
     accessed only by their fully specified names.
 
     Some directives are allowed in the <fielddeclarations> field;
     see the BNF Appendix in the MASM Programmer's Guide.
 
  Example:
 
     EXTENDCHAR  UNION
                 ascii       BYTE    ?      ;Define a union of byte and
                 kanji       WORD    ?      ; word
     EXTENDCHAR  ENDS
     initial     EXTENDCHAR  <J>            ;Create a union variable
     mov         al, initial.ascii          ;Move byte-length data
     mov         bx, initial.kanji          ;Move word-length data
                                    -♦-