Why does printf overwrite the ECX register? [duplicate]
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.
Solution 1:
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 x86 tag wiki. There's even an FAQ section with an entry covering this question.