anyone know how to define the union structure in NASM?
Posted on 2001-10-03 10:38:27 by doby
from masm32.hlp:


Syntax: name UNION [alignment] [, NONUNIQUE]
fielddeclarations
[name] ENDS

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
-o-


Thomas
Posted on 2001-10-03 10:47:08 by Thomas
well is this what you want?

i just take it from a forum:

*******************************
struc button_event
.type resb 1
.which resb 1
.button resb 1
.state resb 1
endstruc

struc key_event
.type resb 1
.which resb 1
.key resb 1
.state resb 1
endstruc

.data
button istruc button_event
iend
key istruc key_event
iend
type: db 0

...but since nasm uses macros to create instances of structs, it was doing
something equivalent to this:

.data
button.type db 0
button.which db 0
button.button db 0
button.state db 0
key.type db 0
key.which db 0
key.key db 0
key.state db 0
type db 0

... which is like declaring a struct of SDL_event (not a union).

But, there is hope. nasm defines the members of the struct to be constant
offsets from the beginning of the struct. So, you can use
button_event.type, key_event.key, etc to access offsets into the same data
declaration.

So, if you did something like this:

.data
event:
type: times 4 db 0

... then code like this:

.code

mov byte , al
mov byte , al
mov byte al,
...

... would all access the same data, and you could refer to the same data
as different events.

Two problems are:
- you have to know what the size of the largest struct is, so you can be
sure to reserve enough space in .data
- you have to know (i.e. comment) that you are using that space for a
union (it's non-obvious)

You could even make a fako struct of type event to make it a little more
obvious about what you were doing:

struc event
key:
button:
type: resb 4
endstruc

... and declare it like this:

.data
myevent: istruc event
iend

... and access it like this:

.code
mov byte , al
mov byte , al
mov byte al,

The first problem still exists, but it makes a tad bit more obivous that
you are trying to use it as a union.

Does this help?
Posted on 2001-10-03 13:18:54 by Nguga
here is another that i just found on the net:




Code:

union {
long value; /* 4 bytes */
struct {
int first; /* 2 bytes */
INT second; /* 2 bytes */
} half;
} number;





can be transalted into

Code:

number.value equ 0
number.half.first equ 0
number.half.second equ 2
number_size equ 4





You can declare the union in the data segment as
Code:

num1 TIMES number_size DB 0




or in uninitialised segement as
Code:

num1 RESB number_size





Now accessing the members of the union.

If you want to access the 'value' part of 'number' union,
you will code like this
Code:

Mov eax, ; Get 'value'
Mov , eax ; Set 'value'




and to access the 'first' and 'second', you do as
Code:

Mov bx,
Mov cx,





This is a simple example. Complex union can also converted
like this.


_________________
Jones.




GET NaGoA RAD IDE for NASM WIN32ASM !

http:\\Visual-Assembler.pt.vu
Posted on 2001-10-03 17:26:26 by Nguga
i got it...
nguga: your example of EQU is the good way to define the complex union stuct :) thank you verymuch.

thomas: i just want NASM's code not MASM, but anyway thank you :)
Posted on 2001-10-04 06:10:51 by doby
doby: Sorry I misread that for MASM :rolleyes:

Thomas
Posted on 2001-10-04 11:12:16 by Thomas