NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: kanvuanza on June 19, 2012, 07:31:12 AM

Title: Preserve registers with macros
Post by: kanvuanza on June 19, 2012, 07:31:12 AM
Hi Nasmers,
I use these macros to declare a function:
Code: [Select]
%imacro PROC 2
%1:
%push %1
enter %2, 0
%endmacro

; just for markup
%imacro ENDPROC 0
%pop
%endmacro

Now I want something that "preserves" my registers, i.e. push when entering into a function, and pop them at exit. Note that my function can have multiple exit points. My wish is:
Code: [Select]
PROC some.function, 0
    PRESERVE ebx, ecx, esi
    ; blabla
    EXIT
ENDPROC

I tried the following:
Code: [Select]
; save all registers into $_regs variable, and push them all
%imacro PRESERVE 1-*
    %define $_regs
    %rep %0
        %define $_regs %+ %1
        push %1
    %rotate 1
    %endrep
%endmacro

But I don't know if this code is correct because I don't know how to verify. The EXIT macro should pop all "preserved" registers in inverse order. How can I achieve this?
Title: Re: Preserve registers with macros
Post by: Rob Neff on June 19, 2012, 03:52:17 PM
Use NASMX?  ;D

Seriously, the NASMX package already contains all the necessary macros you need for various OS calling conventions, preserving registers, etc.
If you wish to obtain more in-depth knowledge of the Nasm assembly macro capability it wouldn't hurt to study the macros contained there.
There are all kinds of little tricks you can do with Nasm macros and having access to the source of a major macro package is a huge bonus.
Of course, the nasmx.inc file is rather large, but if you concentrate on the smaller macros first you'll get a good idea of what you're getting yourself into.
Title: Re: Preserve registers with macros
Post by: Keith Kanios on June 19, 2012, 04:37:46 PM
If you wish to obtain more in-depth knowledge of the Nasm assembly macro capability it wouldn't hurt to study the macros contained there.

Also, preprocessing the file (-E) will allow you to view the end result of what NASMX does.
Title: Re: Preserve registers with macros
Post by: kanvuanza on June 19, 2012, 09:48:22 PM
NASMX looks awesome!

But I want to start with simple things. The '-e' parameter help a lot to debug macros. Now I have the following in pseudo-NASM:

Code: [Select]
%imacro PRESERVE 1-*
    %define $_regs %{1:%0}
    multipush $_regs
%endmacro

%imacro EXIT
    multipop $_regs
    leave
    ret
%endmacro

However %0 isn't expanded inside the parameter range %{1:%0}.