内联asm,错误

时间:2014-03-13 02:10:54

标签: 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()
{
    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。

1 个答案:

答案 0 :(得分:2)

错误确切地说明了什么是错误的。在函数调用之后,您并不是一直在恢复堆栈指针。这看起来像VC输出。您应该编译一个调用fopenfreadfclose的小程序,以查看堆栈的功能。在返回之前,每个函数参数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