Author Topic: OSX macho 16-byte alignment issue  (Read 12462 times)

Offline logarithm

  • New Member
  • Posts: 1
OSX macho 16-byte alignment issue
« 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

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OSX macho 16-byte alignment issue
« Reply #1 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.
« Last Edit: December 20, 2012, 02:17:11 AM by Keith Kanios »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: OSX macho 16-byte alignment issue
« Reply #2 on: December 19, 2012, 05:37:24 AM »
Code: [Select]
   %push %1

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

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: OSX macho 16-byte alignment issue
« Reply #3 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.