在__asm中插入注释会导致C2400错误(VS2012)

时间:2015-07-07 07:28:20

标签: c++ visual-studio assembly

我试图检查VS 2012中某些代码的编译汇编程序。我添加了两行(代码之前和之后):

__asm ; it begins here!
// My code
__asm ; it ends here!

然而,VS并不喜欢这样。我得到了

  

错误C2400:'操作码'中的内联汇编语法错误;发现'坏令牌'

所以我添加了NOP,我不想这样做:

__asm NOP ; Comment!

工作得很好。我的问题是双重的。

  1. 为什么VS不允许我添加汇编注释?
  2. 是否有不同的方法来添加汇编注释而不添加指令,包括NOP

2 个答案:

答案 0 :(得分:2)

它不起作用的原因是__asm是一个关键字,就像int是一个关键字一样,它不能单独显示,必须遵循正确的语法。以下面的代码为例:

int main()
{
    int      // here's a comment, but it's ignored by the compiler
    return 0;
}

以下代码将因编译错误而失败,更具体地说,在VS2012中,您获得error C2143: syntax error : missing ';' before 'return'。这是一个明显的错误,因为我们没有结尾的分号来表示指令的结束;添加分号并编译好,因为我们没有违反C(或本例中的C ++)语言的语法:

int main()
{
    int      // here's a comment, but it's ignored by the compiler
    ; // white space and comments are ignored by the compiler
    return 0;
}

以下代码也是如此:

int main()
{
    __asm ; here's a comment but it's ignored

    return 0;
}

除此之外我们得到错误error C2400: inline assembler syntax error in 'opcode'; found 'constant',因为它将__asm关键字后的所有内容视为汇编程序指令并且正确地忽略了注释..所以下面的代码将会工作:

int main()
{
    __asm ; here's a comment but it's ignored
    NOP ; white space and comments are ignored by the compiler

    __asm {; here's an __asm 'block'
    } // outside of __asm block so only C style comments work
    return 0;
}

这样就回答了你的第一个问题:Why didn't VS allow me to add an assembly comment? ..因为它语法错误。

现在提出第二个问题:Is there a different way to add an assembly comment without adding an instruction, including NOP?

直接,不,没有,但间接地,是的。值得注意的是__asm关键字被编译到程序中的内联汇编中,因此注释将从编译的程序集中删除,就像它是标准的C / C ++注释一样,所以试图&# 39;力'不需要通过该方法在程序集中添加注释,而是可以使用/FAs编译器标志,它将生成与源混合的程序集(机器代码),例如:

给出以下(非常简单)代码:

int main()
{
    // here's a normal comment
    __asm { ; here's an asm comment and empty block
    } // here's another normal comment
    return 0;
}

使用/FAs编译器标志进行编译时,生成的file.asm中包含以下输出:

; Listing generated by Microsoft (R) Optimizing Compiler Version 18.00.31101.0 

    TITLE   C:\test\file.cpp
    .686P
    .XMM
    include listing.inc
    .model  flat

INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES

PUBLIC  _main
; Function compile flags: /Odtp
; File c:\test\file.cpp
_TEXT   SEGMENT
_main   PROC

; 2    :     {

    push    ebp
    mov ebp, esp

; 3    :         // here's a normal comment
; 4    :         __asm { ; here's an asm comment and empty block
; 5    :         } // here's another normal comment
; 6    :         return 0;

    xor eax, eax

; 7    :     }

    pop ebp
    ret 0
_main   ENDP
_TEXT   ENDS
END

注意它是如何包含源和注释的。如果这段代码做得更多,你会看到更多的程序集和与之相关的源代码。

如果您想要在内联汇编本身中添加注释,那么您可以在__asm块本身内使用普通的C / C ++样式注释和汇编注释:

int main()
{
    // here's a C comment
    __asm { ; here's an asm comment
        // some other comments
        NOP ; asm type comment
        NOP // C style comment
    } // here's another comment
    return 0;
}

希望可以提供帮助。

修改

应该注意的是,下面的代码也可以毫无错误地编译,我不能100%确定原因:

int main()
{
    __asm
    __asm ; comment
    // also just doing it on a single line works too: __asm __asm
    return 0;
}

使用单个__asm ; comment编译此代码会产生编译错误,但两者编译都很好;向上面的代码添加指令并检查.asm输出表明,对于它之前的任何其他汇编命令,将忽略第二个__asm。因此,我不能100%确定这是解析错误还是__asm关键字语法的一部分,因为没有关于此行为的文档。

答案 1 :(得分:0)

在 Linux 上,g++ 接受这一点:

__asm(";myComment");

和输出,当您运行 g++ -S -O3 filename.cpp 时:

# 5 "filename.cpp" 1
    ;myComment

然而,当您运行 clang++ -S -O3 filename.cpp 时,clang++ 不喜欢它,并对此抱怨:

filename.cpp:5:9: error: invalid instruction mnemonic 'myComment'
  __asm(";myComment");
        ^
<inline asm>:1:3: note: instantiated into assembly here
        ;myComment
         ^~~~~~~~~

然而,我能够让 g++ 和 clang++ 接受:

__asm("//myComment");

对于两个编译器,它输出的注释与上面的程序集输出中的注释相同。


是什么让我知道这一点,因为我无法在互联网上的任何其他地方找到它,是从 here 中阅读:

<块引用>

Microsoft 特定

__asm 块中的指令可以使用汇编语言注释:

C++

__asm mov ax, offset buff ; Load address of buff

因为 C 宏扩展为单个逻辑行,避免使用 宏中的汇编语言注释。 (请参阅将 __asm 块定义为 C 宏。)一个 __asm 块也可以包含 C 风格的注释;更多 信息,请参阅在 __asm 块中使用 C 或 C++。

结束微软特定

此页面然后链接到 herehere。这些提供了有关此事的更多信息。

相关问题