Assembly Language & x86 Low-Level Systems Programming · 课时

调用约定:cdecl、stdcall 与 System V

比较主要的 x86 调用约定,确保汇编代码与 C 代码在参数传递位置、栈清理责任及需保留的寄存器方面保持一致。

第 4 / 4 课13 个步骤

调用约定:cdecl、stdcall 与 System V 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Conventions Matter

For assembly and C to interoperate, both sides must agree on a calling convention: how arguments are passed, who cleans the stack, and which registers must be preserved.

Argument Passing

Conventions differ on where arguments go: on the stack, in registers, or a mix. Getting this wrong corrupts data silently.

cdecl

cdecl (32-bit, the C default) pushes arguments on the stack right to left, and the caller cleans them up after the call.

push arg2
push arg1
call func
add esp, 8     ; caller cleans

stdcall

stdcall also passes on the stack right to left, but the callee cleans the stack via ret n. The Win32 API uses it.

ret 8          ; callee cleans 8 bytes

Caller vs Callee Cleanup

The key contrast:

  • cdecl: caller cleans, supports variadic functions
  • stdcall: callee cleans, smaller call sites

System V AMD64

The 64-bit Linux/macOS convention passes the first six integer args in registers: RDI, RSI, RDX, RCX, R8, R9; extras go on the stack.

mov rdi, arg1
mov rsi, arg2
call func

Microsoft x64

64-bit Windows uses a different register set: RCX, RDX, R8, R9 for the first four args, plus 32 bytes of shadow space.

Return Values

Integer and pointer results come back in EAX (32-bit) or RAX (64-bit); floating point uses XMM0 in 64-bit conventions.

Preserved vs Scratch Registers

Each convention designates callee-saved registers (the callee must restore them) and caller-saved ones (free to clobber). Violating this corrupts the caller.

Stack Alignment

System V requires the stack to be 16-byte aligned at a call. Forgetting this crashes code that uses SSE instructions.

Matching the Compiler

Always declare functions on the C side with the matching convention attribute (e.g. __stdcall) so both halves agree.

Quick Check

Test your understanding of calling conventions.

Recap

You learned calling conventions:

  • cdecl: stack args, caller cleans, supports varargs
  • stdcall: stack args, callee cleans
  • System V x64: args in RDI,RSI,RDX,RCX,R8,R9
  • Return in EAX/RAX; respect callee-saved registers and alignment
免费开始

用 AI 导师学习 Assembly — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「调用约定:cdecl、stdcall 与 System V」课时是免费的吗?

是的 — 「调用约定:cdecl、stdcall 与 System V」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

「调用约定:cdecl、stdcall 与 System V」这节课中我会学到什么?

比较主要的 x86 调用约定,确保汇编代码与 C 代码在参数传递位置、栈清理责任及需保留的寄存器方面保持一致。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「调用约定:cdecl、stdcall 与 System V」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?

能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从 C 调用汇编
  2. 从汇编调用 C
  3. 混合语言编程技术
  4. 调用约定:cdecl、stdcall 与 System V
← 返回 Assembly Language & x86 Low-Level Systems Programming