为什么printf会覆盖ECX寄存器? [英] Why does printf overwrite the ECX register?

查看:215
本文介绍了为什么printf会覆盖ECX寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道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之后还原它们– Michael提取

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的更多信息,请参见标签Wiki.甚至还有一个FAQ部分,其中包含有关此问题的条目.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆