## ALP Programming Guide and Reference

[prev][next][parent][TOC]

### UNION (Define a Union Type Name)

Defines a Union-TypeName that represents an aggregate data type containing one or more fields. All of the fields occupy the same physical position in storage.

Syntax

    Union - Name   UNION
       FieldDeclaration
        .
        .
        .
    Union - Name   ENDS

Where FieldDeclaration has the following form:

    [ FieldName ]   Allocation - TypeName   InitialValue   [ ,   InitialValue   . . . ]

Remarks

This directive is not available in M510 mode.

The syntax for the FieldDeclaration is that of a normal data allocation statement. See the section on Data Allocation for a full description of this syntax.

The various parts of the FieldDeclaration are described as follows:

FieldName Each FieldName entry is converted to Union-FieldName when processing of the union definition is complete. If this field is omitted and the Allocation-TypeName resolves to a Structure-TypeName or Union- TypeName, then all of the fields defined within the imbedded structure or union are promoted to be visible at the same level as other FieldName entries in the current union given by the Union-Name.

Allocation-TypeName The allowable values for this field are described in detail in the Data Allocation section. The assembler accepts imbedded occurrences of other structures or unions by specifying an identifier that resolves to a Structure-TypeName or Union-TypeName in this field.

InitialValue The InitialValue field must be an Expression that resolves to an ExpressionType appropriate for the Allocation-TypeName utilized in the FieldDeclaration. Only the InitialValue expression for the first field becomes part of the union type definition; expressions specified for the remaining fields are ignored. This value is used as the default initializer when an instance of the union is allocated and no explict override value is specified for the field.

Example

               . 386
    IS _ sint32    equ   - 4
    IS _ sint16    equ   - 2
    IS _ sint8     equ   - 1
    NO _ TYPE      equ    0
    IS _ uint8     equ    1
    IS _ uint16    equ    2
    IS _ uint32    equ    4

    TYPE _ T       typedef   SBYTE

    DATA _ T       union
       uint8      BYTE     ?
       sint8      SBYTE    ?
       uint16     WORD     ?
       sint16     SWORD    ?
       uint32     DWORD    ?
       sint32     SDWORD   ?
    DATA _ T       ends

    VALUE _ T      struct
       DataType    TYPE _ T   NO _ TYPE
       DataValue   DATA _ T   { }
    VALUE _ T      ends

                 . data
    Value        VALUE _ T   {   IS _ uint8 ,   {   1   }   }              ;   unsigned   8 - bit   value   of   1

               . code

    ;   Procedure :    IsNegative
    ;   Returns    :    1   in   EAX   if   Value . DataValue   holds   a   negative   number
    ;                0   in   EAX   if   Value . DataValue   holds   a   positive   number

    IsNegative   proc
                 cmp     Value . DataType ,   NO _ TYPE           ;   check   sign   of   TYPE _ T
                 jns     short   Positive                     ;   if   positive ,   so   is   value

    ;   check   for   signed   8 - bit   integer
                 cmp     Value . DataType ,   IS _ sint8
                 jne     short   @ F                            ;   not   8 ,   check   for   16
                 movsx   EAX ,   Value . DataValue . sint8        ;   convert   8   bits   to   32
                 jmp     short   Check                         ;   and   check   the   value

    ;   check   for   signed   16 - bit   integer
    @ @ :          cmp     Value . DataType ,   IS _ sint16
                 jne     short   @ F                            ;   not   16 ,   check   for   32
                 movsx   EAX ,   Value . DataValue . sint16       ;   convert   16   bits   to   32
                 jmp     short   Check                         ;   and   check   the   value

    ;   check   for   signed   32 - bit   integer
    @ @ :          cmp     Value . DataType ,   IS _ sint32
                 jne     short   Positive                     ;   unknown ,   assume   positive
                 mov     EAX ,   Value . DataValue . sint32       ;   get   full   32   bit   number

    Check :       or      EAX , EAX                             ;   check   for   negative   value
                 jns     short   Positive                     ;   no   sign   bit ,   positive
                 mov     EAX ,   1                              ;   indicate   negative
                 ret                                        ;   and   return
    Positive :    mov     EAX ,   0                              ;   indicate   positive
                 ret                                        ;   and   return
    IsNegative   endp
    end