[Tex/LaTex] Adding custom language to listings causing keyword problem

listings

I am trying to make my own syntax highlighting for ARM assembly by defining a language as follows:

\lstdefinelanguage{ASM}{
    morekeywords={b, ble, blt, bne, 
        ldr, str, 
        r0, r1, r2, r3, r4, r5, r6, r7, rr8, r9, r10, r11, r12
        sp  },
    sensitive=false, % keywords are not case-sensitive
    morecomment=[l]{//}, % l is for line comment
    morecomment=[s]{/*}{*/}, % s is for start and end delimiter
    morestring=[b]" % defines that strings are enclosed in double quotes
} % 

However when I do:

\lstset{language=ASM}
\begin{lstlisting}[style = ASM]
    .section .init
    .global _start

    _start:
        ldr sp, =8000
        b kernel_main
\end{lstlisting}

I am told "undefined control sequence ldr" and "undefined control sequence b". Removing them from the keywords solves the issue but then I have no syntax highlighting. What did I do wrong?

Best Answer

Looking at what you have, you seem to be missing a comma at the end of the line ...r11, r12 and your lstlisting environment should probably be \begin{lstlisting}[language = ASM], rather than using style. With these two fixes your code produces:

enter image description here

Here is the full minimal working example:

\documentclass{article}
\usepackage{listings}

\lstdefinelanguage{ASM}{
    morekeywords={b, ble, blt, bne,
        ldr, str,
        r0, r1, r2, r3, r4, r5, r6, r7, rr8, r9, r10, r11, r12,
        sp  },
    sensitive=false, % keywords are not case-sensitive
    morecomment=[l]{//}, % l is for line comment
    morecomment=[s]{/*}{*/}, % s is for start and end delimiter
    morestring=[b]" % defines that strings are enclosed in double quotes
} %

\begin{document}

\begin{lstlisting}[language = ASM]
    .section .init
    .global _start

    _start:
        ldr sp, =8000
        b kernel_main
\end{lstlisting}

\end{document}