C ++ uses a bare function

I am trying to learn ASM and want to try a few things in conjunction with C ++. Part of ASM performed bare function. But whenever I call a function (empty), the application crashes in the next function. What should I do in a bare function to make it work, do I need esp pop or something else? An example may be helpful.

_declspec(naked) void asmfunc()
{
    _asm
    {
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    i = 1;

    asmfunc();

    cout << i << endl; // <-- crash
    system("pause");

    return 0;
}
+5
source share
1 answer

The naked function will not contain the prologue and epilogue code created by the compiler. This also applies to the implicit return statement at the end of the function.

, ret . asmfunc, . , -, .

, asmfunc - . , goto asmfunc, .. - - .

_declspec(naked) void asmfunc()
{
    _asm
    {
      ret
    }
}

ret .

+12

All Articles