Author Topic: Preprocessor Testing (creating macro from within macro)  (Read 4158 times)

Klod

  • Guest
Preprocessor Testing (creating macro from within macro)
« on: November 28, 2010, 04:55:13 AM »
This is what I had tried to accomplish. With release candidate 2.10rc2-20101123 this works well as long as macros with more than one parameters are defined.

Code: [Select]
%warning __NASM_VER__ 2.10rc2-20101123

%macro definem 2+
mac %1,%2
%endmacro

%macro mac 2-*
EXTERN %1
%macro %[%1] 1
        push %1
       call %?   
%endmacro
%endm

definem printf,ptr
definem ExitProcess,NULL
definem scanf,ptr

In order to fully capitalize on the new possibilities it would be necessary to change %macro %[%1] 1 to:
%macro %[%1] %{0}-1
or %macro %[%1] %$var

How difficult would it be to implement this change?

Regards,
Klod

%macro %[%1] 1

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Preprocessor Testing (creating macro from within macro)
« Reply #1 on: November 29, 2010, 01:23:51 AM »
Code: [Select]
[BITS 32]
[CPU 486]

%macro definem 2+
    mac %1, %2
%endmacro

%macro mac 2-*
%push

    EXTERN %1
    %assign %$ac (%0 -1)

    %macro %[%1] %$ac
        %rep %0
            %rotate -1
            push %1
        %endrep
        call %?
        add esp, (%0 * 4)
    %endmacro

%pop
%endmacro

definem printf, format, message
definem exit, errcode

SECTION .text
GLOBAL main

main:
    printf dword strFormat, dword strMessage
    sub eax, eax
    exit eax

SECTION .data

strFormat    DB "[+] %s", 10
strMessage   DB "Hello, World!", 0

This code was tested and ran under NetBSD using:

Code: [Select]
b0x$ nasm -v
NASM version 2.10rc2 compiled on Nov 28 2010
b0x$ nasm -f elf -o klod.o klod.asm
b0x$ cc klod.o -o klod
b0x$ ./klod
[+] Hello, World!
b0x$

About Bryant Keller
bkeller@about.me

Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: Preprocessor Testing (creating macro from within macro)
« Reply #2 on: November 29, 2010, 01:18:01 PM »
Code: [Select]
    %assign %$ac (%0 -1)

    %macro %[%1] %$ac

Works, but requires such a temporary smacro to allow arbitrary computation of the mmacro's parameter count. %macro should accept a critical expression in parentheses (similar to the handling of "section ... align="). I'll prepare a patch when time permits.
C. Masloch

Klod

  • Guest
Re: Preprocessor Testing (creating macro from within macro)
« Reply #3 on: November 30, 2010, 03:34:13 AM »
my bad, I tried only %macro %[%1] (%0 -1). In the previous versions I had tried all variations possible. So I assumed it would not work.

Quote
Works, but requires such a temporary smacro to allow arbitrary computation of the mmacro's parameter count. %macro should accept a critical expression in parentheses (similar to the handling of "section ... align="). I'll prepare a patch when time permits.

Thanks

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Re: Preprocessor Testing (creating macro from within macro)
« Reply #4 on: January 02, 2013, 01:02:14 PM »
Stumbled up on this topic accidentally..

Just wondering if this 'macro creating macro' concept holds good now. (w.r.t to the reverted preprocessor changes).

Coz , i see call %?  expands to call mac

instead of 'call printf' and 'call exit'

my nasm version shows,

NASM version 2.10.06 compiled on Dec  4 2012

Thanks,
Mathi.

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Re: Preprocessor Testing (creating macro from within macro)
« Reply #5 on: January 11, 2013, 06:49:26 PM »
This is the closest i have got. :)

Code: [Select]
[BITS 32]
[CPU 486]

%macro definem 2+
%1:    mac %1, %2
%endmacro

%macro mac 2-*
%push

    EXTERN %1
    %assign %$ac (%0 -1)
    %xdefine caller %1
    %macro %[%1] 0-%$ac
        %rep %0
            %rotate -1
            push %1
        %endrep
        call %00
        add esp, (%0 * 4)
    %endmacro

%pop
%endmacro

definem printf, format, message
definem exit, errcode

SECTION .text
GLOBAL main

main:
    printf dword strFormat, dword strMessage
    sub eax, eax
    exit eax

SECTION .data

strFormat    DB "[+] %s", 10, 0
strMessage   DB "Hello, World!", 0

Using %00 to get the label before the macro call.

Assemble & Link with,
nasm -felf -o test.o test.asm
gcc test.o  -o test