Home >Backend Development >C++ >Why Does This Assembly Code Include an Initial `push rax` Before a Function Call?
Why the initial RAX Push in this Function Call Assembly?
In the assembly output of the provided C code snippet, the function f exhibits an initial push of the RAX register to the stack.
Rationale
The x64 ABI mandates that the stack alignment be 16 bytes before a call instruction. Since call pushes an 8-byte return address onto the stack, disrupting the alignment, the compiler inserts a push operation to maintain alignment.
This specific choice of pushing a dummy value is optimized for processors equipped with a stack engine, which can execute the push instruction more efficiently compared to sub rsp, 8.
In contrast, the g function, which performs a simple function call without the std::function wrapper, doesn't require stack alignment adjustment and thus doesn't execute this initial push.
The above is the detailed content of Why Does This Assembly Code Include an Initial `push rax` Before a Function Call?. For more information, please follow other related articles on the PHP Chinese website!