1

I am currently reading a book about assembly for x86_64 arch. I am using extern “c” so I am calling a assembly from cpp. The guy is coding in visual studio and he says that cpp is loading the arguments of the function in the eax, edx, R8D, R9D registers. This is the function I am working on

extern “C” integer_add(int a, int b, int c, int d);
// some movs, adds and subs go on in the assembly instructions

My question is: can I assume that cpp loads the parameters in these registers or not? He is using MASM, which I think is for Windows (I have worked with NASM so far, I am on mac in case it means something) and if not, how do I know where I can find my parameters. Technically speaking coming from higher level languages I guess that all this should happen in the stack but he does not seem to push or pop anything. This is what I have found on wikipedia RDI, RSI, RDX, RCX, R8, R9, [XYZ]MM0–7so these seem to be the registers which gcc, Intel C++ linux and MACOS use. Does the order matter, so the first parameter is loaded into rdi, the second in rsi and so on...? thank you for your help

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 3
    https://github.com/cirosantilli/x86-assembly-cheat/blob/master/x86-64/calling-convention.md – Alan Birtles Dec 17 '21 at 20:22
  • 2
    MAC OS uses the x86-64 System V ABI which is different from the 64-bit MS Windows ABI.More on the 64-bit System V ABI can be found here: https://stackoverflow.com/a/40348010/3857942 – Michael Petch Dec 17 '21 at 20:24
  • 1
    _I guess that all this should happen in the stack_ Well, not if you're passing parameters in registers... _Does the order matter?_ Absolutely! Otherwise, how is the called function going to know which parameter is which? Apart from all of which, @AlanBirtles comment should tell you everything you need to know. – Paul Sanders Dec 17 '21 at 20:27
  • thanks everyone for the help and the links – not_here_to_play Dec 17 '21 at 21:01

1 Answers1

1

MacOSX follows the SystemV ABI. This differs from Visual Studio/Windows.

Parameters are definitely not passed on the stack if there are registers available. You're right that the SystemV ABI uses RDI, RSI, RDX, RCX for your 4 parameters - we could fit 2 more. The order of course matters; the caller and callee need to agree.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Parameters can be passed on the stack. Any integer class parameters beyond the first 4 will be on the stack. Structures passed by value may have to be put on the stack as well (there are rules that govern this in the ABI) – Michael Petch Dec 17 '21 at 20:30
  • Actually the first 6 integer class go in registers (R8 and R9 after RCX) and only those after 6 go on the stack – Chris Dodd Dec 17 '21 at 21:16
  • @AlanBirtles: Correct; my bad. I mixed a general statement and the specific case here. Fixed. – MSalters Dec 17 '21 at 22:26
  • 1
    4 was a typo, and I didn't get into the whole floating point situation – Michael Petch Dec 17 '21 at 23:44