Menu

#26 defined names as macros parameters

open
nobody
None
1
2004-02-13
2003-03-01
Anonymous
No

NASM does not recognizes defined names as macros
parameters :(

well hard core without using macros for
proc
i can use the same parameters of that function in
others
functions , the same for locals using stack

example :
%include
'\lab\vasm\inc\nagoa.inc'

buffer dd 0 ; <-- no need of buffer
resb 256 .... ;)

segment code class=code
use32
..start:

invoke uiui,10
invoke
kuko,20,10
call ExitProcess ,
0

;====================================
kuko:

%define
var_2 ebp+8
%define var_1 ebp+12
enter 0,0

sub
esp,4
%define mylocal ebp-4

ccall
wsprintfA,buffer,"%d", [var_1]
call MessageBoxA
,NULL,buffer, "title 2" ,MB_OK

push ebx
lea
ebx,[mylocal]
ccall wsprintfA,ebx,"%d", [var_1]
call
MessageBoxA ,NULL,ebx, "title 2" ,MB_OK
pop
ebx

leave
ret
8

;=====================================

;===================================
uiui:

%define
var_1 ebp+8 ; ----> var_1 is the same above look there
enter
0,0

sub esp,8
%define bobu ebp-4
%define mylocal
ebp-(4+4)

ccall wsprintfA,buffer,"%d", [var_1]
call
MessageBoxA ,NULL,buffer, "title 2" ,MB_OK

push
ebx
lea ebx,[mylocal]
ccall wsprintfA,ebx,"%d",
[var_1]
call MessageBoxA ,NULL,ebx, "title 2"
,MB_OK
pop ebx

leave
ret
4
;====================================

SO
has we can see i i was very happy becouse , now
i started to test a
macro for that just like MASM ..

but NASM doest not let it
...

i build this macro just to test the idea

%macro
proc1 2
%1:
%define %2 ebp+8
enter
0,0
%endmacro

%macro proc2 3
%1:
%define
%2 ebp+8
%define %3 ebp+12
enter
0,0
%endmacro

then changed the function to
work with this test macro
like
this

;====================================
proc2
kuko,var_2,var_1

sub esp,4
%define mylocal ebp-
(4)

ccall wsprintfA,buffer,"%d", [var_1]
call
MessageBoxA ,NULL,buffer, "title 2" ,MB_OK

lea
ebx,[mylocal]
ccall wsprintfA,ebx,"%d", [var_1]
call
MessageBoxA ,NULL,ebx, "title 2" ,MB_OK
%undef var_1 ; IT
WORKS BUT NEED THIS :(
%undef var_2 ; IT WORKS BUT
NEED THIS :(
leave
ret
8
;=====================================

;===================================
proc1
uiui,var_1

sub esp,8
%define bobu ebp-4
%define
mylocal ebp-(4+4) ; ---- > mylocal redefined here again !

ccall
wsprintfA,buffer,"%d", [var_1] ;<-- nasm gives error here
call
MessageBoxA ,NULL,buffer, "title 2" ,MB_OK

lea
ebx,[mylocal] ;<--- nasm gives error here !!
ccall
wsprintfA,ebx,"%d", [var_1] ;<-- nasm gives error here
call
MessageBoxA ,NULL,ebx, "title 2" ,MB_OK
%undef var_1 ; IT
WORKS BUT NEED THIS :(
leave
ret
4
;====================================

why
hard core does not need
%undef and here with macro needs
?

THEN ....

tried to make a macro to make
the %UNDEF thing
automatic at the END ...

And
the variables stays local. But you can't do:

PROC nombre,
par1, par2
LOCALS
_DWORD var1
_buffer buffer,
256
ENDLOCALS

... código aquí

return ;
opcional
undefine par1, par2, var1,
buffer
ENDP

whee "undefine" is a macro that
does

%undef par1
%undef par2
%undef
var1
%undef buffer

NASM does not recognizes defined
names as macros parameters :(

How this problem could be
solved?

is this a bug?
can you CHANGE NASM
?

thanks !

Discussion

  • Nobody/Anonymous

    Logged In: NO

    Can you please post a simple test case?

     
  • Nobody/Anonymous

    Logged In: NO

    ;; here is test code win32
    Nasm
    ;;=======================
    import
    MessageBoxA user32.dll
    ;;===========
    ;;macros for call

    ;;===========
    %macro stdcall 2-*
    %define _j %1

    %rep %0-1
    %rotate -1
    push dword %1
    %endrep
    extern
    _j
    call [_j]
    %endmacro

    %macro invoke 2-*
    %define _j
    %1
    %rep %0-1
    %rotate -1
    push dword %1
    %endrep

    call _j

    %endmacro
    ;;==========================
    ;;macros
    for procedure 2
    parameters
    ;;==========================
    %macro
    funcion 3
    %1:
    %define %2 ebp+8
    %define %3
    ebp+12
    enter
    0,0
    %endmacro
    ;;=====================
    one1 db
    'text 1',0
    two1 db 'captation 1',0
    one2 db 'text 2',0
    two2 db
    'captation 2',0
    [section CODE USE32
    class=CODE]
    ..start:

    invoke
    FunctionOne,one1,two1
    invoke
    FunctionTwo,one2,two2
    stdcall ExitProcess,0

    funcion
    FunctionOne,lpText,lpCaption
    stdcall
    MessageBoxA,0,[lpText],[lpCaption],0
    %undef
    lpText
    %undef lpCaption
    leave
    ret 8

    funcion
    FunctionTwo,lpText,lpCaption
    stdcall
    MessageBoxA,0,[lpText],[lpCaption],0
    %undef
    lpText
    %undef lpCaption
    leave
    ret
    8

    ;;===============================
    ;; end test
    code
    ;;===============================

     
  • Nobody/Anonymous

    Logged In: NO

    nasmw test.asm -f obj

    :)

     
  • Nobody/Anonymous

    Logged In: NO

    I've tried your sample code as follows:

    import MessageBoxA user32.dll

    %macro stdcall 2-*
    %define _j %1
    %rep %0-1
    %rotate -1
    push dword %1
    %endrep
    extern _j
    call [_j]
    %endmacro

    %macro invoke 2-*
    %define _j %1
    %rep %0-1
    %rotate -1
    push dword %1
    %endrep
    call _j
    %endmacro

    %macro funcion 3
    %1:
    %define %2 ebp+8
    %define %3 ebp+12
    enter 0,0
    %endmacro

    one1 db 'text 1',0
    two1 db 'captation 1',0
    one2 db 'text 2',0
    two2 db 'captation 2',0

    [section CODE USE32 class=CODE]
    ..start:

    invoke FunctionOne,one1,two1
    invoke FunctionTwo,one2,two2
    stdcall ExitProcess,0

    funcion FunctionOne,lpText,lpCaption
    stdcall MessageBoxA,0,[lpText],[lpCaption],0
    %undef lpText
    %undef lpCaption
    leave
    ret 8

    funcion FunctionTwo,lpText,lpCaption
    stdcall MessageBoxA,0,[lpText],[lpCaption],0
    %undef lpText
    %undef lpCaption
    leave
    ret 8

     
  • Nobody/Anonymous

    Logged In: NO

    c:\temp\nasm_0_98_35>nasm -l z.lst z.asm -f obj

     
  • Nobody/Anonymous

    Logged In: NO

    With that, I'm getting no warnings or errors:

    1 import MessageBoxA
    user32.dll
    2
    3 %macro stdcall 2-*
    4 %define _j %1
    5 %rep %0-1
    6 %rotate -1
    7 push dword %1
    8 %endrep
    9 extern _j
    10 call [_j]
    11 %endmacro
    12
    13 %macro invoke 2-*
    14 %define _j %1
    15 %rep %0-1
    16 %rotate -1
    17 push dword %1
    18 %endrep
    19 call _j
    20 %endmacro
    21
    22 %macro funcion 3
    23 %1:
    24 %define %2 ebp+8
    25 %define %3 ebp+12
    26 enter 0,0
    27 %endmacro
    28
    29 00000000 74657874203100 one1 db 'text 1',0
    30 00000007 636170746174696F6E- two1 db 'captation
    1',0
    31 00000010 203100
    32 00000013 74657874203200 one2 db 'text 2',0
    33 0000001A 636170746174696F6E- two2 db 'captation
    2',0
    34 00000023 203200
    35
    36 [section CODE USE32
    class=CODE]
    37 ..start:
    38
    39 invoke
    FunctionOne,one1,two1
    40 <1> %define _j %1
    41 <1> %rep %0-1
    42 <1> %rotate -1
    43 <1> push dword %1
    44 <1> %endrep
    45 <2> %rotate -1
    46 00000000 68[07000000] <2> push dword %1
    47 <2> %rotate -1
    48 00000005 68[00000000] <2> push dword %1
    49 0000000A E81A000000 <1> call _j
    50 invoke
    FunctionTwo,one2,two2
    51 <1> %define _j %1
    52 <1> %rep %0-1
    53 <1> %rotate -1
    54 <1> push dword %1
    55 <1> %endrep
    56 <2> %rotate -1
    57 0000000F 68[1A000000] <2> push dword %1
    58 <2> %rotate -1
    59 00000014 68[13000000] <2> push dword %1
    60 00000019 E829000000 <1> call _j
    61 stdcall
    ExitProcess,0
    62 <1> %define _j %1
    63 <1> %rep %0-1
    64 <1> %rotate -1
    65 <1> push dword %1
    66 <1> %endrep
    67 <2> %rotate -1
    68 0000001E 6800000000 <2> push dword %1
    69 <1> extern _j
    70 00000023 FF15[00000000] <1> call [_j]
    71
    72 funcion
    FunctionOne,lpText,lpCaption
    73 <1> %1:
    74 <1> %define %2 ebp+8
    75 <1> %define %3 ebp+12
    76 00000029 C8000000 <1> enter 0,0
    77 stdcall
    MessageBoxA,0,[lpText],[lpCaption],0
    78 <1> %define _j %1
    79 <1> %rep %0-1
    80 <1> %rotate -1
    81 <1> push dword %1
    82 <1> %endrep
    83 <2> %rotate -1
    84 0000002D 6800000000 <2> push dword %1
    85 <2> %rotate -1
    86 00000032 FF750C <2> push dword %1
    87 <2> %rotate -1
    88 00000035 FF7508 <2> push dword %1
    89 <2> %rotate -1
    90 00000038 6800000000 <2> push dword %1
    91 <1> extern _j
    92 0000003D FF15[00000000] <1> call [_j]
    93 %undef lpText
    94 %undef lpCaption
    95 00000043 C9 leave
    96 00000044 C20800 ret 8
    97
    98 funcion
    FunctionTwo,lpText,lpCaption
    99 <1> %1:
    100 <1> %define %2 ebp+8
    101 <1> %define %3 ebp+12
    102 00000047 C8000000 <1> enter 0,0
    103 stdcall
    MessageBoxA,0,[lpText],[lpCaption],0
    104 <1> %define _j %1
    105 <1> %rep %0-1
    106 <1> %rotate -1
    107 <1> push dword %1
    108 <1> %endrep
    109 <2> %rotate -1
    110 0000004B 6800000000 <2> push dword %1
    111 <2> %rotate -1
    112 00000050 FF750C <2> push dword %1
    113 <2> %rotate -1
    114 00000053 FF7508 <2> push dword %1
    115 <2> %rotate -1
    116 00000056 6800000000 <2> push dword %1
    117 <1> extern _j
    118 0000005B FF15[00000000] <1> call [_j]
    119 %undef lpText
    120 %undef lpCaption
    121 00000061 C9 leave
    122 00000062 C20800 ret 8

     
  • Nobody/Anonymous

    Logged In: NO

    the problem is when we try to make a macro to %undef the
    %defined
    parameters of the function , the nasm does not work!

    we can not
    make a macro to %undef parameter %defined inside
    a macro
    ...

    :(

    PedroGC

     
  • Nobody/Anonymous

    Logged In: NO

    That behavior is expected because single-line
    macros take precedence over multi-line macros.

    When you invoke

    funcion FunctionOne,lpText,lpCaption

    NASM defines two single-line macros named lpText
    and lpCaption, which will expand to "ebp+8" and
    "ebp+12", respectively.

    Then, when you invoke

    stdcall MessageBoxA,0,[lpText],[lpCaption],0

    NASM expands lpText and lpCaption, and hands the
    resulting "[ebp+8]" and "[ebp+12]" to your multi-
    line macro, as %3 and %4. Obviously you can not
    feed those expanded values to %UNDEF -- it needs
    a macro name as its argument, not a macro value.

    I am not aware of a means that would allow you
    to suppress the expansion of single-line macros.

    Even if such a means would exist, it would not
    help you with this piece of code. After all you
    do feed the _value_ of %2, %3, %4, and %5 to PUSH,
    but at the same time you'd like to feed the _name_
    of %2 and %3 to %UNDEF -- you can't have both.

     
  • Nobody/Anonymous

    Logged In: NO

    well all it begans becouse if we do code with no macros
    we can use the
    same names var or parameters every were in other functions
    (x1,....,x)

    push 20
    push 10
    call function1

    push
    30
    push 40
    call function1

    function1:
    %define x1
    ebp+8
    %define x2 ebp+12
    enter 0,0
    %define x3 ebp-
    4
    %define x4 ebp-4+4
    sub esp,8
    ;code

    ret
    8

    function2:
    %define x1 ebp+8
    %define x2
    ebp+12
    enter 0,0
    %define x3 ebp-4
    %define x4 ebp-
    4+4
    sub esp,8
    ;code
    ret 8

    well this code works !!!
    and then with macros do not !! :(

    its bad :(

    any way i have a
    solution already to by pass this look it
    here

    http://forum.cjb.net:81/cgi-
    bin/forum.cgi?forum=nagoa&thread=Dh0sBKE0FNMHtqd9

    http://forum.cjb.net:81/cgi-
    bin/forum.cgi?forum=nagoa

    PedroGC

     
  • Nobody/Anonymous

    Logged In: NO

    Again, NASM behaves as expected. This is not a bug.

     
  • Nickolay Yurchenko

    Logged In: YES
    user_id=806493

    What do you think if we'll have something to prevent
    identifier expansion?
    Something like this: %<identifier>. Such identifier will be
    unbraced, but will not be expanded.
    This would be useful to pass parameters into macro 'by
    reference'.

     
  • Nobody/Anonymous

    Logged In: NO

    I have implemented 4 new directives. These directives are
    modified versions of %define. These variants will only expand
    one level. Ie. it will only expand to what's contained in the
    variable/define and no further. Parameters are not allowed.

    Also, I have added the fuctionality of concat strings on top
    of just Id's and numbers. If both items catenated are strings,
    they remain as string, otherwise it sets it to the appropriate
    type. This is also usable with %+

    The new directives are:
    %ydefine (like %define, but only expands 1 level)
    %iydefine (like %idefine, but only expands 1 level)
    %zdefine (like %xdefine, but only expands 1 level)
    %izdefine (like %ixdefine, but only expands 1 level)

    now you can do
    %macro mac1 1
    %push st1
    %zdefine %$smac_name %1
    %xdefine %1 1024
    %endmacro

    %macro mac2 0
    %undef '%'$smac_name
    %pop
    %endmacro

    mac1 myvar
    myvar
    mac2
    myvar

    will result in:
    1024
    myvar

    the quotes in the %undef is to force an expansion of
    %$smac_name.

    This isn't exactly what the user wanted, but it can be used
    to achieve the same results.

    Let me know if anyone wants this code or how I may include
    it in the distribution if that's possible.

    cleo@wt-%20esolutions.com (remove the %20)
    email may only be valid for the next couple days.

    Just reply here if it's not available.

     
  • nasm64developer

    nasm64developer - 2003-10-24

    Logged In: YES
    user_id=804543

    Check SF #829879 for my solution (%#).

     
  • nasm64developer

    nasm64developer - 2004-02-13
    • priority: 5 --> 1
     

Log in to post a comment.