Using inline assembly from C ++

Just to experiment with building in C ++, I tried the following, which caused the application to crash:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    __asm {
 push 5000
 call Sleep
    }

    ...
}

      

the assembly part of the code should act like the following line

Sleep(5000);

      

What am I doing wrong?

edit: I am getting an access violation.

+2


source to share


4 answers


I just checked the assembly code in VC ++ 6.

You must call this procedure like this:



call dword ptr [Sleep]

      

+3


source


Write the code directly in C - disassemble it, figure out what the compiler does, then you can write the correct version -



+3


source


It's been a long time since I did this, but I remember from the past that sometimes I had to inject a single parameter into the EAX register instead of pushing it onto the stack. Or perhaps you need to pop out again after words if convention requires it.

As Arak says, make sure you follow the compiler calling convention. Masm will supplant one convention, check your C compiler enforces.

0


source


I'm not an x86 guy, but I think you should check the calling convention used by the compiler. Seems to Sleep

clean up after itself, so maybe the compiler is also inserting the cleanup code?

0


source







All Articles