2011-09-09 45 views
1

我在visual C++ 2010 Ultimate(Windows 7 Professional)中内联汇编时遇到了问题。我所有的内联组件不工作,当我使用字符,DWORD串等等等等......所以,我在我的控制台应用程序复制从MSDN此代码:内联程序集崩溃?

// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp 
// processor: x86 
#include <stdio.h> 

char format[] = "%s %s\n"; 
char hello[] = "Hello"; 
char world[] = "world"; 
int main(void) 
{ 
    __asm 
    { 
     mov eax, offset world 
     push eax 
     mov eax, offset hello 
     push eax 
     mov eax, offset format 
     push eax 
     call printf 
     //clean up the stack so that main can exit cleanly 
     //use the unused register ebx to do the cleanup 
     pop ebx 
     pop ebx 
     pop ebx 
    } 
} 

我什么都没有,除了我的应用程序的那些行,结果:该字符串未打印并且应用程序崩溃。任何想法为什么发生这种情况

回答

0

我假设弹出ebx是原因。维护所有寄存器的完整性是您的责任,不包括eax。改为尝试弹出eax

+0

所以我不知道为什么'printf'导致访问冲突,但是您可以通过简单地推入前三个参数(不需要首先加载到'eax'),然后将12加到esp后重现问题函数调用,即不弹出'eax'。 'printf'仍然崩溃.' –

+0

虽然这不是原因,但这个答案仍然增加了一些非常重要的东西,这肯定会避免_future_崩溃。 +1 – BlackBear

1

Project + Properties,C/C++,Code Generation,select/MTd。重复发布配置,选择/ MT。

如果你想让它与非静态版本的CRT的工作,那么你就需要编写调用是这样的:从一个DLL

call dword ptr printf 

出口需要间接调用。

+0

啧啧,很好,赶上那里。 +1 –

+0

另一个无名的心理调试答案。感谢upvote :) –

+0

Raymond Chen会感到自豪= D –