Author Topic: Which ABI does it use when compiling on a x86-64 processor?  (Read 4409 times)

Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Which ABI does it use when compiling on a x86-64 processor?
« on: January 15, 2013, 06:24:07 AM »
Hello I have a question based on this Wikipedia article.

Based on that table, it seems do indicate that, if the architecture is x86-64 then the ABI that is used for the calling convention is either System V or Windows Native (depending on OS).

Is this true regardless of BITS 32 or BITS 64?  Or would it use one of the old ABI's if it's in BITS 32 mode on an x86-64 architecture? (e.g. cdecl, fastcall, stdcall, thiscall)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2437
  • Country: us
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #1 on: January 15, 2013, 08:02:04 AM »
Hmmm... seems to me that if you were writing 32-bit code, you'd have a tough time using a 64-bit ABI - the registers aren't available. Despite the fact that the article says "if the architecture...", it seems to me to be an exclusively software issue. Whatever software you hope to interface with, that's the ABI you'd better use! Within the confines of your own assembly code, you're free to use any ABI... or make one up. I just invented one called "randomcall" where I pick the first register out of my err... ear and use it to pass a parameter. :) Smarter to be consistent, and use the same interface internally as you're (pretty much) forced to when interfacing with external code, but neither Nasm nor the hardware forces you to. The hardware imposes some limitations of course - you can't push a 32-bit register in 64-bit mode (long mode) or vice-versa. But if you were running 32-bit Windows on a 64-bit machine (something many people did in the early days of 64-bit hardware - early 64-bit Windows was buggy, I'm told) it certainly wouldn't alter the ABI. Match what you're interfacing with...

I'm not sure if I'm answering your question or some other question... What does "it" refer to?

Best,
Frank


Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #2 on: January 15, 2013, 05:23:25 PM »
To add on to what Frank said since you mentioned compiling:

You can certainly assemble/compile a 32-bit program on a 64-bit computer.  You can also assemble/compile a 64-bit program on a 32-bit machine.  However, you cannot execute the 64-bit program within a 32-bit operating system, but you can indeed execute a 32-bit program on a 64-bit OS due to backward compatibility.

The x64 ABI really has only two major conventions of interest - one is Windows and the other is UNIX ( as currently implemented by the BSD's and Linux ).  Of course, if you're writing your own OS you're free to also write your own conventions but that also means writing all the tool-chain support ( compiler, linker, debugger, etc. ).


Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #3 on: January 15, 2013, 05:58:38 PM »
Thanks, guys. I believe you answered my question. I got confused by that table and thought perhaps all code (regardless if it was 32-bit or 64-bit) would have to use the "System V" or "Microsoft x64" conventions, if running on a x86-64 architecture.  But now I know that's not true.  I appreciate the thorough responses.

Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #4 on: January 18, 2013, 04:51:53 AM »
So... just to confirm...

If you were writing a 64-bit Windows program on x64 architecture, you could actually use the System V convention internally -- even though it's running on Windows?

(As long as you used the Microsoft x64 convention whenever you made a call to a function in the Windows libraries)

Right?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2437
  • Country: us
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #5 on: January 18, 2013, 05:23:46 AM »
Seems right to me, yeah.

Best,
Frank


Offline hhh3h

  • Jr. Member
  • *
  • Posts: 41
Re: Which ABI does it use when compiling on a x86-64 processor?
« Reply #6 on: January 19, 2013, 06:52:42 PM »
Thank you sir