Frustrated by the lack of syntax highlighting on Stack Exchange sites for code snippets written in x86 assembly language, I decided to do something about it. The network uses Google Code Prettify as its syntax-highlighting engine, so I wrote a new module that adds support for this language to Prettify.

The Prettify project's (limited) documentation says this is as simple as adding a new file following the lang-*.js pattern, so that's is what I did, following the model of the other language files included with the library. Here is the final result:


 * @license
 * Copyright (C) 2008 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

 * @fileoverview
 * Registers a language handler for x86 assembly language,
 * supporting both Intel/MASM and AT&T/GAS syntax.
 * Note that some of the more esoteric syntax and features of these assemblers
 * is not supported. The highlighting focuses on constructs that would be found
 * in typical code samples. All constructs are matched without respect to case.
 * This file is an amalgamation of patterns taken from the other lang-*.js files
 * included with this library, as well as novel patterns written by the author.
 * The authoritative documentation for the registers and instruction mnemonics is
 * the "Intel IA-32 Architecture Software Developer's Manual", available online
 * at: https://software.intel.com/en-us/articles/intel-sdm.
 * Documentation for Microsoft's assembler (MASM) is available online here:
 *   https://msdn.microsoft.com/en-us/library/afzk3475.aspx.
 * Documentation for the GNU assembler 'as' (GAS) is available online here:
 *   https://sourceware.org/binutils/docs/as/.
 * To use, include prettify.js and this file in your HTML page.
 * Then, put your code in an HTML tag like:
 *      <pre class="prettyprint lang-x86">...</pre>
 * @author cody@<myfullname>.com

   PR['createSimpleLexer']([], [
         // Comments begin with either ';' (Intel/MASM) or '#' (AT&T/GAS),
         // and extend to the end of that line.
         [PR['PR_COMMENT'],      /^[;#].*/],  

         // Instruction mnemonics are like "names" for attributes.
         // Unfortunately, regular expressions don't help us much here: we have to exhaustively enumerate every possibility.
         // However, we can use a regex to match AT&T/GAS's suffixes, albeit at the risk of potentially matching instructions
         // that are not valid (and this is especially true for those with alternate encodings in Intel vs. AT&T syntax).
         // (Base)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:aaa|aad|aam|aas|adc|add|and|arpl|bound|bsf|bsr|bswap|bt|btc|btr|bts|call|cbw|cdq|clt|cflush|clc|cld|cli|clts|cmc|cmp|cmps|cmpsb|cmpsd|cmpsw|cmpxchg|cmpxchg486|cmpxchg8b|cpuid|cwd|cwde|daa|das|dec|div|enter|esc|fatret|hlt|ibts|icebp|idiv|imul|in|inc|ins|insb|insd|insw|int|into|invd|invlpg|iret|iretd|iretdf|iretf|iretw|lahf|lar|lds|lea|leave|les|lfs|lgdt|lgs|lidt|lldt|lmsw|loadall|loadalld|loadall286|lock|lods|lodsb|lodsd|lodsw|loop|loopd|loope|looped|loopew|loopne|loopned|loopnew|loopnz|loopnzd|loopnzw|loopw|loopz|loopzd|loopzw|lsl|lss|ltr|mov|movs|movsb|movsd|movsw|movsx|movsbw|movslq|movzx|movzwl|movzbl|mul|neg|nop|not|or|out|outs|outsb|outsd|outsw|pop|popa|popad|popaw|popf|popfd|popfw|push|pusha|pushad|pushaw|pushd|pushf|pushfd|pushfw|pushw|rcl|rcr|rdmsr|rdpmc|rdshr|rdtsc|rep|repe|repne|repnz|repz|ret|retf|retn|rol|ror|rsdc|rsldt|rsm|rsts|sahf|sal|salc|sar|sbb|scas|scasb|scasd|scasw|sgdt|shl|shld|shr|shrd|sidt|sldt|smi|smint|smintold|smsw|stc|std|sti|stos|stosb|stosd|stosw|str|sub|svdc|svldt|svts|syscall|sysenter|sysexit|sysret|test|ud0|ud1|ud2|umov|verr|verw|wait|wbinvd|wrmsr|wrshr|xadd|xbts|xchg|xlat|xlatb|xor)[bwlq]?\b/i],
         // (Base, x86-64)         
         [PR['PR_ATTRIB_NAME'],  /^\s(?:cdqe|cqo|cmpsq|cmpxchg16|iretq|jrcxz|lodsq|movsxd|popfq|pushfq|rdtscp|scasq|stosq|swapgs)[bwlq]?\b/i],         
         // (Jumps/Branches)
         [PR['PR_ATTRIB_NAME'],  /^\sj(?:mp|cxz|ecxz|o|no|s|ns|e|z|ne|nz|b|nae|c|nb|ae|nc|be|na|a|nbe|l|nge|ge|nl|le|ng|g|nle|p|pe|po|np)\b/i],
         // (Other conditional instructions)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:set|cmov)(?:o|no|s|ns|e|z|ne|nz|b|nae|c|nb|ae|nc|be|na|a|nbe|l|nge|ge|nl|le|ng|g|nle|p|pe|po|np)[bwlq]?\b/i],
         [PR['PR_ATTRIB_NAME'],  /^\sfcmov(?:b|e|be|u|nb|ne|nbe|nu)\b/i],
         // (x87 FPU)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:f2xm1|fabs|fadd|faddp|fbld|fbstp|fchs|fclex|fcom|fcomi|fcomip|fcomp|fcompp|fcos|fdecstp|fdisi|fdiv|fdivp|fdivr|fdivrp|feni|ffree|ffreep|fiadd|ficom|ficomp|fidiv|fidivr|fild|fimul|fincstp|finit|fist|fistp|fisub|fisubr|fld|fld1|fldcw|fldenv|fldenvd|fldenvw|fldl2e|fldl2t|fldlg2|fldln2|fldpi|fldz|fmul|fmulp|fnclex|fndisi|fneni|fninit|fnop|fnsave|fnsaved|fnsavew|fnstcw|fnstenv|fnstenvd|fnstenvw|fnstsw|fpatan|fprem|fprem1|fptan|frndint|frstor|frstord|frstorw|fsave|fsaved|fsavew|fscale|fsetpm|fsin|fsincos|fsqrt|fst|fstcw|fstenv|fstenvd|fstenvw|fstp|fstsw|fsub|fsubp|fsubr|fsubrp|ftst|fucom|fucomi|fucomp|fucomip|fucompp|fwait|fxam|fxch|fxtract|fyl2x|fyl2xp1)[slqt]?\b/i],
         // (MMX)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:(?:movntq|pavgb|pavgw|pextrw|pinsrw|pmaxsw|pmaxub|pminsw|pminub|pmulhuw|psadbw|pshufw)[bwlq]?)|(?:emms|femms|maskmovq|pmovmskb)\b/i],
         // (SSE)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:(?:addps|addss|andnps|andps|clflush|cmpeqps|cmpeqss|cmpleps|cmpless|cmpltps|cmpltss|cmpneqps|cmpneqss|cmpnleps|cmpnless|cmpnltps|cmpnltss|cmpordps|cmpordss|cmpps|cmpss|cmpunordps|cmpunordss|comiss|cvtpi2ps|cvtps2pi|cvtsi2ss|cvtss2si|cvttps2pi|cvttss2si|divps|divss|fxrstor|fxsave|ldmxcsr|lfence|maxps|maxss|mfence|minps|minss|movaps|movd|movhps|movlps|movntdq|movnti|movntpd|movntps|movss|movups|mulps|mulss|orps|packssdw|packsswb|packuswb|paddb|paddd|paddq|paddsb|paddsw|paddusb|paddusw|paddw|pand|pandn|pavgb|pavgw|pcmpeqb|pcmpeqd|pcmpeqw|pcmpgtb|pcmpgtd|pcmpgtw|pextrw|pinsrw|pmaddwd|pmaxsw|pmaxub|pminsw|pminub|pmulhuw|pmulhw|pmullw|pmuludq|por|psadbw|pshufd|pshufhw|pshuflw|pslld|pslldq|psllq|psllw|psrad|psraw|psrld|psrldq|psrlq|psrlw|psubb|psubd|psubq|psubsb|psubsw|psubusb|psubusw|psubw|punpckhbw|punpckhdq|punpckhqdq|punpckhwd|punpcklbw|punpckldq|punpcklqdq|punpcklwd|pxor|rcpps|rcpss|rsqrtps|rsqrtss|sfence|shufps|sqrtps|sqrtss|stmxcsr|subps|subss|ucomiss|unpckhps|unpcklps|xorps)[bwlq]?)|(?:maskmovdqu|movhlps|movlhps|movmskps|movdqa|movdqu|movdq2q|movq|movq2dq|pmovmskb)\b/i],
         // (SSE2)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:(?:addpd|addsd|andnpd|andpd|cmpeqpd|cmpeqsd|cmplepd|cmplesd|cmpltpd|cmpltsd|cmpneqpd|cmpneqsd|cmpnlepd|cmpnlesd|cmpnltpd|cmpnltsd|cmpordpd|cmpordsd|cmppd|cmpsd|cmpunordpd|cmpunordsd|comisd|cvtdq2pd|cvtdq2ps|cvtpd2dq|cvtpd2pi|cvtpd2ps|cvtpi2pd|cvtps2dq|cvtps2pd|cvtsd2si|cvtsd2ss|cvtsi2sd|cvtss2sd|cvttpd2dq|cvttpd2pi|cvttps2dq|cvttsd2si|divpd|divsd|maxpd|maxsd|minpd|minsd|movapd|movhpd|movlpd|movsd|movupd|mulpd|mulsd|orpd|shufpd|sqrtpd|sqrtsd|subpd|subsd|ucomisd|unpckhpd|unpcklpd|xorpd)[bwlq]?)|(?:movmskpd)\b/i],
         // (SSE3)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:(?:addsubpd|addsubps|haddpd|haddps|hsubpd|hsubps|lddqu|movddup|movshdup|movsldup)[bwlq]?)|(?:monitor|mwait|fisttp)\b/i],
         // (SSSE3)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:pabsb|pabsw|pabsd|palignr|phaddw|phaddd|phaddsw|phsubw|phsubd|phsubsw|pmaddubsw|pmulhrsw|pshufb|psignb|psignw|psignd)[bwlq]?\b/i],
         // (SSE4.1)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:blendpd|blendps|blendvpd|blendvps|dppd|dpps|extractps|insertps|movntdqa|mpsadbw|packusdw|pblendvb|pblendw|pcmpeqq|pextrb|pextrd|pextrq|pextrw|phminposuw|pinsrb|pinsrd|pinsrq|pmaxsb|pmaxsd|pmaxud|pmaxuw|pminsb|pminsd|pminud|pminuw|pmovsxbw|pmovsxbd|pmovsxbq|pmovsxwd|pmovsxwq|pmovsxdq|pmovzxbw|pmovzxbd|pmovzxbq|pmovzxwd|pmovzxwq|pmovzxdq|pmuldq|pmulld|ptest|roundpd|roundps|roundsd|roundss)[bwlq]?\b/i],
         // (SSE4.2)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:crc32|pcmpestri|pcmpestrm|pcmpistri|pcmpistrm|pcmpgtq|popcnt)[bwlq]?\b/i],
         // (BMI1)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:andn|bextr|blsi|blsmsk|blsr|lzcnt|tzcnt)[bwlq]?\b/i],
         // (BMI2)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:bzhi|mulx|pdep|pext|rorx|sarx|shrx|shlx)[bwlq]?\b/i],
         // (AVX)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:(?:vaddpd|vaddps|vaddsd|vaddss|vaddsubpd|vaddsubps|vandpd|vandps|vandnpd|vandnps|vblendpd|vblendps|vblendvpd|vblendvps|vblendvpd|vbroadcastss|vbroadcastsd|vbroadcastf128|vcmpeqpd|vcmpltpd|vcmplepd|vcmpunordpd|vcmpneqpd|vcmpnltpd|vcmpnlepd|vcmpordpd|vcmpeq_uqpd|vcmpngepd|vcmpngtpd|vcmpfalsepd|vcmpneq_oqpd|vcmpgepd|vcmpgtpd|vcmptruepd|vcmpeq_ospd|vcmplt_oqpd|vcmple_oqpd|vcmpunord_spd|vcmpneq_uspd|vcmpnlt_uqpd|vcmpnle_uqpd|vcmpord_spd|vcmpeq_uspd|vcmpnge_uqpd|vcmpngt_uqpd|vcmpfalse_ospd|vcmpneq_ospd|vcmpge_oqpd|vcmpgt_oqpd|vcmptrue_uspd|vcmppd|vcmpeqps|vcmpltps|vcmpleps|vcmpunordps|vcmpneqps|vcmpnltps|vcmpnleps|vcmpordps|vcmpeq_uqps|vcmpngeps|vcmpngtps|vcmpfalseps|vcmpneq_oqps|vcmpgeps|vcmpgtps|vcmptrueps|vcmpeq_osps|vcmplt_oqps|vcmple_oqps|vcmpunord_sps|vcmpneq_usps|vcmpnlt_uqps|vcmpnle_uqps|vcmpord_sps|vcmpeq_usps|vcmpnge_uqps|vcmpngt_uqps|vcmpfalse_osps|vcmpneq_osps|vcmpge_oqps|vcmpgt_oqps|vcmptrue_usps|vcmpps|vcmpeqsd|vcmpltsd|vcmplesd|vcmpunordsd|vcmpneqsd|vcmpnltsd|vcmpnlesd|vcmpordsd|vcmpeq_uqsd|vcmpngesd|vcmpngtsd|vcmpfalsesd|vcmpneq_oqsd|vcmpgesd|vcmpgtsd|vcmptruesd|vcmpeq_ossd|vcmplt_oqsd|vcmple_oqsd|vcmpunord_ssd|vcmpneq_ussd|vcmpnlt_uqsd|vcmpnle_uqsd|vcmpord_ssd|vcmpeq_ussd|vcmpnge_uqsd|vcmpngt_uqsd|vcmpfalse_ossd|vcmpneq_ossd|vcmpge_oqsd|vcmpgt_oqsd|vcmptrue_ussd|vcmpsd|vcmpeqss|vcmpltss|vcmpless|vcmpunordss|vcmpneqss|vcmpnltss|vcmpnless|vcmpordss|vcmpeq_uqss|vcmpngess|vcmpngtss|vcmpfalsess|vcmpneq_oqss|vcmpgess|vcmpgtss|vcmptruess|vcmpeq_osss|vcmplt_oqss|vcmple_oqss|vcmpunord_sss|vcmpneq_usss|vcmpnlt_uqss|vcmpnle_uqss|vcmpord_sss|vcmpeq_usss|vcmpnge_uqss|vcmpngt_uqss|vcmpfalse_osss|vcmpneq_osss|vcmpge_oqss|vcmpgt_oqss|vcmptrue_usss|vcmpss|vcomisd|vcomiss|vcvtdq2pd|vcvtdq2ps|vcvtpd2dq|vcvtpd2ps|vcvtps2dq|vcvtps2pd|vcvtsd2si|vcvtsd2ss|vcvtsi2sd|vcvtsi2ss|vcvtss2sd|vcvtss2si|vcvttpd2dq|vcvttps2dq|vcvttsd2si|vcvttss2si|vdivpd|vdivps|vdivsd|vdivss|vdppd|vdpps|vextractf128|vextractps|vhaddpd|vhaddps|vhsubpd|vhsubps|vinsertf128|vinsertps|vlddqu|vldqqu|vlddqu|vldmxcsr|vmaskmovps|vmaskmovpd|vmaxpd|vmaxps|vmaxsd|vmaxss|vminpd|vminps|vminsd|vminss|vmovapd|vmovaps|vmovq|vmovd|vmovddup|vmovdqa|vmovqqa|vmovdqa|vmovdqu|vmovqqu|vmovdqu|vmovhpd|vmovhps|vmovlpd|vmovlps|vmovmskpd|vmovmskps|vmovntdq|vmovntqq|vmovntdq|vmovntdqa|vmovntpd|vmovntps|vmovsd|vmovshdup|vmovsldup|vmovss|vmovupd|vmovups|vmpsadbw|vmulpd|vmulps|vmulsd|vmulss|vorpd|vorps|vpabsb|vpabsw|vpabsd|vpacksswb|vpackssdw|vpackuswb|vpackusdw|vpaddb|vpaddw|vpaddd|vpaddq|vpaddsb|vpaddsw|vpaddusb|vpaddusw|vpalignr|vpand|vpandn|vpavgb|vpavgw|vpblendvb|vpblendw|vpcmpestri|vpcmpestrm|vpcmpistri|vpcmpistrm|vpcmpeqb|vpcmpeqw|vpcmpeqd|vpcmpeqq|vpcmpgtb|vpcmpgtw|vpcmpgtd|vpcmpgtq|vpermilpd|vpermiltd2pd|vpermilmo2pd|vpermilmz2pd|vpermil2pd|vpermilps|vpermiltd2ps|vpermilmo2ps|vpermilmz2ps|vpermil2ps|vperm2f128|vpextrb|vpextrw|vpextrd|vpextrq|vphaddw|vphaddd|vphaddsw|vphminposuw|vphsubw|vphsubd|vphsubsw|vpinsrb|vpinsrw|vpinsrd|vpinsrq|vpmaddwd|vpmaddubsw|vpmaxsb|vpmaxsw|vpmaxsd|vpmaxub|vpmaxuw|vpmaxud|vpminsb|vpminsw|vpminsd|vpminub|vpminuw|vpminud|vpmovsxbw|vpmovsxbd|vpmovsxbq|vpmovsxwd|vpmovsxwq|vpmovsxdq|vpmovzxbw|vpmovzxbd|vpmovzxbq|vpmovzxwd|vpmovzxwq|vpmovzxdq|vpmulhuw|vpmulhrsw|vpmulhw|vpmullw|vpmulld|vpmuludq|vpmuldq|vpor|vpsadbw|vpshufb|vpshufd|vpshufhw|vpshuflw|vpsignb|vpsignw|vpsignd|vpslldq|vpsrldq|vpsllw|vpslld|vpsllq|vpsraw|vpsrad|vpsrlw|vpsrld|vpsrlq|vptest|vpsubb|vpsubw|vpsubd|vpsubq|vpsubsb|vpsubsw|vpsubusb|vpsubusw|vpunpckhbw|vpunpckhwd|vpunpckhdq|vpunpckhqdq|vpunpcklbw|vpunpcklwd|vpunpckldq|vpunpcklqdq|vpxor|vrcpps|vrcpss|vrsqrtps|vrsqrtss|vroundpd|vroundps|vroundsd|vroundss|vshufpd|vshufps|vsqrtpd|vsqrtps|vsqrtsd|vsqrtss|vstmxcsr|vsubpd|vsubps|vsubsd|vsubss|vtestps|vtestpd|vucomisd|vucomiss|vunpckhpd|vunpckhps|vunpcklpd|vunpcklps|vxorpd|vxorps)[bwlq]?)|(?:vmaskmovdqu|vmovhlps|vmovlhps|vpmovmskb|vzeroall|vzeroupper)\b/i],
         // (FMA)
         [PR['PR_ATTRIB_NAME'],  /^\s(?:vfmadd132ps|vfmadd132pd|vfmadd312ps|vfmadd312pd|vfmadd213ps|vfmadd213pd|vfmadd123ps|vfmadd123pd|vfmadd231ps|vfmadd231pd|vfmadd321ps|vfmadd321pd|vfmaddsub132ps|vfmaddsub132pd|vfmaddsub312ps|vfmaddsub312pd|vfmaddsub213ps|vfmaddsub213pd|vfmaddsub123ps|vfmaddsub123pd|vfmaddsub231ps|vfmaddsub231pd|vfmaddsub321ps|vfmaddsub321pd|vfmsub132ps|vfmsub132pd|vfmsub312ps|vfmsub312pd|vfmsub213ps|vfmsub213pd|vfmsub123ps|vfmsub123pd|vfmsub231ps|vfmsub231pd|vfmsub321ps|vfmsub321pd|vfmsubadd132ps|vfmsubadd132pd|vfmsubadd312ps|vfmsubadd312pd|vfmsubadd213ps|vfmsubadd213pd|vfmsubadd123ps|vfmsubadd123pd|vfmsubadd231ps|vfmsubadd231pd|vfmsubadd321ps|vfmsubadd321pd|vfnmadd132ps|vfnmadd132pd|vfnmadd312ps|vfnmadd312pd|vfnmadd213ps|vfnmadd213pd|vfnmadd123ps|vfnmadd123pd|vfnmadd231ps|vfnmadd231pd|vfnmadd321ps|vfnmadd321pd|vfnmsub132ps|vfnmsub132pd|vfnmsub312ps|vfnmsub312pd|vfnmsub213ps|vfnmsub213pd|vfnmsub123ps|vfnmsub123pd|vfnmsub231ps|vfnmsub231pd|vfnmsub321ps|vfnmsub321pd|vfmadd132ss|vfmadd132sd|vfmadd312ss|vfmadd312sd|vfmadd213ss|vfmadd213sd|vfmadd123ss|vfmadd123sd|vfmadd231ss|vfmadd231sd|vfmadd321ss|vfmadd321sd|vfmsub132ss|vfmsub132sd|vfmsub312ss|vfmsub312sd|vfmsub213ss|vfmsub213sd|vfmsub123ss|vfmsub123sd|vfmsub231ss|vfmsub231sd|vfmsub321ss|vfmsub321sd|vfnmadd132ss|vfnmadd132sd|vfnmadd312ss|vfnmadd312sd|vfnmadd213ss|vfnmadd213sd|vfnmadd123ss|vfnmadd123sd|vfnmadd231ss|vfnmadd231sd|vfnmadd321ss|vfnmadd321sd|vfnmsub132ss|vfnmsub132sd|vfnmsub312ss|vfnmsub312sd|vfnmsub213ss|vfnmsub213sd|vfnmsub123ss|vfnmsub123sd|vfnmsub231ss|vfnmsub231sd|vfnmsub321ss|vfnmsub321sd)[bwlq]?\b/i],

         // Registers are like the corresponding "values" for attributes.
         // REVIEW: Would it be better (simpler to match and thus more performant)
         //         for this to simply be an exhaustive list of candidates?
         [PR['PR_ATTRIB_VALUE'], /^%?\b(?:(?:dil|sil|bpl)|(?:[re]?(?:ax|bx|cx|dx|di|si|sp|bp|ip))|(?:(?:a|b|c|d|sp|bp)[lh]+)|(?:[cdefgs]+s)|(?:r([8-9]|1[0-5])[blwd]?)|(?:[xy]?mm[0-7]))\b/i],
         // The x87 FPU registers are matched using a separate expression because of the optional parentheses.
         // For some reason, "/^%?\b(?:st\(?[0-7]?\)?)/i" doesn't highlight the trailing parenthesis.
         [PR['PR_ATTRIB_VALUE'], /^%?\b(?:st\(?[0-7]?)\W/i],

         // Keywords/directives relating to types are highlighted accordingly.
         [PR['PR_TYPE'],         /^(?:(?:\bshort)|(?:\b(?:(?:s|d|sd|q|f|mm|xmm|ymm)?word|[st]?byte)(?: ptr)?)|(?:\breal(?:4|8|10)\b)|(?:\bd[bwdqft]\b)|\s\.(?:byte|short|hword|word|int|long|quad|octa|sleb128|uleb128|float|single|double|ascii|asciz|string|string8|string16))\b/i],

         // All other keywords/directives/pseudo-ops are highlighted as "keywords".
         // First, match GAS-style with the initial '.', and then match MASM.
         [PR['PR_KEYWORD'],      /^\.(?:type|title|text|tag|subsection|struct|space|skip|size|section|rept|reloc|org|noaltmacro|model|macro|local|loc_mark_labels|loc|ln|list|line|internal|intel_syntax|include|ident|gnu_attribute|globl|global|func|fill|file|extern|exitm|eqv|equiv|equ|endm|endfunc|endef|dim|desc|def|data|altmacro|cfi_window_save|cfi_val_encoded_addr|cfi_undefined|cfi_startproc|cfi_signal_frame|cfi_sections|cfi_same_value|cfi_return_column|cfi_restore|cfi_remember_state|cfi_rel_offset|cfi_register|cfi_personality|cfi_offset|cfi_lsda|cfi_escape|cfi_endproc|cfi_def_cfa_register|cfi_def_cfa_offset|cfi_def_cfa|cfi_adjust_cfa_offset|balignw|balignl|align)\s/i],
         [PR['PR_KEYWORD'],      /^(?:xdata|tiny|times|struct|stdcall|small|sizeof|size|segment|seg|section|rept|repeat|proto|proc|pdata|org|option|offset|npad|near|medium|macro|length|large|istruc|invoke|includelib|include|import|huge|group|global|flat|far|extrn|externdef|extern|export|exitm|even|equ|eq|endstruc|ends|endp|endm|end|dup|dosseg|const|compact|comdat|assumes|assume|alignb|align|alias|addr)|(?:[\._]+(?:text|data|code|bss))\s/i],

         // Any text that is followed by a colon is considered to be a label.
         // This is a little bit over-eager, since labels can only be a single token (no spaces),
         // but that's actually OK because it will match GAS-style leading function signatures.
         // Note that we must explicitly ignore comments on a line following a label, especially
         // those with embedded colons, to prevent them being treated as part of the label.
         // Colons inside of string & character literals are also ignored.
         // The beginning of line anchor ('^') does not seem to do what it is expected to do,
         // so '\b' is used as a workaround, but this means we need to explicitly include any
         // symbols that might be found preceding a label, like '.' and '$'.
         [PR['PR_DECLARATION'],  /^[$._]*\b[^'":;#\r\n]*:/],

         // String & character literals.
         [PR['PR_STRING'],       /^"[^"\\]*(?:\\.[^"\\]*)*"/],
         [PR['PR_STRING'],       /^'[^'\\]*(?:\\.[^'\\]*)*'/],

         // Decimal and hexadecimal integer literals.
         // (Rarely-used octal and binary literals are not yet supported.)
         // The current implementation highlights the prefixes and suffixes, mostly for convenience
         // since I can't get positive lookaheads to work, but also because it makes sense to me to
         // group them semantically with the number when reading code. However, this is up for
         // debate and may need to be reconsidered/
         [PR['PR_LITERAL'],      /^(?=\$?)0x[\da-f]+/i], // "0x" prefix
         [PR['PR_LITERAL'],      /^\d[\da-f]*(?=h)/i],   // MASM-style "h" suffix (must start with a decimal digit)
         // In Intel and certain places in AT&T syntax, decimal literals occur with no identifying prefix
         // or suffix (although MASM allows an optional 'd' suffix). These are tricky because they must
         // be matched in just about any context, except when they are embedded within another identifier
         // (like those commonly generated by compilers). This is handled by explicitly detecting such
         // identifers and marking them as "plain text" to exclude them from future matches, and then
         // detecting and highlighting the remaining integer literals.
         [PR['PR_PLAIN'],        /^(?:[a-z_\.]+\d+)|(?:\d+[a-z_\.]+)/i],
         // Also, although we normally don't want to highlight any preceding symbols (like arithmetic
         // operators) as being part of numeric literals, we *do* need to highlight the leading
         // negative sign for negative integer literals.
         // BUG: This matches the negative sign in expressions like "[eax-4]", where the operator
         //      actually denotes subtraction. A simple workaround is to add a space: "[eax - 4]".
         [PR['PR_LITERAL'],      /^(?=\$?)\-?\d+(?=d?)(?![^\s\](),\+\-*])/],

         // Significant punctuation: grouping, comma, and arithmetic operators.
         [PR['PR_PUNCTUATION'],  /^[\[\]\(\),+\-*?]/],

This works perfectly when I test it locally, but before I submit a pull request, I wanted to get my work reviewed. The code consists primarily of regular expressions inside a JavaScript wrapper—two technologies in which I am only barely competent! Therefore, I am looking for:

  • general advice on improving the code,
  • specific ways to optimize the regular expressions for maximum performance without sacrificing functionality,
  • anywhere that I did something incorrectly or non-idiomatically.

However, most of the JavaScript that comprises the file's general structure is not up for review, as these conventions were established by the Prettify project and are not things that I can change. (See the existing language modules here.)

The biggest thing that I'm unhappy with is having to provide a nearly-exhaustive list of all the instruction opcodes/mnemonics. This severely bloats the code, which is a concern since it is downloaded with each page view. It also increases the maintenance burden as new instructions are added to future processor generations, which happens much faster than keywords are added to other languages. But I don't see a better approach. Although I've taken a couple of shortcuts at the expense of perfect matching accuracy in order to use regular expressions to their fullest potential, there are a lot of instructions that need to be supported and their syntax doesn't follow a regular pattern. If these aren't recognized and highlighted, they will be difficult to distinguish from user-defined identifiers, defeating much of the point.

What is especially tricky about this compared to other "languages" is, in order for it to be most useful, two different types of syntax must be supported: Intel/MASM and AT&T/GAS. Simple yet representative examples of both are shown below (the code is nonsense, with intentionally inconsistent formatting):

    TheAnswer  DD  42

?TestCode PROC
     xor    eax, eax
2:   push   eax
     push   TheAnswer
8:   push   0x6e696228
d:   mov    ebx,esp        ; save current stack pointer
     or     al,-1
13:  int    21h            ; call interrupt
     ret    0
?TestCode ENDP
.global _start

    movl  $4, %eax            # put some stuff in registers
    xorl  $1,%bx
    movl  $msg,%ecx
    movl  MsgLength, %edx
    int   $0x80               # call interrupt

        .ascii  "Hello, world!\n"
    MsgLength:      .long 14

Though too long to reproduce in full in this question, more complete samples that utilize a variety of instructions and syntactical constructs are included in this live demo of the code for testing purposes. These code samples are cleaned-up output from major compilers, so if the syntax highlighting works here, it should work perfectly on the relatively simple code samples that might be displayed on a web page.

  • 5
    \$\begingroup\$ This is gorgeous! \$\endgroup\$ Jan 17, 2017 at 6:48
  • \$\begingroup\$ You should not get to worried about RegExp performance. The parsing of any code via regexp will take much less time than updating the DOM with the new styles (That is the real bottleneck). I would also suggest that you not write the lang-???.js files directly but create it from a higher level data source (something a little more human readable). This will make it easier to stay up to date, allow you apply optimisations (for regexp) that are hard for us humans to spot, and save you a ton of hard fiddly work. \$\endgroup\$
    – Blindman67
    Jan 17, 2017 at 23:35
  • \$\begingroup\$ Oh, that's a good point, @blindman. I didn't think about the bottleneck being DOM manipulation. Still, I imagine that, like any language, it's possible for someone who is a complete novice to write something so pathologically sub-optimal that it might matter, which is why I asked. As for creating from a higher-level data source, that's intriguing, but I wouldn't know where to begin. I also am hesitant to try and convince the Google Prettify maintainers to accept a sub-project that generates this one language module, so I'd have to maintain that as a separate project myself. Is that worth it? \$\endgroup\$ Jan 18, 2017 at 7:20
  • \$\begingroup\$ What I think would make this more readable would be to break up the regexes onto multiple lines and/or to introduce variables that store the long lists of identifiers (and then concatenate those when building the regex. Either of those would go a long way towards increasing readability and maintainability, without vastly increasing the cost or complexity, but I couldn't find a way to do this. \$\endgroup\$ Jan 18, 2017 at 7:21
  • \$\begingroup\$ Having had another look and a few experiments suggests that the order of the different types is important. When items have been matched they will not be matched again even if they match other regexps . Thus comments first ensures all comments are safe from further matches. Then in order of easy to match to hardest Keywords, types, literals, punctuation, and so on. In the end you are left with the mnemonics and registers which you can class on position more so than textual content. \$\endgroup\$
    – Blindman67
    Jan 18, 2017 at 11:49

1 Answer 1


I'm going to start with a point of design review:

What is especially tricky about this compared to other "languages" is, in order for it to be most useful, two different types of syntax must be supported: Intel/MASM and AT&T/GAS

Why does one configuration have to handle two different syntax styles? On the bases of KISS, of doing one thing well, and of potential ambiguities I would think it makes more sense to register two handlers (perhaps x86-masm and x86-gas), even if they share regexes for opcodes.

specific ways to optimize the regular expressions for maximum performance

That depends on the regex engines, which might do more or fewer optimisations themselves, but basically you want to reduce backtracking. The obvious way you can reduce backtracking for a set of literal strings such as the opcodes is to turn them into a tree. E.g. instead of

[PR['PR_ATTRIB_NAME'],  /^\s(?:crc32|pcmpestri|pcmpestrm|pcmpistri|pcmpistrm|pcmpgtq|popcnt)[bwlq]?\b/i],

you could write

[PR['PR_ATTRIB_NAME'],  /^\s(?:crc32|pcmp(estr[im]|istr[im]|gtq)|popcnt)[bwlq]?\b/i],

That isn't quite a tree, and could be taken further, but backtracking over the p shared by pcmp(...) and popcnt isn't nearly as bad as testing pcmpestr twice (once for pcmpestri and once for pcmpestrm) if someone decides to use pcmpestrz as a name. There is an argument for combining the pcmp?str? as

[PR['PR_ATTRIB_NAME'],  /^\s(?:crc32|pcmp([ei]str[im]|gtq)|popcnt)[bwlq]?\b/i],

but that's more about readability than speed.

One thing which puzzles me about that regex is the (?: I'm sure there's a reason for wanting not to capture the keyword, but it's odd. However, looking at one of the examples you list (specifically lang-ml.js) I note that they're using lookaround for the whole keyword. In the example I picked above (more or less at random), the [bwlq]? is outside the lookaround. I find that suspicious, and I suggest that you double-check the lookarounds.

Oops: misread non-matching group as lookaround.

  • \$\begingroup\$ (1) The unified design is to make it easier for the user, at the expense of complexity in the implementation. In particular, this allows the syntax highlighting to be made the default for the [x86] tag on SO, without requiring users to add fiddly annotations for which specific syntax they're using. I'd rather have the complexity hidden in the library than exposed each time for everyone that needs to use it. (2) So it is, in general, more optimal to do as you've shown here and build a tree. I wouldn't have thought so. Good to know; that probably also answers my "REVIEW" comment question. \$\endgroup\$ Jan 17, 2017 at 12:02
  • \$\begingroup\$ (3) I used the non-capturing syntax because I read a couple of places online that it was more optimal than the capturing syntax. All I really needed was a grouping effect. I thought a lookaround was either the lookahead or lookbehind syntax (the latter, I believe, not supported by JS regexes), and although I do use lookaheads in a few places, the regexes that have [bwlq]?are not one of those places. So I'm not really sure what you mean to say here, or how to double-check it. As I understand it, (?: is not a lookaround. \$\endgroup\$ Jan 17, 2017 at 12:04
  • \$\begingroup\$ @CodyGray To get to a "best of both worlds" - a single js file for include, yet two different syntax implementations - is there a way to detect which syntax is being used and then route to appropriate syntax-specific handler? \$\endgroup\$
    – Mike Brant
    Jan 17, 2017 at 16:22
  • \$\begingroup\$ @mike Is it possible to detect? Maybe. I mean, a $ before an integer literal or a % in front of a register would obviously signify AT&T syntax with no false positives. But false negatives would still be possible. And I'm not really convinced of the advantage. Any problem can be solved by adding another level of indirection, but that also adds a new problem. You'd now need to maintain the detector and routing logic, in addition to each syntax-specific handlers. But now that I have the unified syntax highlighting working, there's no real maintenance burden there. And it wouldn't be any faster. \$\endgroup\$ Jan 18, 2017 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.