Originally published 2009-09-24

Typesetting MIPS Assembly Code in LaTeX

I'm TAing an introductory architecture course this term, and for each lab assignment I need to present some material in the lab. Since the course is taught with MIPS (using SPIM), my presentation slides sometimes need to contain MIPS assembly language. I use LaTeX and Beamer for my slides, so naturally I turned to the listings package to typeset my code.

As it turns out, though, listings doesn't include a MIPS assmebly language definition, so while it will happily typeset the code, it won't do any sort of syntax hilighting.

So, I created a language definition for MIPS. This definition treats all the directives and instructions supported by SPIM (including pseudoinstructions) as keywords, so listings will format them as such.

See the listings package documentation for how to use this style file. An example is:

\documentclass{article}

\usepackage{listings}
\usepackage{mips}

\begin{document}
\lstset{language=[mips]Assembler}
\begin{lstlisting}
    .text
main:
    # I'm going to output a string.
    li $v0, 4
    la $a0, mystr
    syscall

    # Bye!
    li $v0, 10
    syscall

    .data
mystr:
    .asciiz "This is my string. All mine!\n"
\end{lstlisting}
\end{document}