Question

Is there any way to generate the following header and footer for routines in nasm without the need to explicitly code them every time I define a new routine?

Header:

  push  ebp
  mov   ebp, esp    ; Entry code - set up ebp and esp

body of the routine

Footer:

  mov   esp, ebp    ; Function exit code
  pop   ebp
  ret

I'm looking for something like:

some command that generates entry code
body of the routine
some command that generates exit code
Was it helpful?

Solution

You could define a multi-line macro...

%macro  prologue 1 

        push    ebp 
        mov     ebp,esp 
        sub     esp,%1 

%endmacro

Source.

You could define one for function entry and exit.

%macro  prologue 1 

        push    ebp 
        mov     ebp,esp 
        sub     esp,%1 

%endmacro

%macro  epilogue 1 

        mov     esp,ebp
        pop     ebp 
        ret

%endmacro

The argument supplied is how much room you'd like to reserve on the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top