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.
- 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
?
The reason it doesn't work is that
__asm
is a keyword, just likeint
is a keyword, it cannot appear by itself and must follow the proper syntax. Take the following bit of code as an example: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:The same is true of the following code:
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: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:
When compiled with the
/FAs
compiler flag, thefile.asm
that was produced had the following output in it: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: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:
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.