Why does printf overwrite the ECX register?

2019-01-29 01:27发布

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

I'm running my code on a 64-bit Linux distribution.

1条回答
干净又极端
2楼-- · 2019-01-29 01:40

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

For more info on calling conventions / ABIs, see the tag wiki. There's even an FAQ section with an entry covering this question.

查看更多
登录 后发表回答