Search code examples
assemblynasm

Naming convention for labels, functions


I'm confused a bit and still haven't found an explicit answer: should I name labels as just "label1" or with underscore "_label1"? What's the most popular and recognizable way? I've even seen one with dot ".label1" if I remember correctly.

The same question goes for functions: "function1" or "_function1"?


Solution

  • From a code maintainability viewpoint, leading dots and underscores don't add any useful information to a label, so if there is no specific need for those prefixes (see below), you should get rid of them. Just the fact that you may think "it looks fancy" is not enough reason to use them.

    Dots are sometimes used by assemblers to specify that a label is local, i.e. not to be exported as a symbol to the object file. This can be useful in order to not pollute the global name space with symbols you do not need.

    Other assemblers use dots to denote an assembler directive and do not allow dots in symbols at all.

    Underscores are typically used by assembler code that has to integrate with higher-level languages. Some C compilers prefix all exported symbols with underscores, and in order to be able to be callable from C code, assembler functions need to follow this rule.

    Generally, your assembler (and/or compiler, in case you integrate with compiled code) manual should be able to tell you what variations your specific assembler supports or requires. And if the manual doesn't explicitly tell you when and why you should use such prefixes, simply don't use them - Assembler labels are one (the minimal and most important) part of self-documenting your code. Don't waste that for meaningless prefixes if you don't need to.