2

Can I use semicolons in GAS for comment characters?

I am using GAS 2.30 with the Intel syntax as below.

.intel_syntax noprefix

.section .text

# Program entry point
.globl _start

_start:

    # Put the code number for system call
    mov eax, 1

    # Return value
    mov ebx, 0

    # Kernel call
    int 0x80

This works nicely except that I have to use '#' as a comment character instead of ';'.

The main reason why it matters is that the editor that I have (SublimeText 3.2.2, build 3211) only has syntax highligting for ARM or NASM assembly syntax, neither of which understands '#' for comments and as I am following materials that mainly use Intel syntax, this is something I would just like to keep consistent with what I am reading about.

I checked the Intel manuals, the 5000+ pages PDF document, and unless I missed it somehow, it says nothing about comments although an introductory page by Intel uses semicolons for comments.

My question is if there is maybe a switch to let GAS use semicolons instead of pound signs in this context or is that perhaps one of the little differences to simply get used to?

I understand that GAS will also accept /, // and /* */ but none of these is understood by SublimeText.

Interestingly enough, telling SublimeText to use Bash syntax highlighting is quite a good solution as in the screenshot below but perhaps there is a way to configure GAS to use semicolons too? Thanks.

GAS assembly syntax highlighting in SublimeText

  • 3
    You can't switch the comment character, but you could preprocess the file if you really wanted. I assume it's simpler to edit the syntax highlight configuration. You could also just use `nasm` if there is proper highlighting for that. Note that directives also differ and probably break highlighting anyway for `gas`. – Jester Sep 06 '20 at 22:07
  • @Jester - Thanks. In addition to various tutorials, the main book that I am reading is "Modern X86 Assembly Language Programming" which uses MASM whereas I am on Linux. GAS with Intel syntax seems to be closer to MASM than NASM and that is one of the reasons why I am using GAS, i.e. among other things, I want to minimise the number of changes to code samples I have to make while reading the book. –  Sep 06 '20 at 22:14
  • You can use jwasm which is masm compatible. However if the book you follow uses masm it's quite likely the code is intended for windows and you will quickly run into problems. – Jester Sep 06 '20 at 22:21
  • In this case, the book does not really have much OS-specific code and practically all the examples are to do with math, no Windows syscalls are involved except for some that are easy to translate to Linux ones, like sys_exit. Thanks for the tip about jwasm, I will check it out. –  Sep 06 '20 at 22:27
  • 2
    you can do something like ;# or #; and see if your syntax highlighter is happier. – old_timer Sep 07 '20 at 00:18
  • 1
    intel docs are going to be related to whatever intel assembler they created or maintain, assembly language is specific to the tool not the target, so gas assembly is not expected to be identical to any other. if it happens to be then great, but if not then that is expected. they like to use the semicolon to separate instructions mov eax,1 ; mov ebx 0, possibly for inline assembly that is fed into the assembler after preprocessing. even though historically a lot of assembly languages used it for comments. – old_timer Sep 07 '20 at 00:21
  • 1
    It seems like the gas folks have a habit of insuring that gas assembly language is incompatible with the native tool from the processor vendor, just part of life, take it or leave it. – old_timer Sep 07 '20 at 00:21

1 Answers1

4

No, gas has no command-line switch or directive for this. The comment characters are hardcoded.

However, it looks like it wouldn't be too hard to change them in the gas source and compile a custom version, if you really want that. At a glance you would want to add ; to i386_comment_chars and line_comment_chars, and remove it from line_separator_chars.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 4
    It would probably be just as easy to modify the syntax-highlighting of a text editor. As an upside, then your source code could be in standard GAS syntax, instead of only ever being usable with a patched version of GAS or with a custom preprocessor. Inventing yet another new dialect of x86 assembly language just because of syntax highlighting defaults / options in some random text editor is one of the worst ideas I've heard in a while. – Peter Cordes Sep 07 '20 at 02:10
  • I understand that the option to add semicolons to GAS was added just for completeness as caveated by the "if you really want that" part of your answer. If I were writing code for my own benefit only this would not be that bad because it would make the GAS's Intel syntax appear closer to MASM. But this code will be shared with other people so this is not really a choice. Instead, I wanted to send a suggestion to GAS authors but apparently [opening an account](https://sourceware.org/bugzilla/createaccount.cgi) means one's email address is made public so I will have to skip that. –  Sep 07 '20 at 09:25
  • That all said, yes, @PeterCordes, I was aware of the fact that each ISA would have its own assembly syntax but I did not realise that the fragmentation of the environment included assembler programs and, apparently, I tacitly assumed that "Intel syntax" = "MASM syntax". –  Sep 07 '20 at 09:29
  • 1
    @Terry: I think it would pretty unlikely that upstream GAS would accept a patch to add an alternate-comment option. More fragmentation of x86 asm syntax is not a good thing. It's not like that would let GAS assemble MASM sources because the directive syntax is different; use JWASM for that. – Peter Cordes Sep 07 '20 at 10:41
  • I thought this could lead to less fragmentation because that should mean more uniformity but, at any rate, this is something that I can much easier deal with by changing the syntax highlighter. As an aside, SublimeText is just an editor that I use for non-primary work but purely to let you know, it has a few million users so it is not just any random one like you noted in of the other comments. Not that I have anything to do with it really, but I am only saying that it is not a niche one. –  Sep 07 '20 at 11:31
  • @Nate Eldregde After spending more time with GAS & Intel syntax I came to a realisation that using NASM will be better in the longer run which is what I am doing now :-) –  Sep 08 '20 at 15:26