Commenting syntax for x86 AT&T syntax assembly

Comments for at&t assembler are:

 # this is a comment
 /* this is a comment */

According to the fourth result Google gave me

// and /* */ comments are only supported in .S files because GCC runs the C preprocessor on them before assembling. For .s files, the actual assembler itself (as) only handles # as a comment character, for x86.

For some other ISAs, GAS uses other comment characters, for example @ for ARM.


Avoid Using # Comments

There are many hidden landmines with # as a comment. # is also the GCC preprocessor directive symbol. This means that this:

# include comments in your code to get full credit

at the beginning of the line (whitespaces don't count) will give you error: #include expects "FILENAME" or <FILENAME> with gcc, even with a space after the #. Words you shouldn't use: include, if, else, line, define, and anything else in the C preprocessor. Oddly enough, these do seem to be case-sensitive, so capitalizing # Include actually works.

If you insist on using #'s, ## Line comment will work. Just don't use it on any lines that are part of a #define macro because ## is also the token pasting operator.

Use C-style Comments

Now for what your TAs and profs should have told you.

In most cases/architectures, // and / for line comments are supported:

  • // Rest of line comment Works pretty much as you'd expect from C. However, in rare cases this causes problems with . pseudo-ops. To work around this, I just use a block comment or just move the comment to the preceding line. (If you're worried about compatibility, GCC has apparently allowed this since April of 2000, so you're probably fine.)

  • / Start line comment may only be used at the start of a line (after whitespace removal).

Of course, /* Use this for block comments */.

In short, only use // and /**/ (C-style), and you'll most likely be ok. There are some examples of this here. Like Peter has commented, you don't have to preprocess, but it's a good idea to use something that works in all cases.

Technical Differences

There is a small difference in how these comments are treated. When compiled using gcc -S (create *.s code), the C /**/ and // comments are scraped out, but the # comments are left in.


Try # or // or /* */. Might work