Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function parameters transferred in registers on 64bit OS?

I am reading one of Agner Fog's manuals and as an advantage for 64 bit Operating Systems (over 32 bit) he says:

Function parameters are transferred in registers rather than on the stack. This makes function calls more efficient.

Is he saying the stack is not used for passing function parameters (64bit OS) at all???

like image 426
user997112 Avatar asked Jul 02 '13 23:07

user997112


People also ask

How are parameters passed in x64?

Parameter passingBy default, the x64 calling convention passes the first four arguments to a function in registers. The registers used for these arguments depend on the position and type of the argument. Remaining arguments get pushed on the stack in right-to-left order.

Are parameters stored in registers?

In x86-32 assembly, parameters are stored on the stack but in x86-64, parameters stored in registers.

What 64 bit register will hold the return value of a function?

6.4. The global registers and the output registers can be used to hold 64-bit integer values, but the input registers and the local registers can only be used to hold 32-bit values in the lower half of the register.

Which registers are used for parameters?

Argument registers (X0-X7) These are used to pass parameters to a function and to return a result. They can be used as scratch registers or as caller-saved register variables that can hold intermediate values within a function, between calls to other functions.


1 Answers

Yes, that's what he's saying, but it's not quite accurate. The stack may be used, but only if your function has a lot of parameters (or you write code that forces a spill).

If you check out the wikipedia list of 64-bit intel calling conventions, you'll see that registers are used to pass the first several parameters. There are two major 64-bit Intel calling conventions. For the Microsoft ABI:

The Microsoft x64 calling convention uses registers RCX, RDX, R8, R9 for the first four integer or pointer arguments (in that order left to right), and XMM0, XMM1, XMM2, XMM3 are used for floating point arguments. Additional arguments are pushed onto the stack (right to left). Integer return values (similar to x86) are returned in RAX if 64 bits or less. Floating point return values are returned in XMM0. Parameters less than 64 bits long are not zero extended; the high bits contain garbage.

And the System V ABI:

The first six integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX, R8, and R9, while XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7 are used for floating point arguments ... As in the Microsoft x64 calling convention, additional arguments are passed on the stack and the return value is stored in RAX.

like image 154
Carl Norum Avatar answered Oct 16 '22 15:10

Carl Norum