# Undesirable highlighting of a keyword name (dh) occuring within an identifier (0dh)

I'm using the x86masm dialect of Assembler and register names are emphasized even when they occur inside of a label or a hexadecimal number. For example, dh is register, but 0dh is a number, yet the dh part of it is emphasized. I haven't had this problem in listings with any other languages, so I'm assuming it's a problem with the language definition. Is there simple way to redefine the language so that this doesn't happen?

\documentclass[12pt]{article}
\usepackage{courier, listings}

\lstset{
language={[x86masm]Assembler},
basicstyle=\ttfamily\small,
frame=single
}

\begin{document}
\begin{lstlisting}
cmp al, 0dh ; dh = register, 0dh = number
je somewhere
\end{lstlisting}

\end{document}

-

use deletekeywords=.... However, you should use another monotypefont like beramono. Courier is not a good looking mono font.

\documentclass[12pt]{article}
\usepackage{courier, listings}

\lstset{
language={[x86masm]Assembler},
basicstyle=\ttfamily\small,
frame=single,
deletekeywords={dh}
}

\begin{document}
\begin{lstlisting}
cmp al, 0dh ; dh = register, 0dh = number
je somewhere
\end{lstlisting}

\end{document}

-
I might still use the dh register somewhere else, and I'd want it emphasized there. Also, this happens with a lot of other registers (bh, ah, ch), and labels are even more likely to have mnemonics in them, so I might as well remove all highlighting if I'm gonna do it this way. –  Adrian Sep 14 '11 at 14:26
@Herbert: I think the problem is that the OP wants the register dh highlighted, but does not want it highlighted in the number 0dh. Seems that to get a complete general solution, the package would be required to parse the actual language to do that as opposed to just match keywords. –  Peter Grill Sep 14 '11 at 16:15
I did some more tests, and this is a listings problem, not just a problem with the language definition. It only happens with literals starting with digits, ending in a keyword, and containing no other letters. This problem is much more likely to occur in assembly than in other languages because of the short mnemonic names and ubiquity of hexadecimal numbers. –  Adrian Sep 14 '11 at 16:40
Okay, I was able to solve this by turning on case sensitivity and making all hexadecimal digits uppercase. –  Adrian Sep 14 '11 at 16:50
@Adrian: Your last two comments might be worth to be given as an answer: They give some more hints about the root of the problem and provide a reasonable workaround. (Note that it is perfectly acceptable to answer your own questions, other members might profit from your solution!) –  Daniel Sep 14 '11 at 19:25

By default, listings treats sequences of digits (and other characters) and sequences of letters separately. More specifically here, listings recognises the digit 0 and prints it out, and then recognises the x86masm keyword dh and highlights it as such.

As Herbert wrote, you can of course delete dh form the list of keywords:

deletekeywords={dh}


but that approach is less than ideal because it completely prevents any instance of that identifier from being highlighted, as shown in the screenshot below.

A preferable approach is to tell listings to treat digits and letters together, not separately, by making digits "letters":

alsoletter=0123456789,


Then, listings will treat 0dh as a single entity; dh will get highlighted, but 0dh won't, as desired.

## Complete code

\documentclass[12pt]{article}
\usepackage{courier, listings}

\lstset{
language={[x86masm]Assembler},
% deletekeywords={dh},    % less than ideal
alsoletter=0123456789,  % better
basicstyle=\ttfamily\small,
frame=single,
}

\begin{document}
\begin{lstlisting}
cmp al, 0dh ; dh = register, 0dh = number
je somewhere
--- TestTestTest: dh ---
\end{lstlisting}

\end{document}

-