C++ 内联asm,错误为

C++ 内联asm,错误为,c++,c,assembly,C++,C,Assembly,我得到了这个错误: 运行时检查失败#0-未在函数调用中正确保存ESP的值。这通常是调用使用一个调用约定声明的函数以及使用另一个调用约定声明的函数指针的结果 我不知道怎么解决,有人能帮我吗 我的代码是: #include "common.h" char* file = "c:\\town.las"; char* file_mode = "r"; #pragma pack(1) struct LASHEADER { char LASF[4]; }; void main() {

我得到了这个错误:

运行时检查失败#0-未在函数调用中正确保存ESP的值。这通常是调用使用一个调用约定声明的函数以及使用另一个调用约定声明的函数指针的结果

我不知道怎么解决,有人能帮我吗

我的代码是:

#include "common.h"

char* file = "c:\\town.las";
char* file_mode = "r";


#pragma pack(1)
struct LASHEADER
{
    char LASF[4];
};


void main()
{
    LASHEADER lasHeader;
    FILE* of;

    __asm
    {
            push ebp       


            mov     eax, file_mode
            push    eax
            push    file
            call    fopen
            mov of, eax

            mov esi, esp
            mov eax, DWORD PTR of
            push eax
            push 1
            push 4 // this should be sizeof LASHEADER

            lea ecx, DWORD PTR lasHeader
            push ecx

            call DWORD PTR fread
            add esp, 16
            cmp esi, esp



            mov eax, of
            push eax
            call fclose


    }
}

我该怎么做呢?我试着在最后推ebp和pop,但没有运气

错误正好说明了问题所在。函数调用后,您没有一致地恢复堆栈指针。这看起来像是VC输出。您应该编译一个调用
fopen
fread
fclose
的小程序,以查看堆栈的处理情况。返回前,每个功能参数
push
必须与添加到
esp
的4个字节相匹配

下面是一个关于什么会起作用的猜测:

        push ebp       

        push    file_mode  ; 1 word
        push    file       ; 2 words
        call    fopen
        mov of, eax        ; this could be wrong depending on compiler

        mov esi, esp
        mov eax, DWORD PTR of
        push eax ; 3 words
        push 1 ; 4 words
        push 4 ; 5 words

        lea ecx, DWORD PTR lasHeader
        push ecx ; 6 words

        call DWORD PTR fread

        mov eax, of ; again could be wrong depending on compiler
        push eax  ; 7 words
        call fclose

        add esp, 28 ; REMOVE 7 words from the stack

        pop ebp

你错过了一个
push eax
:)@Jester有一个无用的加载eax和push,当可以推一个常量时,我就把它替换了。这就是你的意思吗?多亏了这一点,我现在得到了一个新的错误“运行时检查失败#2-变量'lasHeader'周围的堆栈已损坏”。但我会尝试你的建议。不,我的意思是你有一个
push eax
,你忘了计入推送的字数,根据我的计数应该是7。