# X86 调用约定

x86 调用约定是一组规则，规定一个函数（调用方）如何向另一个函数（被调用方）传递参数、双方各自可以自由修改哪些寄存器、返回值放在哪里，以及由谁清理栈。x86 家族并没有单一的约定：32 位 x86 积累了若干相互竞争的约定，而 64 位 x86 则整合为两种主导约定——Windows 上的 Microsoft x64 约定，以及 Linux、BSD 和 macOS 上的 System V AMD64 约定。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup><sup> • </sup><sup>[2](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)</sup>

| 关键事实 | 细节 |
|---|---|
| Microsoft x64 默认 | 四寄存器快速调用约定：整数参数放在 RCX、RDX、R8、R9；浮点参数放在 XMM0 至 XMM3；第五个及之后的参数放在栈上<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup> |
| System V AMD64 默认 | 前六个整数参数放在 RDI、RSI、RDX、RCX、R8、R9；浮点和向量参数放在 XMM0 至 XMM7<sup>[2](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)</sup> |
| 红区 | System V 在 RSP 之下保留 128 字节供叶子函数存放临时数据；Linux 内核不承认这一区域<sup>[2](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)</sup> |
| Linux 系统调用 | 六个参数放在 RDI、RSI、RDX、R10、R8、R9，系统调用号放在 RAX；syscall 指令会破坏 RCX 和 R11<sup>[3](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/kernel.tex?ref_type=heads)</sup> |
| 32 位 x86 关键字 | __cdecl（调用方清理栈）、__stdcall（被调用方清理）、__fastcall（前两个 DWORD 参数放在 ECX、EDX）、__thiscall（this 放在 ECX）<sup>[4](https://learn.microsoft.com/en-us/cpp/cpp/cdecl?view=msvc-170)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=vs-2019)</sup><sup> • </sup><sup>[6](https://learn.microsoft.com/en-us/cpp/cpp/fastcall?view=msvc-160)</sup><sup> • </sup><sup>[7](https://learn.microsoft.com/en-us/cpp/cpp/thiscall?view=vs-2019)</sup> |
| 跨 ABI 编译 | GCC 和 Clang 接受 ms_abi 和 sysv_abi 函数属性，以便按函数选择 Microsoft 或 System V 约定<sup>[8](https://gcc.gnu.org/onlinedocs/gcc/x86-Attributes.html)</sup> |

## 两种 64 位约定

**Microsoft x64。** Windows 上的 x64 应用二进制接口默认采用四寄存器快速调用约定。参数与寄存器之间存在严格的一一对应关系：大小超过 8 字节、或者大小不是 1、2、4、8 字节的参数，必须通过引用传递，并且单个参数绝不会分散到多个寄存器中。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup> 调用方必须始终为四个寄存器参数分配栈空间，即使被调用方使用的参数更少；这一影子存储简化了对无原型 C 函数和可变参数函数的支持。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup> x87 寄存器栈不被使用；所有浮点运算都使用 16 个 XMM 寄存器。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup>

**System V AMD64。** 在 Linux、BSD 和 macOS 上，System V AMD64 psABI 将前六个整数类参数放在 RDI、RSI、RDX、RCX、R8 和 R9 中，将浮点参数放在 XMM0 至 XMM7 中。<sup>[2](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)</sup> 参数被分类为 INTEGER、SSE、MEMORY 等类别；大于四个八字节的聚合体，或者包含未对齐字段的聚合体，通过内存传递。<sup>[9](https://refspecs.linuxfoundation.org/elf/x86_64-abi-0.99.pdf)</sup> 就在执行 call 指令之前，栈必须按 16 字节对齐，而 RSP 之下 128 字节的区域（即红区）可供叶子函数存放临时数据，无需调整栈指针。<sup>[2](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)</sup>

两种约定在寄存器分配、聚合体的分类方式以及哪些寄存器需要保留上都有所不同。在 Windows x64 上，RAX、RCX、RDX、R8、R9、R10、R11 以及 XMM0 至 XMM5 是易失的，而 RBX、RBP、RDI、RSI、RSP、R12 至 R15 以及 XMM6 至 XMM15 是非易失的，任何使用它们的函数都必须保存。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup> 在 System V 下，RDI 和 RSI 是参数寄存器，由调用方保存；而在 Windows 上它们由被调用方保存；混合使用两种 ABI 的代码在跨越边界时必须将 RSI、RDI 和 XMM6-15 视为已被破坏。<sup>[10](https://gcc.gnu.org/onlinedocs/gcc-11.4.0/gcc/x86-Options.html)</sup>

## 返回值

在 Windows x64 上，能放进 64 位的标量返回值（包括 __m64 类型）通过 RAX 返回；非标量类型，包括 float、double 以及 __m128 等向量类型，通过 XMM0 返回。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup> 用户定义类型只有在长度为 1、2、4、8、16、32 或 64 位，并且没有用户定义的构造函数、析构函数或拷贝赋值运算符时，才能按值通过 RAX 返回；否则由调用方分配内存并传递一个指针作为第一个参数，被调用方必须将该指针在 RAX 中返回。<sup>[1](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)</sup>

在 System V 下，INTEGER 类返回值使用 RAX，然后使用 RDX；SSE 类值使用 XMM0，然后使用 XMM1；long double 通过 x87 栈在 ST0 中返回。<sup>[9](https://refspecs.linuxfoundation.org/elf/x86_64-abi-0.99.pdf)</sup> 在 32 位 x86 上，整数和指针值通过 EAX 返回，64 位值通过 EDX:EAX 对返回，标量浮点值通过 x87 栈顶 ST0 返回；即使调用方不使用该值，也必须将其弹出。<sup>[11](https://www.uclibc.org/docs/psABI-i386.pdf)</sup>

## 32 位 x86 约定

32 位 x86 架构带有若干约定，它们都遵循相同的寄存器保留规则：函数必须保留除 EAX、ECX、EDX 和 ESP 之外的每一个寄存器。<sup>[12](https://learn.microsoft.com/pl-pl/windows-hardware/drivers/debugger/x86-architecture)</sup>

- **__cdecl**，C 和 C++ 程序的默认约定，从右到左将参数压入栈，并由调用方将它们弹出；由于调用方负责清理，它支持可变参数函数。<sup>[4](https://learn.microsoft.com/en-us/cpp/cpp/cdecl?view=msvc-170)</sup>
- **__stdcall**，用于 Win32 API 函数，从右到左压入参数，但由被调用方清理栈，因此编译器会将可变参数函数改为 __cdecl。<sup>[5](https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=vs-2019)</sup>
- **__fastcall** 将前两个不大于 DWORD 的参数放在 ECX 和 EDX 中，其余参数压入栈，并由被调用方清理。<sup>[6](https://learn.microsoft.com/en-us/cpp/cpp/fastcall?view=msvc-160)</sup>
- **__thiscall**，x86 上非可变参数 C++ 成员函数的默认约定，将 this 指针放在 ECX 中，其余参数压入栈，由被调用方清理。<sup>[7](https://learn.microsoft.com/en-us/cpp/cpp/thiscall?view=vs-2019)</sup>
- **__vectorcall** 比 __fastcall 或默认 x64 约定使用更多寄存器传递参数，在 x64 上最多可将六个向量或浮点参数放在 XMM0 至 XMM5 中。<sup>[13](https://learn.microsoft.com/en-us/cpp/cpp/vectorcall?view=vs-2019)</sup>

在 x64 和 ARM 目标上，这些关键字会被接受但被忽略，因为这些架构默认使用基于寄存器的约定。<sup>[7](https://learn.microsoft.com/en-us/cpp/cpp/thiscall?view=vs-2019)</sup> System V i386 ABI 按相反顺序在栈上传递大多数参数，但前三个 __m64 参数放在 MM0 至 MM2 中、前三个 __m128 参数放在 XMM0 至 XMM2 中，属于例外。<sup>[11](https://www.uclibc.org/docs/psABI-i386.pdf)</sup>

## 系统调用

系统调用遵循自己的约定。在 Linux x86-64 上，用户程序将参数放在 RDI、RSI、RDX、R10、R8 和 R9 中，系统调用号放在 RAX 中；内核接口使用 R10 而不是 RCX，因为 syscall 指令本身会破坏 RCX 和 R11。系统调用最多只能有六个参数，返回值在 -4095 到 -1 之间表示出错，其中保存的是 -errno。<sup>[3](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/kernel.tex?ref_type=heads)</sup> Linux 内核不承认红区，因此内核代码必须使用 -mno-red-zone 编译。<sup>[3](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/kernel.tex?ref_type=heads)</sup>

## 混合约定

编译器提供按函数控制跨越 ABI 边界的方式。GCC 和 Clang 接受 ms_abi 属性以使用 Microsoft 约定，接受 sysv_abi 以使用 System V ELF 约定，默认情况下在 Windows 目标上使用 Microsoft，在其他地方使用 System V。<sup>[8](https://gcc.gnu.org/onlinedocs/gcc/x86-Attributes.html)</sup> Clang 还额外说明，在 x86-64 上 preserve_none 约定只保留 RSP 和 RBP，从而将 R12 至 R15、RDI、RSI 以及其他通用寄存器释放出来用于传递参数。<sup>[14](https://clang.llvm.org/docs/AttributeReference.html)</sup> 由于两种 64 位约定在哪些寄存器需要保留上存在分歧，手写汇编以及附带汇编代码的库必须针对每种 ABI 分别构建和测试；寄存器使用上的不匹配可能导致只在某一个平台上出现的崩溃或栈损坏。<sup>[15](https://github.com/cloudflare/circl/issues/7)</sup>

## References

1. [x64 calling convention, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170)
2. [x86-64 psABI, low-level system information, x86 psABIs GitLab](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/low-level-sys-info.tex)
3. [AMD64 Linux Kernel Conventions, x86-64 psABI kernel.tex](https://gitlab.com/x86-psABIs/x86-64-ABI/-/blob/master/x86-64-ABI/kernel.tex?ref_type=heads)
4. [__cdecl, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/cdecl?view=msvc-170)
5. [__stdcall, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/stdcall?view=vs-2019)
6. [__fastcall, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/fastcall?view=msvc-160)
7. [__thiscall, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/thiscall?view=vs-2019)
8. [x86 Attributes, GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/x86-Attributes.html)
9. [System V Application Binary Interface, AMD64 ABI Draft 0.99.6](https://refspecs.linuxfoundation.org/elf/x86_64-abi-0.99.pdf)
10. [x86 Options, GCC 11.4 documentation](https://gcc.gnu.org/onlinedocs/gcc-11.4.0/gcc/x86-Options.html)
11. [System V Application Binary Interface, Intel386 ABI 1.0](https://www.uclibc.org/docs/psABI-i386.pdf)
12. [x86 architecture, Windows drivers, Microsoft Learn](https://learn.microsoft.com/pl-pl/windows-hardware/drivers/debugger/x86-architecture)
13. [__vectorcall, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/cpp/vectorcall?view=vs-2019)
14. [Attributes in Clang](https://clang.llvm.org/docs/AttributeReference.html)
15. [ABI testing, cloudflare/circl issue #7](https://github.com/cloudflare/circl/issues/7)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Instruction set architectures › x86 and x86-64

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/x86-calling-conventions

English original: https://www.edgepedia.org/x86-calling-conventions
