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

Inserting a comment in __asm results in C2400 error (VS2012)

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

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

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

error C2400: inline assembler syntax error in 'opcode'; found 'bad token'

所以我添加了一个我不想添加的 NOP:

__asm NOP ; Comment!

效果很好。我的问题是双重的。

  1. 为什么 VS 不允许我添加程序集注释?
  2. 是否有不同的方法可以在不添加指令的情况下添加汇编注释,包括 NOP

它不起作用的原因是__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++ 注释一样,因此尝试 'force' 不需要通过该方法在程序集中注释,相反,您可以使用 /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

注意它是如何包含来源和评论的。如果这段代码做得更多,您会看到更多的程序集以及与之关联的源代码。

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

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 关键字语法的一部分,因为没有关于此行为的文档。

在 Linux 上,g++ 接受这个:

__asm(";myComment");

并输出,当你 运行 g++ -S -O3 filename.cpp:

# 5 "filename.cpp" 1
    ;myComment

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

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 Specific

Instructions in an __asm block can use assembly-language comments:

C++

__asm mov ax, offset buff ; Load address of buff

Because C macros expand into a single logical line, avoid using assembly-language comments in macros. (See Defining __asm Blocks as C Macros.) An __asm block can also contain C-style comments; for more information, see Using C or C++ in __asm Blocks.

END Microsoft Specific

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