11

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
mghis
  • 517
  • 1
  • 5
  • 14
  • Some regex (Vim, Perl, sed...) probably would do it easily. You can add the appropriate regex command in `Makefile` to preprocess your source code to convert the comment syntax of your choice to a comment syntax valid for GNU AS. – nrz Mar 27 '13 at 15:58
  • My version of `as` (2.20.1, x86_64-linux-gnu) accepts `/`, `//`, and `#` as comment starters. – Michael Mar 27 '13 at 16:02
  • @Michael: you see, `/` works in `as` only if it's the first char in a line. You cannot put it after an instruction, to comment out the rest of the line. – mghis Mar 27 '13 at 16:27
  • Ok. So you'll have to use `#` then. If you find those distracting when you read code you could follow nrz's suggestion. – Michael Mar 27 '13 at 16:31
  • 2
    gnu prides itself in destroying concepts of sane assembly language syntax. – old_timer Mar 27 '13 at 18:33
  • I'm quite conscious that it isn't a very good idea, but at last I modified GNU as source code to get the behaviour I wanted. I can use --divide flag to change `/` meaning to division operator. In fact, the use of comments of GNU is peculiar of GNU AS implementation and cannot be found in other UNIX assemblers. If useful for anyone, change binutils-2.23/gas/config/tc-i386.c lines 329-330 to `const char *i386_comment_chars = "#/";`, `#define SVR4_COMMENT_CHARS 1`, `#define PREFIX_SEPARATOR '\\'`. – mghis May 13 '13 at 16:13
  • Let me ask a dumber question... Do you plan on assembling the file? Or is this something for readability? If readability after preprocessing is the goal, then maybe you can `cat source.s | sed 's|^#|/|g'`. – jww Feb 26 '18 at 15:17

3 Answers3

22

In Gnu assembler the comment start character is target specific. For i386 and x86_64 it is #. For ARMv7 it is @.

Some other comment conventions work under some conditions. I'm not sure about the details. // comment starter and /* */ multi line comments are examples I have seen.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Uwe Geuder
  • 2,236
  • 1
  • 15
  • 21
14

Yes, keep using # and you'll get used to it.

There may be ways of getting / to work but then your code isn't just processor-specific but literally computer-specific. You're better-off getting used to small things than completely destroying the portability of your code to fancy a whim.

Veltas
  • 1,003
  • 6
  • 19
8

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.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985