Re: Calling convention odd?

Qyz <questioner_x@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in part:
While doing some asm coding recently I noticed that when I called
_malloc, my ECX register was getting trashed. I did some digging
and was surprised to learn that this is normal, that callers to
C library routines are expected to save EAX, ECX, and EDX.

You ask a good, old question for which there is no solidly
settled answer. Caller- vs callee-save is contentious with
many side-effects, the biggest being function return flexibility.
As an ASMer, I chafe at restrictions.

The caller knows what regs need saving (maybe none),
the callee knows which regs it wants to use (at least one).
So I tend towards caller-saves, and call "big" fns.

I suspect EAX is saved in C because the condition code has to be
returned there. EDX for some "extended" return. ECX may then
be for some "succeeded" length of read/write/malloc/... .

Seems to me that if one is doing a lot of such calls, it is best
to not use ECX and EDX for local variables at all, lest one has
to push/pop over and over which is more expensive than storing
data on the stack.

Not much difference -- use SUB ESP,4 / MOV [ESP],EAX if you
_really_ think PUSH EAX is too slow. Better for multiple regs.

-- Robert


Relevant Pages