Search code examples
assemblycommentsgnugnu-assembler

How to make the GNU Assembler use a slash / for comments?


This is indeed a stupid idiosyncrasy of mine, but I can't stand the way GNU AS uses to insert a comment. I am too accustomed to the Sun way (the same used in most UNIX assemblers), that uses a simple slash "/" to comment out the code till the end of the line.

Do you know of a way to accomplish my little whim?


Solution

  • GNU GAS docs

    Under the "Machine Dependencies" section, go into each arch, and then into "Syntax" and "Chars".

    This documents what the comments are for each arch.

    x86

    https://sourceware.org/binutils/docs-2.26/as/i386_002dChars.html#i386_002dChars

    The presence of a '#' appearing anywhere on a line indicates the start of a comment that extends to the end of that line.

    If a '#' appears as the first character of a line then the whole line is treated as a comment, but in this case the line can also be a logical line number directive (see Comments) or a preprocessor control command (see Preprocessing).

    If the --divide command line option has not been specified then the '/' character appearing anywhere on a line also introduces a line comment.

    However, I either I'm missing something, or there is a bug, since my tests don't match the documentation.

    OK:

    / mycomment
    # mycomment
    nop # mycomment
    

    Fail:

    nop / mycomment
    

    This suggests that / only works if it is the first character.

    And --divide didn't make any difference.

    arm

    https://sourceware.org/binutils/docs-2.26/as/ARM_002dInstruction_002dSet.html#ARM_002dInstruction_002dSet

    The presence of a '@' anywhere on a line indicates the start of a comment that extends to the end of that line.

    If a '#' appears as the first character of a line then the whole line is treated as a comment, but in this case the line could also be a logical line number directive (see Comments) or a preprocessor control command (see Preprocessing).

    My tests with arm-linux-gnuabihf-as confirm what the documentation says.

    OK:

    # mycomment
    @ mycomment
    nop @ mycomment
    

    Fail:

    nop # mycomment
    

    aarch64

    https://sourceware.org/binutils/docs-2.26/as/AArch64_002dChars.html#AArch64_002dChars

    The presence of a '//' on a line indicates the start of a comment that extends to the end of the current line. If a `#' appears as the first character of a line, the whole line is treated as a comment.

    Furthermore, this is also encouraged by the ARMv8-fb manual has at C1.2 "Structure of the A64 assembler language" itself:

    In Example C1-1 on page C1-185, the sequence // is used as a comment leader and A64 assemblers are encouraged to accept this syntax.

    My tests with aarch64-linux-gnuabihf-as confirm what the documentation says.

    OK:

    // mycomment
    # mycomment
    nop // mycomment
    

    Fail:

    nop # mycomment
    

    Personal recommendation

    If you can choose, just always compile your assembly with gcc or use the C preprocessor cpp explicitly, and use C preprocessor comments:

    /* mycomment */
    

    because:

    • C is standardized, and it will work for all archs
    • you will need the C preprocessor in any case, because GNU GAS macros are not powerful enough
    • # is bad as it could conflict with the # preprocessor directives

    Tested on Ubuntu 16.04, Binutils 2.26.1.