I just started to learn a bit assembler from compiler output.
test(1);
This simple function call creates following asm output (compiled with x64)
000000013FFF2E76 mov ecx,1
000000013FFF2E7B call test (13FFF33C0h)
But why isn't it:
000000013FFF2E76 push 1
000000013FFF2E7B call test (13FFF33C0h)
I thought a function parameter will be pushed
to the stack and then poped
in the function. Can somebody explain why VS prefer the top one?
It's because that's the ABI on x64 Windows.
On Windows x64, the first integer argument is passed in RCX
, the second in RDX
, the third in R8
and the fourth in R9
. The fifth and following are passed through the stack.
Because your function has a single argument, only RCX
is used.
The compiler issued a write to ECX
because all writes to 32-bit registers result in zeroing the higher part of the 64-bit register, and 32-bit immediates are obviously shorter than 64-bit ones (instruction cache anyone?).