【问题标题】:Inline assembly crashs?内联汇编崩溃?
【发布时间】:2011-09-09 18:15:27
【问题描述】:

我在 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
   }
}

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

【问题讨论】:

    标签: visual-c++ assembly inline-assembly


    【解决方案1】:

    项目 + 属性,C/C++,代码生成,选择 /MTd。重复发布配置,选择 /MT。

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

      call dword ptr printf
    

    需要间接调用来自 DLL 的导出。

    【讨论】:

    • 另一个无名的通灵调试答案。感谢您的支持:)
    • Raymond Chen 会很自豪=D
    【解决方案2】:

    我假设突然进入 ebx 是原因。您有责任维护所有寄存器的完整性,eax 除外。尝试改为使用eax。

    【讨论】:

    • 所以我不知道为什么printf 会导致访问冲突,但是您可以通过简单地推送前三个参数来重现问题(无需先加载到eax)然后在函数调用后将 12 添加到 esp,即不会弹出到 eax。 printf 仍然崩溃。`
    • 虽然这不是原因,但这个答案仍然添加了一些非常重要的东西,肯定会避免 future 崩溃。 +1
    猜你喜欢
    • 1970-01-01
    • 2012-10-20
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2017-11-01
    • 2010-10-23
    相关资源
    最近更新 更多