Home >Backend Development >C++ >Why Does This Assembly Code Include an Initial `push rax` Before a Function Call?

Why Does This Assembly Code Include an Initial `push rax` Before a Function Call?

DDD
DDDOriginal
2024-12-15 18:55:10190browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn