ihsoy ih
ihsoy ih

Reputation: 1018

Commenting syntax for x86 AT&T syntax assembly

The Intel syntax has comments using the semicolon. When I switched to AT&T, it actually tried to interpret the comments.

What is the comment syntax for AT&T assembly?

Upvotes: 14

Views: 21686

Answers (4)

Peter Cordes
Peter Cordes

Reputation: 363882

# (hash) is the x86 comment character for GAS (as / gcc -c)
and Clang's built-in assembler (clang -c foo.s)

Same for any compatible assembler; # comments are normal for AT&T syntax.
The GNU assembler manual's i386 syntax section documents this

## example usage and style
    lea   (%rdi, %rsi, 2), %eax     # x + 2*y
.foo_loop:                          # do {
    ...
    dec   %ecx
    jnz   .foo_loop                 # }while(count != 0)
   # EAX = whatever, ECX=0  

GAS also handles /* */ comments itself (but not //), so for example
add $2 /* size of word */, %ecx /* maybe multi-line comment */ works,
as documented in GAS's general section on comments independent of target ISA.
(I don't recommend embedding comments between operands with /* */, but it's possible.)
Typical style is to just use # at the beginning of every line of a block-comment.

In a .S file assembled by GCC or Clang, it will be run through the C preprocessor before assembling (unlike for lower-case .s) so you can also use // comment and even #if 0 / #endif to disable whole blocks, plus #define / #ifdef and all that.


/ at the start of a line (before any non-whitespace) is also a comment character in GAS but not Clang. But don't do that, only use / for assemble-time division like mov $foo_size / 8, %ecx.

clang -c foo.s handles // comments anywhere in a line, but not single-/ at the start of a line. (Even with a lower-case .s where it doesn't process #define so this isn't the C preprocessor handling such comments.)

For GAS, // at the start of a line "works" because it starts with /, but not anywhere later on a line.


The Intel syntax has comments using the semicolon.

Actually, in Clang and the GNU assembler's .intel_syntax noprefix mode, comment characters are the same as in .att_syntax mode and ; is still a statement separator (letting you put multiple instructions on the same source line). The actual instruction syntax is MASM-like in that mode, but directives like .byte and .long are still GAS not MASM.

Different assemblers have different comment characters. NASM / FASM / MASM all use ; as a comment character. They only support (different flavours of) Intel syntax, so probably you mean you switched from one of those to GAS, not just that you switched from GAS Intel syntax to GAS AT&T syntax.

(YASM apparently supports GAS syntax as well as its default of NASM style, but I haven't tried that out. I don't know if it's GAS Intel or GAS AT&T syntax.)

Upvotes: 2

General Grievance
General Grievance

Reputation: 4998

GNU AS Comments

The following are handled by as directly. (Not the C preprocessor.)

  • # Comments - Works as a "rest of line" comment.

    Important Caveat: # is also the GCC preprocessor directive symbol. The preprocessor runs first, so this means that if you are running it,

    # 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 #.

    However, these are case-sensitive, so capitalizing # Include actually works:

    # Include comments in your code to get full credit
    

    While it is generally good practice to capitalize the first letter of your comments anyway, you can use ## as a just-in-case measure. (Just don't use it on any lines that are part of a #define macro because ## is also the token pasting operator.)

  • / comments - Start of line comment

    These may only be used at the start of a line (after whitespace removal).

    / This is OK
    xor %eax, %eax / This is *not* ok
    

C-style Comments (preprocessor)

These work if the C preprocessor is run on the source file.

In most architectures, the following are supported:

  • // Rest of line comment works pretty much as you'd expect from C.

    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.

  • /* Use this for block comments */. I currently haven't run into any issues with this.

So what do I use?

  • If you're not allowed to preprocess everything, choose one of the GNU AS Comment styles, # or /.
  • If you're sure you will preprocess everything, it may be safer to go with the C-style comments // and /**/ to avoid preprocessor issues. However, if you keep in mind the hidden gotchas, you should be ok.
  • If you're concerned about having to handle both, choose either / or ## so you don't have to worry about the preprocessor or lack thereof on any one file. ## is more versatile, but may lead to messier code.
  • Whatever the case may be, choose one and be consistent.

Upvotes: 9

Zec
Zec

Reputation: 843

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.

Upvotes: 19

john
john

Reputation: 57

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

Upvotes: 1

Related Questions