Search code examples
assemblyx86comments

Can a hashtag '#' be used as a comment in assembly (NASM)?


I'm following this assembly source code for learning purposes and came across the following lines:

# load a disk extent into high memory
# eax - sector address
# ecx - sector count
# edi - destination
load_extent:
    ; loading kernel to 1MiB
    ; move part of kernel to startup_end via bootsector#load and then copy it up
    ; repeat until all of the kernel is loaded
    buffer_size_sectors equ 127

There are lines beginning with # which appear to be comments.

He then follows up with the label load_extent: and adds comments which start with ;. Which is what I'm used to seeing.

Was this done intentionally so people don't carelessly copy his code? Or am I missing something?


Solution

  • I went through whole NASM documentation, and there's no mention of hash being comment character, yet my own experiment with NASM v 2.11.08 shows the line starting with # is treated as comment line. So it looks like unofficial feature of NASM. Only ; character is mentioned in documentation.

    Curiously enough, the line:

    xx: # inc ebx
    

    Where hash is used after label will fail with "error: parser: instruction expected".

    Also from the NASM history change-list: "#, @, ~ and c{?} are now valid characters in labels." since v0.91 from 1996, so xx#yy: defines symbolic label xx#yy.

    This makes it somewhat compatible with sh scripts, and can be probably abused somewhat more when creating mixed header file for both C and NASM, where NASM will ignore then C preprocessor directives, so this may be useful.

    But I would advice against using this, at least until it is officially admitted this is intentional feature of NASM, and not some bug of parser.


    Some chit-chat from my early comments before answering this with full answer:

    Assembly is not a single programming language, but general term for all the machine language mnemonics => has no rules.

    Each CPU vendor has it's own instruction set(s) ("ISA"), and it needs particular assembler, and each assembler vendor tends to fill up the gaps missing in official docs (which are only describing instruction functionality, not bothering to define full valid syntax of programming language), so even on the same CPU you can have assemblers with slightly different dialects, one supporting # as comment, other not.

    On x86 there're even assemblers with majorly different syntax: AT&T vs NASM, far beyond additional comment-start character, actually reversing order of operands for instructions, and using different mnemonics for some instructions (movzbl vs movzx for example), etc...

    For other example: in popular (at universities courses) MIPS simulators "MARS" and "SPIM" the # is the only comment character instead of ;, but that is well documented in their official docs (and not relevant to x86).