2014-03-13 31 views
2

我得到这个错误:内联汇编,误差

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

我不知道如何解决这个问题,谁能帮助我?

我的代码是:

#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和流行音乐。

回答

2

该错误说明究竟出了什么问题。在函数调用之后,您并不一致地恢复堆栈指针。这看起来像VC输出。您应该编译一个小程序,调用fopen,freadfclose以查看堆栈已完成的操作。在返回之前,每个函数参数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 
+0

你错过了一个'推eax' :) – Jester

+0

@Jester有一个无用的负荷EAX和推动,当推恒定是可能的,所以我取代了它。那是你的意思吗? – Gene

+0

谢谢,它清除了一点,但我现在得到一个新的错误“运行时检查失败#2 - 围绕变量'lasHeader'堆栈已损坏。”但我会尝试你的建议。 – Dean