2014-03-13 318 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