Author Topic: Preserve registers with macros  (Read 11532 times)

Offline kanvuanza

  • Jr. Member
  • *
  • Posts: 3
  • Country: 00
Preserve registers with macros
« 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?
« Last Edit: June 19, 2012, 07:34:23 AM by kanvuanza »
You'll wish that you had done some of the hard things when they were easier to do.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Preserve registers with macros
« Reply #1 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.

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Preserve registers with macros
« Reply #2 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.

Offline kanvuanza

  • Jr. Member
  • *
  • Posts: 3
  • Country: 00
Re: Preserve registers with macros
« Reply #3 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}.
You'll wish that you had done some of the hard things when they were easier to do.