NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: logarithm on December 18, 2012, 08:47:36 PM

Title: OSX macho 16-byte alignment issue
Post by: logarithm on December 18, 2012, 08:47:36 PM
Hello,

I've ported some code that works great in Linux and Windows with nasm.  My issue is with OSX macho-32 and getting around the 16-byte alignment issue.  Does anyone have a solution for making sure ESP is always probably aligned without manually editing their code?  Better yet is there a macro that can align esp properly for OSX?

Thank you!
-loga
Title: Re: OSX macho 16-byte alignment issue
Post by: Keith Kanios on December 19, 2012, 04:16:39 AM
Here's a macro to overload the call function with what Mac OS X expects for the CDECL calling convention:

Code: [Select]
%imacro call 1-*
 mov ebx,esp
 and esp,0xFFFFFFF0
 %if %0 > 1
  %if ((%0-1) % 4) > 0
   sub esp,16-(((%0-1) % 4) * 4)
  %endif
  %rep (%0 - 1)
   %rotate -1
   push %1
  %endrep
  %rotate -1
 %endif
 call %1
 mov esp,ebx
%endmacro

The above is "shooting from the hip" so to speak, so check it over for logic/syntax errors.
Title: Re: OSX macho 16-byte alignment issue
Post by: Frank Kotler on December 19, 2012, 05:37:24 AM
Code: [Select]
   %push %1

I don't think we want that first '%', do we?

Best,
Frank

Title: Re: OSX macho 16-byte alignment issue
Post by: Keith Kanios on December 20, 2012, 02:15:50 AM
Code: [Select]
   %push %1

I don't think we want that first '%', do we?

No. Good catch. That definitely should be push %1.