asm stackframe question

Timon Gehr timon.gehr at gmx.ch
Sat Apr 7 18:15:48 PDT 2012


On 04/08/2012 12:13 AM, Stefan wrote:
> As a little learning exercise for D I’m writing a fixed point library.
> Part of this is checking standard integer operations for overflows.
>
> This is the code for adding 2 longs:
>
> int64 add64ov(int64 a, int64 b) {
> int64 res;
> asm {
> mov EAX,a ;
> mov EDX,a+4 ;
> add EAX,b ;
> adc EDX,b+4 ;
> jo overflow ;
> mov res,EAX ; // Not optimal: cannot do a ret here because of stack
> frame previously not correctly set
> mov res+4,EDX;
> }
> return res;
> overflow:
> throw new OverflowException("Overflow in 64 bit addition");
> }
>
> The detour via the res variable is costly; the standard epilogue
> (I probably need a frame pointer for the exception throw to work):
> mov ESP, EBP
> pop EBP
> ret
>
> creates an access violation.
>
> Ideas, anyone?

The D calling convention leaves stack cleanup up to the callee. Either 
mark your function as extern(C) or use

leave
ret 16

The second option might not be portable across all currently available 
compilers though.


More information about the Digitalmars-d-learn mailing list