NASM Forum > Programming with NASM

Error when using "istruc"

<< < (3/3)

Bryant Keller:
I don't really advise it, but if you want to play around with doing some structure emulation or enhancement stuff inside macros you might want to go to the source. STRUC/ISTRUC are little more than a bit of ABSOLUTE magic. For example, here is a structure:


--- Code: ---[ABSOLUTE 0]
mytype:
.long resd 1
.short resw 1
.byte resb 1
mytype_size equ ($-mytype)
%ifdef __SECT__
__SECT__
%endif
--- End code ---

Now we can create an instance of our structure:


--- Code: ---SECTION .bss
mystruct: RESB mytype_size
--- End code ---

And then use our structure somewhere in our code:


--- Code: ---SECTION .text
...
    Xor Eax, Eax
    Mov [mystruct + mytype.long], Eax
    Inc Eax
    Mov [mystruct + mytype.short], Ax
    Inc Eax
    Mov [mystruct + mytype.byte], Al
...
--- End code ---

Not bad huh. That's all STRUC's are really doing. We can even do union simulation with ABSOLUTE as well. Check out this snippet.


--- Code: ---%imacro CODESEG 0
%if __BITS__ < 32
SEGMENT .text
%else
SECTION .text
%endif
%endm

%imacro DATASEG 0
%if __BITS__ < 32
SEGMENT .data
%else
SECTION .data
%endif
%endm

%imacro STACKSEG 0
%if __BITS__ < 32
SEGMENT .bss
%else
SECTION .bss
%endif
%ENDM

%imacro STRUCT 1-2 0
%push __STRUCT__
%define %$name %1
[ABSOLUTE %2]
%{$name}:
%endm

%imacro UNION 1-2 0
%push __UNION__
%define %$name %1
%assign %$base %2
%assign %$max 0
[ABSOLUTE %{$base}]
%{$name}:
%endm

%imacro SET 1+
%ifctx __STRUCT__
. %+ %1
%elifctx __UNION__
[ABSOLUTE %{$base}]
. %+ %1
%if %$max < ($-%{$name})
%assign %$max ($-%{$name})
%endif
%else
%1
%endif
%endm

%imacro ENDS 0
%ifctx __UNION__
%{$name}_size   EQU %$max
%pop
%elifctx __STRUCT__
%{$name}_size   EQU ($-%{$name})
%pop
%else
%define __SECT__ [SECTION .data]
%endif
%ifdef __SECT__
__SECT__
%endif
%endm
--- End code ---

That's from my old BKMACROS.ASM that used to float around ASM Community back before NASM32 was written. It's not the prettiest implementation of unions/structures but it does handle the issue of calculating the union '_size' value based on a %$max variable. Take your time looking over it, it should give you some insight into how structures/unions are implemented on a deeper level as I'm seeing you trying to implement macro wrappers with them.

Regards,
Bryant Keller

Navigation

[0] Message Index

[*] Previous page

Go to full version