5

In an answer I wrote I have a code block containing asm code, starting with ~~~nasm (I also tried just ~~~), and it produces nonsense formatting of the assembly:

I have no clue what's going on, the highlighted elements seem more or less random (e.g,. on one line the 0 in r10 is highlighted, while on the next like all of r10 is highlighted, and random words like no in the comments are highlighted).

How can I fix this to get proper nasm/asm highlighting?

5
  • 6
    Apparently, from inspecting the source code, Highlight.js is interpreting your code as Perl.
    – Nat Riddle
    Commented Oct 25, 2021 at 19:55
  • It looks like there's no highlight grammar installed for assembly, based on dev.sstatic.net/js/highlightjs-loader.en.js (search for grmr_bash to find the list).
    – Ryan M Mod
    Commented Oct 25, 2021 at 19:56
  • 1
    If Assembly syntax highlighting isn't supported, you can remove the syntax highlighting by using ~~~none. Commented Oct 25, 2021 at 20:20
  • @NatRiddle - lol, just my luck that it chooses perl of course. I guess perl and asm are similarly inscruitable at times.
    – BeeOnRope
    Commented Oct 25, 2021 at 21:56
  • I just tried to swipe on the right on your screenshot... :)
    – TGrif
    Commented Oct 26, 2021 at 15:23

1 Answer 1

8

The Highlight.js engine that SO uses now simply doesn't support syntax-highlighting for assembly language.

Mind you, neither did the old Google Prettify that used to be used here on Stack Overflow prior to the switchover to highlight.js at the end of September 2020. I had submitted a pull request many years ago, back in 2017, to Google Prettify that added support for x86 assembly code, but it never got merged. This lack of maintenance on Prettify was a big reason why Stack Exchange felt compelled to switch to highlight.js.

Anyway, since highlight.js doesn't support it, it doesn't matter what "hint" string you put after the code fence. As has been mentioned in the comments by Nat Riddle, apparently highlight.js's "automatic" detection is interpreting it as Perl code. In my experience, highlight.js's "automatic" detection is hilariously wrong and should generally just be suppressed:

```none

should do the trick. Better than wrong highlighting, at least.

See also: What is syntax highlighting and how does it work?
2
  • 2
    Thanks Cody. Turns out x86asm is supported by highlight.js (using that hint, which is different than some other highlighters), but it's not the in the handful of languages SO supports out of the many supported by highlight.js. I guess the reluctance to add more is to avoid bloating the javascript, but it seems like there are technical solutions to this problem (e.g., loading per-language javascript dynamically, at least for the more obscure langs).
    – BeeOnRope
    Commented Oct 25, 2021 at 21:16
  • 2
    I added a feature-request to see if we can make x86 asm a supported language: meta.stackoverflow.com/questions/412543/…
    – BeeOnRope
    Commented Oct 25, 2021 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .