flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > ESP-based Proc macros

Author
Thread Post new topic Reply to topic
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight 15 Dec 2008, 06:21
Just wondering, has anyone more experienced in FASM macros created a set of PROC/ENDP macros that uses ESP to access the registers rather than EBP? I switched over from NASM recently but I am unable to bring some of my macros over with me. The following are for NASM (but the latest versions are breaking my macros to pieces).

Code:
%imacro Proc 1.nolist
  %push Procedure$$
  %assign %$Proc.ParamOffset 4
  %assign %$Proc.LocalOffset 0
  %assign %$Proc.StackOffset 0
  %assign %$Proc.RegsOffset  0
  %assign %$Proc.RegsAlloced 0
%1:
%endmacro

%imacro EndProc 1.nolist
  %ifndef %$Proc.StackOffset
    %error "'EndProc' used without a preceding Proc!"
  %else
    %pop
  %endif
    align 4 
%endmacro

%imacro Param 1-*.nolist
  %ifndef %$Proc.ParamOffset
    %error "'Param' is not in a Procedure Context!"
  %else
    %assign %$Proc.LocalSave %$Proc.LocalOffset
    %undef %$Proc.LocalOffset
    %rep %0
      %ifnnum %1
        %assign %$Proc.StackSave %$Proc.StackOffset
        %undef %$Proc.StackOffset
        %xdefine %$%1 esp+%$Proc.LocalOffset+%$Proc.ParamOffset+%$Proc.StackOffset
        %define %1 %$%1
        %assign %$Proc.StackOffset %$Proc.StackSave
        %ifnum %2
          %assign %$Proc.ParamOffset %$Proc.ParamOffset+%2
        %else
          %assign %$Proc.ParamOffset (%$Proc.ParamOffset+7)&0FFFFFFFCh
        %endif
      %endif
      %rotate 1
    %endrep
    %assign %$Proc.LocalOffset %$Proc.LocalSave
  %endif
%endmacro

%imacro Local 1-*.nolist
  %ifndef %$Proc.ParamOffset
    %error "'Local' is not in a Procedure Context!"
  %else
    %rep %0
      %ifnnum %1
        %assign %$Proc.StackSave %$Proc.StackOffset
        %undef %$Proc.StackOffset
        %xdefine %$%1 esp+%$Proc.LocalOffset+%$Proc.StackOffset
        %define %1 %$%1
        %assign %$Proc.StackOffset %$Proc.StackSave
        %ifnum %2
          %assign %$Proc.LocalOffset %$Proc.LocalOffset+%2
        %else
          %assign %$Proc.LocalOffset (%$Proc.LocalOffset+7)&0FFFFFFFCh
        %endif
      %endif
      %rotate 1
    %endrep
  %endif
%endmacro

%define ParamSpace$$    %$Proc.ParamOffset-4
%define LocalSpace$$    %$Proc.LocalOffset
%define EDI$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+0]
%define ESI$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+4]
%define EBP$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+8]
%define ESP$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+12]
%define EBX$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+16]
%define EDX$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+20]
%define ECX$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+24]
%define EAX$$           [esp+%$Proc.StackOffset-%$Proc.RegsOffset+28]

;--------------------------------------------------------------------------
%macro push 0-*.nolist
  %rep %0
    %ifidn %1,all
      pushad
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset+32
        %if (%$Proc.RegsAlloced == 0)
          %assign %$Proc.RegsOffset  %$Proc.StackOffset
          %assign %$Proc.RegsAlloced 1
        %endif
      %endif
    %elifidn %1,flags
      pushfd
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset+4
      %endif
    %else
      push %1
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset+4
      %endif
    %endif
    %rotate 1
  %endrep
%endmacro

%macro pop 0-*.nolist
  %rep %0
    %ifidn %1,all
      popad
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset-32
      %endif
    %elifidn %1,flags
      popfd
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset-4
      %endif
    %else
      pop %1
      %ifdef %$Proc.StackOffset
        %assign %$Proc.StackOffset %$Proc.StackOffset-4
      %endif
    %endif
    %rotate 1
  %endrep
%endmacro
    



The way you would use this is like this:

Code:
Proc TestProc
Param Param1, Param2, 8, Param3
Local Local1, Local2, 16
        sub esp,LocalSpace$$
        push all

        ; ... <-- do what you want here
        mov eax,[Param3] ;<-- like this

        mov [EAX$$],eax ; stores eax onto the stack for returning to caller
        mov [EDX$$],edx ; stores edx onto the stack for returning to caller

        pop all
        add esp,LocalSpace$$
        ret ParamCount$$
EndProc
    


Param and Local defaults to 4 bytes data size unless the size follows the variable being declared

LocalSpace$$ is the size of the space allocated for local variables, while ParamCount$$ is the number of bytes to pop off the stack when returning to the caller.

I tend to use all registers as much as possible, so I save all the registers upon entry and restore them all upon exit. EAX$$ and other macros are simply placeholders on the stack, so that when one does a pop all prior to returning to the caller, the return variables will be popped into their respective registers.

Sorry if this seems like a bit much, I have been working on FASM now for the past four months and have a lot of my code ported over but at this time being they use the EBP-based proc macro. I've tried many different ways to write the Proc macro for FASM but to no avail, so I am asking ya'll for help.

Thanks! Very Happy
Post 15 Dec 2008, 06:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 15 Dec 2008, 10:03
Yes, there have been many versions posted. Some also from myself. But in general the problems outweigh the benefits IMO. However, if you have a specific requirement and are prepared to follow a few rules with stack usage and algorithm expression then ESP based procs can be beneficial. Probably the most obvious case is leaf routines with no fancy stack usage within.
Post 15 Dec 2008, 10:03
View user's profile Send private message Visit poster's website Reply with quote
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight 15 Dec 2008, 18:36
Hmmmmm I'd wondered. I spent time searching this forum before asking but search engine came up with a bunch of posts having nothing to do with any of the keywords I chose to use. *GRRRRRRRRRRR* Any quick links so I can see what has been done and see what was done that I might not have thought of?

There is one option left, and that's using a structure. It ain't pretty, but it gets the job done (mentally, that is).

Btw, what's the leaf routines you are talking about?
Post 15 Dec 2008, 18:36
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
bitRAKE 15 Dec 2008, 20:29
IIRC, there are some posts in the macro section of the board.

A quick search for ESP yielded:
http://board.flatassembler.net/topic.php?t=5938
http://board.flatassembler.net/topic.php?t=7764
Post 15 Dec 2008, 20:29
View user's profile Send private message Visit poster's website Reply with quote
Mercury Knight



Joined: 09 Nov 2008
Posts: 15
Mercury Knight 16 Dec 2008, 03:36
thanks bitRAKE! Very Happy
Post 16 Dec 2008, 03:36
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 16 Dec 2008, 04:56
Mercury Knight wrote:
Btw, what's the leaf routines you are talking about?
Leaf routines are those that do not call any other routines. The name is taken from the analogy with a tree: trunk, branches and leaves. The leaves are the last in the hierarchy.
Post 16 Dec 2008, 04:56
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.