News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

UNION and initialisation ...

Started by James Ladd, September 03, 2005, 04:24:59 AM

Previous topic - Next topic

James Ladd

I have a structure that contains a UNION.
I cannot work out how to initialise it. Please help ?


EX1 struct
    ex1f1 DWORD 0
    ex1f2 WORD  0
EX1 ends

EX2 struct
    ex2f1 BYTE 0
    ex2f2 BYTE 0
EX2 ends

EX3 struct
    func DWORD 0
    union
        first  EX1 <>
        second EX2 <>
    ends
EX3 ends

; -----------------------------------------------------------------------------
;
.data
    example EX3 <1, 2, 3>
.code


Does masm support the initialisation of UNIONS ?

Darrel

It works this way

EX1 struct
    ex1f1 DWORD 0
    ex1f2 WORD  0
EX1 ends

EX2 struct
    ex2f1 BYTE 0
    ex2f2 BYTE 0
EX2 ends

EX3 struct
    func DWORD 1
    union
        first  EX1 <2,3>
        second EX2 <2,3>
    ends
EX3 ends

.data
example EX3 <>


Hope this helps,

Darrel

EDIT: 
QuoteDoes masm support the initialisation of UNIONS ?

No it doesn't.

If I get arount to making my own compiler I will make sure it does.  :8)

If you need to use more than one structure and initialize at least one of them, you can use a hex editor and modify them.

James Ladd

#2
Ok, thanks for that.

Let me rephrase the question. How can I initialise structures containing a UNION in the data section ?
For example I want to do this:


; -----------------------------------------------------------------------------
;
.data
    example EX3 <1, 2, 3>
.code


I know this isnt the hardest of questions but I do need help with this !

rgs, striker

tenkey

It's ambiguous.

Do you want to store "2" as a DWORD or a BYTE?

You're probably going to need to use an old-school technique - create a label and define each field separately.

Here are two examples of storing 2 and 3 as BYTEs (using EX2 field).

; this example uses the most primitive data types
example LABEL EX3      ; define a label of type EX3
        DWORD 1        ; func
        BYTE  2,3      ; ex2f1 and ex2f2
        BYTE  4 DUP(0) ; padding to fill the EX3 structure

; this example uses as many structures as possible
example LABEL EX3      ; define a label of type EX3
        DWORD 1        ; func
        EX2   <2,3>    ; second
        BYTE  4 DUP(0) ; padding to fill the EX3 structure
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

James Ladd

tenkey,

Thanks for the example.
It must be real old-school coz I didnt know you could do that. Ill look up MASM help on LABEL.

Ill try what you suggest too.

<edit>Hey it worked well thanks so much.

Rgs, striker