插入在C2400错误__asm​​结果(VS2012)评论 [英] Inserting a comment in __asm results in C2400 error (VS2012)

查看:595
本文介绍了插入在C2400错误__asm​​结果(VS2012)评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想检查VS 2012年一些code的编译汇编我加了两条线(前,我的code)之后这样:

  __ ASM;它从这里开始!
//我的code
__asm​​;它结束了!

不过,VS不喜欢这一点。我得到了


  

错误C2400:在运code'内联汇编语法错误;发现坏令牌


所以我增加了一个 NOP ,这是我不想:

  __ ASM NOP;评论!

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


  1. 为什么不VS允许我补充装配对此有何评论?

  2. 是否有不同的方式来增加汇编注释不增加的指令,包括 NOP


解决方案

它不工作的原因是的 __ ASM 是一个关键词,就像 INT 是一个关键字,它不能单独出现,必须遵循正确的语法。以code以下位为例:

  INT的main()
{
    INT //这里有一个评论,但它是由编译器忽略
    返回0;
}

以下code将失败,编译错误,更具体地说是在VS2012你得到错误C2143:语法错误:缺少;前回归。这是一个明显的错误,因为我们还没有结束分号来表示指令的结束;加上分号因为我们没有DIS-服从的C(或C ++在这种情况下)的语法,它编译罚款语言:

  INT的main()
{
    INT //这里有一个评论,但它是由编译器忽略
    ; //空白和注释都被编译器忽略
    返回0;
}

同样是下面的code的情况:

  INT的main()
{
    __asm​​;这里有一个评论,但它忽略    返回0;
}

除了在这里我们得到的误差错误C2400:在运code'内联汇编语法错误;发现'常量',监守它的治疗 __ ASM 关键字作为汇编指令和注释后一切都被理所当然地忽略了..所以下面$ C $ç将工作:

  INT的main()
{
    __asm​​;这里有一个评论,但它忽略
    NOP;空白和注释都被编译器忽略    __asm​​ {;这里是一个__asm​​'块'
    } // __asm​​块,C仅风格的注释工作之外
    返回0;
}

所以,回答你的第一个问题:<?code>为什么不VS允许我补充汇编注释 ..因为它语法错误。

现在你的第二个问题:<?code>是否有不同的方式来增加汇编注释不增加的指令,包括NOP

直接,没有,没有,而是间接的,是有。值得注意的是, __ ASM 关键字获取你的程序编译成内联汇编,所以注释将从编译的程序集被删除就好像它是一个标准的C / C ++评论,所以试图力通过这种方法在汇编评论是没有必要的,相反,你可以使用的 /的FA 编译器标志,它会生成组件(机code)与源混合,例如:

考虑下面的(很简单)code:

  INT的main()
{
    //这是一个正常的评论
    __asm​​ {;这里是一个ASM注释和空块
    } //这里的另一个正常的评论
    返回0;
}

在与 /的FA 编译器标志编译时,这是产生 file.asm 有以下输出它:

 ;由微软(R)优化编译器版本18.00.31101.0清单生成    TITLE C:\\测试\\ file.cpp
    .686P
    .XMM
    包括listing.inc
    .MODEL平INCLUDELIB LIBCMT
INCLUDELIB OLDNAMES公共_main
;功能编译标志:/ Odtp
;文件c:\\测试\\ file.cpp
_TEXT段
_main PROC; 2:{    推EBP
    MOV EBP,ESP; 3://这是一个正常的评论
; 4:__asm​​ {;这里是一个ASM注释和空块
; 5:} //这里的另一个正常的评论
; 6:0的回报;    XOR EAX,EAX; 7:}    流行EBP
    RET 0
_main ENDP
_TEXT ENDS
结束

注意它包括源和评论。如果这个code做了更多,你会看到与之相关更多的装配和来源。

如果你想放的意见在内联大会本身,那么你可以在 __ ASM 块本身正常使用C / C ++风格的注释,以及装配意见

  INT的main()
{
    //这里有一个C注释
    __asm​​ {;这里是一个ASM评论
        //一些其他意见
        NOP; ASM类型评论
        NOP // C风格的注释
    } //这里的另一个评论
    返回0;
}

希望能有所帮助。

编辑:

应该注意code以下位也编译没有错误,我不是100%地肯定了原因:

  INT的main()
{
    __asm
    __asm​​;评论
    //也只是做在一行上工作过:__asm​​ __asm
    返回0;
}

此编译code跟单 __ ASM;注释给出编译错误,但兼具它编译罚款;添加说明上述code和检查 .ASM 输出显示,二 __ ASM 任何被忽略其他装配命令preceding它。所以,我不知道100%,如果这是一个错误解析或 __ ASM 关键字语法的一部分,有这种行为。

没有文件

I was trying to check the compiled assembler of some code in VS 2012. I added two lines (before and after my code) as such:

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

However, VS didn't like that. I got

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

So I added a NOP, which I didn't want to:

__asm NOP ; Comment!

That worked fine. My question is twofold.

  1. Why didn't VS allow me to add an assembly comment?
  2. Is there a different way to add an assembly comment without adding an instruction, including NOP?

解决方案

The reason it doesn't work is that __asm is a keyword, just like int is a keyword, it cannot appear by itself and must follow the proper syntax. Take the following bit of code as an example:

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

The following code will fail with a compilation error, more specifically in VS2012 you get error C2143: syntax error : missing ';' before 'return'. This is an obvious error since we do not have the ending semi-colon to denote end of instruction; add the semi-colon and it compiles fine because we did not dis-obey the syntax of the C (or C++ in this case) language:

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;
}

The same is true of the following code:

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

    return 0;
}

Except here we get the error error C2400: inline assembler syntax error in 'opcode'; found 'constant', becuase it's treating everything after the __asm keyword as an assembler instruction and the comment is being rightfully ignored .. so the following code WOULD work:

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;
}

So that answers your first question: Why didn't VS allow me to add an assembly comment?.. because it is a syntax error.

Now for your second question: Is there a different way to add an assembly comment without adding an instruction, including NOP?

Directly, no, there is not, but indirectly, yes there is. It's worth noting that the __asm keyword gets compiled into inline assembly in your program, so comments will be removed from the compiled assembly just as if it were a standard C/C++ comment, so trying to 'force' a comment in your assembly via that method is not necessary, instead, you can use the /FAs compiler flag and it will generate the assembly (machine code) mixed with the source, example:

Given the following (very simple) code:

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

When compiled with the /FAs compiler flag, the file.asm that was produced had the following output in it:

; 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

Notice how it includes the source and comments. If this code did more, you would see more assembly and the source associated with that as well.

If you're wanting to put comments in the inline assembly itself, then you can use normal C/C++ style comments as well as assembly comments within the __asm block itself:

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;
}

Hope that can help.

EDIT:

It should be noted the following bit of code also compiles without error and I'm not 100% sure why:

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

Compiling this code with the single __asm ; comment gives the compilation error, but with both it compiles fine; adding instructions to the above code and inspecting the .asm output shows that the second __asm is ignored for any other assembly commands preceding it. So I'm not 100% sure if this is a parsing bug or part of the __asm keyword syntax as there's no documentation on this behavior.

这篇关于插入在C2400错误__asm​​结果(VS2012)评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆