Re: Asm called by c_func

new topic     » goto parent     » topic index » view thread      » older message » newer message

Bernie wrote:

> Juergen Luethje wrote:
> 
>> Larry Miller wrote:
>> 
>>> Thanks to Bernie and Robert. I suspected that all registers should be saved
>>> but was not certain. Bit.e by Juergen Luethje in the recent contributions
>>> uses
>>> ECX but does not save it.
>> 
>> Yes, since I have the same information as Rob regarding this point. smile
>> Also, I never have encountered any problem by not saving the values of
>> AX, CX, DX (in the old 16-bit world) or EAX, ECX, EDX respectively.
>> 
> 
> Hi Juergen
> 
> If you are mixing programming langauges with assembler and the langauge
> is using fast calls the parameters are pass back and forth in the registers.
> 
> If you destroy a registers you may crash the code.

Larry had asked a concrete question:
| What registers, if any, must be preserved if the routine is called by c_func?

In an assembler routine that is called by c_func(), the parameters are
passed on the stack. This is the reason why inside the routine we can
refer to the parameters e.g. with [esp+4].

> You should always preserve any register that you use.

Nope.

> If you use a register that changes the direction flag you should
> do a CLD a clear the direction flag before you return because
> some lanagauges are depending on the direction flag to be clear.

Registers do not change the direction flag at all. Certain _ASM commands_
change the direction flag. This is a completely different thing, and has
nothing got to do with our question here.

> If your not concerned about speed you can use the
>   pushad / popad to preserve the 32bit registers.

This is necessary for implementing a custum interrupt handler or
something. Here it is not only not required, but it would destroy the
functions. The functions _have to_ change the contents of EAX, because
they store the return value there!

>   pushfd / popfd to preserve the flag registers.
> 
> Or preserve just the registers that you disturb.

I'm not "disturbing" anything. The code in 'bit.e' as it is currently
is correct.

> This being in addition to returning with stack be setup in the correct
> condition.

<quote>
32 bit Windows, Linux, BSD, Mac OS
----------------------------------
Scratch registers    : EAX, ECX, EDX, ...
Callee-save registers: EBX, ESI, EDI, EBP
[...]
Scratch registers are registers that can be used for temporary storage
without restrictions (also called caller-save or volatile registers).
Callee-save registers are registers that you have to save before using
them and restore after using them (also called non-volatile registers).
</quote>

Reference:
http://www.agner.org/optimize/calling_conventions.pdf
Chapter 6, Register usage

Regards,
   Juergen

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu