为什么 printf 会覆盖 ECX 寄存器? [英] Why does printf overwrite the ECX register?
问题描述
我知道printf
返回EAX 中打印的字符数.为什么 printf
将寄存器 ECX 更改为 0?我的代码片段:
I know printf
returns the number of characters printed in EAX. Why is printf
changing register ECX to 0? My code snippet:
push eax
push intFormat
call printf
add esp,8
我在 64 位 Linux 发行版上运行我的代码.
I'm running my code on a 64-bit Linux distribution.
推荐答案
正如你所知,EAX 将具有 printf
的返回值,但其后的函数CDECL 调用约定(包括 C 库)也可以破坏 ECX 和 EDX.他们可能不会改变,但他们可能并且他们的价值观不能被依赖.您必须使用不会被破坏的不同寄存器(如果不使用 PIC 代码,则 EBX 可用,ESI,EDI 也可用),或者您必须手动保留这些寄存器并在 printf<之后恢复它们/code> – 迈克尔·佩奇
As it stands EAX will have the return value from printf
as you know, but functions that follow the CDECL calling convention (the C library included) can also clobber ECX, and EDX. They may not change, but they might and their values can't be relied upon for anything. You'll have to use different registers that don't get clobbered (EBX is available if not using PIC code, ESI, EDI are also available) or you'll have to manually preserve those registers and restore them after printf
– Michael Petch
有关调用约定/ABI 的更多信息,请参阅x86 标签维基.甚至还有一个常见问题解答部分,其中包含一个涵盖此问题的条目.
For more info on calling conventions / ABIs, see the x86 tag wiki. There's even an FAQ section with an entry covering this question.
这篇关于为什么 printf 会覆盖 ECX 寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!