tags:

views:

244

answers:

4

Just to experiment assembly in C++, I tried the following, which is causing the application to crash:

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

    ...
}

the assembly part of code is supposed to act like the following line

Sleep(5000);

What am I doing wrong?

edit: I am getting an Access Violation.

A: 

It's been a long time since I did this but I recall from the past that sometimes I had to put a single parameter in the EAX register rather than push it on the stack. Or perhaps you need to pop it off again after words if the calling convention requires it.

As Arak says, check that you are matching the compiler calling convention. Masm will force one convention, check your C compiler is enforcing the same.

Preet Sangha
When writting in MASM, push 5000 followed by call Sleep works perfectly.
devoured elysium
A: 

I am not x86 guy, but I think you should check the calling convention used by the compiler. It seems that Sleep cleans after it self, so maybe the compiler is inserting cleaning code also after that?

AraK
+3  A: 

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

You have to call the routine like this:

call dword ptr [Sleep]
Nick D
+3  A: 

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

Jeff