Author Topic: Nasm Macro in Macro  (Read 11645 times)

Offline Curry

  • Jr. Member
  • *
  • Posts: 2
Nasm Macro in Macro
« on: June 14, 2010, 03:32:46 PM »
Sorry for my bad english.
I'm trying to learn how to use macros nasm.

Only I do not know how to get at "mov dword [%?], eax" the above-defined label.
"%?" returns "Dim", but it should give back "$Test".  ???

Can someone give me a tip on how I get on the label?
Thank you!

Code: [Select]
[Bits 32]

; Beendet das Skript.
%macro Exit 0-1 0
extern ExitProcess
import ExitProcess Kernel32.dll
push %1
call [ExitProcess]
%endmacro

; Zeigt eine einfache MessageBox an.
%macro MsgBox 3-5 0,0
extern MessageBoxA
import MessageBoxA User32.dll
push %1
push %%Titel
push %%Text
push %5
call [MessageBoxA]
[section .data]
%%Titel:
db %2,0
%%Text:
db %3,0
[section .text]
%endmacro

; Deklariert eine Variable.
%macro Dim 2
[section .data]
%1:
dd %2
[section .text]
%macro %1 2+
%1 %2
mov dword [%?], eax
%endmacro
%endmacro

segment .text use32
..start:

; Autoit Syntax Start
Dim $Test, 2
$Test MsgBox, 3, "Frage:", "Ja oder Nein?"
Exit

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Nasm Macro in Macro
« Reply #1 on: June 14, 2010, 10:31:32 PM »
This is the sort of thing I've thought about in the past myself but as of yet is not supported in NASM. The problem is, how do we propose a method of making the nested %1 form from the original %1. So for a short answer... that's not supported. But you're definitely not alone in having tried this and I'm sure if a reasonable method to accomplishing it without totally changing the macro syntax was proposed, eventually it would be supported. Unfortunately most of the "fixes" I've heard have been dramatic changes to the syntax of NASM or really obfuscated things like "%1::%1" which I think is just goofy looking. :p

Sorry if that wasn't what you wanted to hear, and if anyone has a work around I too look forward to hearing it.

Regards,
Bryant Keller

About Bryant Keller
bkeller@about.me

Offline Curry

  • Jr. Member
  • *
  • Posts: 2
Re: Nasm Macro in Macro
« Reply #2 on: June 15, 2010, 10:35:20 AM »
I got this morning an idea how can I pass the label.
Nasm supports overloading of parameters.

Unfortunately, it takes but a lot of code.
Can not simplify it somehow?

Code: [Select]
[Bits 32]

; Beendet das Skript.
%macro Exit 0-1 0
extern ExitProcess
import ExitProcess Kernel32.dll
push %1
call [ExitProcess]
%endmacro

; Zeigt eine einfache MessageBox an.
%macro MsgBox 3-5 0,0
extern MessageBoxA
import MessageBoxA User32.dll
push %1
push %%Titel
push %%Text
push %5
call [MessageBoxA]
[section .data]
%%Titel:
db %2,0
%%Text:
db %3,0
[section .text]
%endmacro

; Deklariert eine Variable.
%macro Dim 2
[section .data]
%1:
dd %2
[section .text]
%macro %1 2 %1
%1 %2
mov dword [%3], eax
%endmacro
%macro %1 3 %1
%1 %2, %3
mov dword [%4], eax
%endmacro
%macro %1 4 %1
%1 %2, %3, %4
mov dword [%5], eax
%endmacro
%macro %1 5 %1
%1 %2, %3, %4, %5
mov dword [%6], eax
%endmacro
%macro %1 6 %1
%1 %2, %3, %4, %5, %6
mov dword [%7], eax
%endmacro
%macro %1 7 %1
%1 %2, %3, %4, %5, %6, %7
mov dword [%8], eax
%endmacro
; ...
%endmacro

segment .text use32
..start:

; Autoit Syntax Start
Dim $Test, 2
$Test MsgBox, 3, "Frage:", "Ja oder Nein?"
Exit

Offline Cyrill Gorcunov

  • NASM Developer
  • Full Member
  • *****
  • Posts: 179
  • Country: 00
Re: Nasm Macro in Macro
« Reply #3 on: June 22, 2010, 07:50:11 PM »
Hi, will the following xdefine trick help?

Code: [Select]
; Deklariert eine Variable.
%macro Dim 2
[section .data]
%1:
dd %2
[section .text]
%xdefine caller %1
%macro %1 2+
%1 %2
mov dword [caller], eax
%endmacro
%endmacro
« Last Edit: June 22, 2010, 08:01:18 PM by Cyrill Gorcunov »