ASM代码中对C ++变量的未定义引用

时间:2014-07-30 16:05:50

标签: c++ gcc assembly compiler-errors

我试图在C ++文件中编译以下asm代码,但它像GCC一样找不到asm部分中使用的变量。

DWORD d_eaxSave, d_ebxSave, d_ecxSave, d_edxSave, d_esiSave, d_ediSave, d_espSave, d_ebpSave;

#define ASM_UNPREFIXED_INTEL_START ".intel_syntax noprefix\n\t"
#define SAVE_REGISTERS() __asm__ (ASM_UNPREFIXED_INTEL_START    \
                              "mov d_eaxSave, eax\n\t"      \
                              "mov d_ebxSave, ebx\n\t"      \
                              "mov d_ecxSave, ecx\n\t"      \
                              "mov d_edxSave, edx\n\t"      \
                              "mov d_esiSave, esi\n\t"      \
                              "mov d_ediSave, edi\n\t"      \
                              "mov d_espSave, esp\n\t"      \
                              "mov d_ebpSave, ebp");

但是当我尝试编译时,GCC给了我以下错误:

undefined reference to `d_eaxSave'
undefined reference to `d_ebxSave'
undefined reference to `d_ecxSave'
undefined reference to `d_edxSave'
undefined reference to `d_esiSave'
undefined reference to `d_ediSave'
undefined reference to `d_espSave'
undefined reference to `d_ebpSave'

我能做些什么来摆脱那些错误呢?

1 个答案:

答案 0 :(得分:3)

你想通常使用AT& T语法,因为据我所知,GCC无法在没有-masm=intel命令行开关的情况下发出正确的intel syntax in inline assembler - 我认为你不能包括任何使用AT& T内联汇编的头文件。

因此:

DWORD d_eaxSave, d_ebxSave, d_ecxSave, d_edxSave, d_esiSave, d_ediSave, d_espSave, d_ebpSave;

#define SAVE_REGISTERS() __asm__ __volatile__(         \
     "movl %%eax, %0\n\t"      \
     "movl %%ebx, %1\n\t"      \
     "movl %%ecx, %2\n\t"      \
     "movl %%edx, %3\n\t"      \
     "movl %%esi, %4\n\t"      \
     "movl %%edi, %5\n\t"      \
     "movl %%esp, %6\n\t"      \
     "movl %%ebp, %7":         \
     "=m"(d_eaxSave), "=m"(d_ebxSave), "=m"(d_ecxSave), "=m"(d_edxSave),  \
     "=m"(d_esiSave), "=m"(d_ediSave), "=m"(d_espSave), "=m"(d_ebpSave)   \
)

汇编器可能不存在符号,因此您需要通过输出参数使用它们! 另请注意,通常您需要使用__volatile__来确保汇编程序块未被优化掉。