NASM Forum > Programming with NASM

Variables in macros

(1/3) > >>

isywum:
Hi,

I'm beginner of programming NASM and got a little problem.
I wanna create a variable in a macro but I always get some errors.
Could anyone tell me how to create a variable in a macro?! :)

Thanks in advanve.

Frank Kotler:
It would probably be helpful to know what you tried, and what errors you got. Here's "an" example:


--- Code: ---%macro say 1+
    section .data
    %%str db %1
    db 0
    section .text
    push %%str
    call putz ; must exist
%endmacro

--- End code ---

That may not be any help, if you're trying to do something different... :)

Best,
Frank

Mathi:
Can you give more details on your problem.
May be a portion of the code which is causing the issue?

'creating a variable in macro' -  If you are doing something like

%macro INTEGER 1
%1: dd 0
%endmacro

you can use this macro in section .data like,

INTEGER X
INTEGER Y

We need to make sure the labels are not redefined.

For general documentation of Macros refer to,
http://www.nasm.us/doc/nasmdoc4.html#section-4.3

Bryant Keller:
I would also like to suggest you make clear the term "variable". As Mathi and Frank assumed, a runtime variable can be declared more or less the same way as any other variable, by perpending %% to the label, you make the label local to the context of the macro. Another type of variable you might be referring to is a preprocessor variable. An example of that could be to use %assign or %define. The following example code might give you some info on preprocessor variables (designed for linux).


--- Code: (intarray.asm) ---;; build with:
;; nasm -f elf intarray.asm
;; gcc -nostartfiles -o intarray intarray.o

%macro int_array 1
%push _int_array_
%assign %$index 0
%ifidni __SECT__,[SECTION .bss]
%define %$defint resd 1
%else
%define %$defint dd 0
%endif
%rep %1
.@%{$index}: %{$defint}
%assign %$index %{$index} + 1
%endrep
%pop
%endmacro

EXTERN printf

SECTION .data

strFormat: db "%d", 10, 0

myDataIntegers:
int_array 5

SECTION .bss

myBssIntegers:
int_array 11

SECTION .text

GLOBAL _start
_start:
xor eax, eax
mov [myDataIntegers.@0], eax
inc eax
mov [myDataIntegers.@1], eax
inc eax
mov [myDataIntegers.@2], eax
inc eax
mov [myDataIntegers.@3], eax
inc eax
mov [myDataIntegers.@4], eax
inc eax
mov [myBssIntegers.@0], eax
inc eax
mov [myBssIntegers.@1], eax
inc eax
mov [myBssIntegers.@2], eax
inc eax
mov [myBssIntegers.@3], eax
inc eax
mov [myBssIntegers.@4], eax
inc eax
mov [myBssIntegers.@5], eax
inc eax
mov [myBssIntegers.@6], eax
inc eax
mov [myBssIntegers.@7], eax
inc eax
mov [myBssIntegers.@8], eax
inc eax
mov [myBssIntegers.@9], eax
inc eax
mov [myBssIntegers.@10], eax

mov edi, myDataIntegers
mov ecx, 5
.@LoopFirst:
push ecx
push dword [edi]
push strFormat
call printf
add esp, 8
add edi, 4
pop ecx
dec ecx
or ecx, ecx
jnz .@LoopFirst

mov edi, myBssIntegers
mov ecx, 11
.@LoopSecond:
push ecx
push dword [edi]
push strFormat
call printf
add esp, 8
add edi, 4
pop ecx
dec ecx
or ecx, ecx
jnz .@LoopSecond

xor eax, eax
xor ebx, ebx
inc eax
int 0x80

--- End code ---

Regards,
Bryant Keller

isywum:
Hi,
I'd like to do something like this (it doesn't work of course):
%macro MyMacro 5
res db ?
%%start:
mov ax, %1
add ax, %2
mov res, ax
%endmacro

I tried for example: %%res db ? or %local res db ? etc.

@Bryant Keller:
Unfortunately I don't understand your code. :(

Thanks for your help!

Navigation

[0] Message Index

[#] Next page

Go to full version