[Issue 5591] New: EBX register not preserved when calling stdcall function pointer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 15 04:00:50 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5591

           Summary: EBX register not preserved when calling stdcall
                    function pointer
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: hypothermia.frost at gmail.com


--- Comment #0 from hypothermia.frost at gmail.com 2011-02-15 03:58:17 PST ---
According to D's calling convention EBX register is preserved across function
calls, but when when I call a pointer to an extern(Windows) function, EBX gets
filled with some garbage value. It only happens the first time this function is
called, and the following times it behaves as expected!

The following sample code can be run to show it:

extern(Windows) void foo(int i){
    size_t ebx;
    asm { mov ebx,EBX; }
    std.stdio.writefln(" foo (EBX = %s,%s)",ebx,i);
    asm { mov EBX,10; }
}

alias extern(Windows) void function(int) fooT;

fooT bar;

void main(){
    size_t ebx=0;
    bar=&foo;

    asm { mov EBX,1; } //try normal call
    foo(2);
    asm { mov ebx,EBX; }
    std.stdio.writefln("EBX(after foo) = %x",ebx);    

    asm { mov EBX,3; } // now try pointer call for the 1st time
    bar(4);
    asm { mov ebx,EBX; }
    std.stdio.writefln("EBX(after bar) = %x",ebx);

    asm { mov EBX,5; } //2nd time
    bar(6);
    asm { mov ebx,EBX; }
    std.stdio.writefln("EBX(after bar 2nd time round) = %x",ebx);

    main2();
}

void main2(){ //and 3rd time!
    size_t ebx=0;
    asm { mov EBX,7; }
    bar(8);
    asm { mov ebx,EBX; }
    std.stdio.writefln("EBX(after bar 3rd time round in other func) = %x",ebx);
}

output:
 foo (EBX = 1,2)
EBX(after foo) = 1
 foo (EBX = 5518328,4)
EBX(after bar) = 5433f8
 foo (EBX = 5,6)
EBX(after bar 2nd time round) = 5
 foo (EBX = 7,8)
EBX(after bar 3rd time round in other func) = 7

What's going on with EBX on the 1st call?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list