Skip to content

Instantly share code, notes, and snippets.

@AntonLydike
Last active August 30, 2024 22:37
Show Gist options
  • Save AntonLydike/e339c3c3a4dcab8bc3c620b3fa436cda to your computer and use it in GitHub Desktop.
Save AntonLydike/e339c3c3a4dcab8bc3c620b3fa436cda to your computer and use it in GitHub Desktop.
RISC-V Assembler lstlisting syntax and style
% RISC-V Assembler syntax and style for latex lstlisting package
%
% These are risc-v commands as per our university (University Augsburg, Germany) guidelines.
%
% Author: Anton Lydike
%
% This code is in the public domain and free of licensing
% language definition
\lstdefinelanguage[RISC-V]{Assembler}
{
alsoletter={.}, % allow dots in keywords
alsodigit={0x}, % hex numbers are numbers too!
morekeywords=[1]{ % instructions
lb, lh, lw, lbu, lhu,
sb, sh, sw,
sll, slli, srl, srli, sra, srai,
add, addi, sub, lui, auipc,
xor, xori, or, ori, and, andi,
slt, slti, sltu, sltiu,
beq, bne, blt, bge, bltu, bgeu,
j, jr, jal, jalr, ret,
scall, break, nop
},
morekeywords=[2]{ % sections of our code and other directives
.align, .ascii, .asciiz, .byte, .data, .double, .extern,
.float, .globl, .half, .kdata, .ktext, .set, .space, .text, .word
},
morekeywords=[3]{ % registers
zero, ra, sp, gp, tp, s0, fp,
t0, t1, t2, t3, t4, t5, t6,
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11,
a0, a1, a2, a3, a4, a5, a6, a7,
ft0, ft1, ft2, ft3, ft4, ft5, ft6, ft7,
fs0, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11,
fa0, fa1, fa2, fa3, fa4, fa5, fa6, fa7
},
morecomment=[l]{;}, % mark ; as line comment start
morecomment=[l]{\#}, % as well as # (even though it is unconventional)
morestring=[b]", % mark " as string start/end
morestring=[b]' % also mark ' as string start/end
}
% usage example:
% define some basic colors
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{
% listings sonderzeichen (for german weirdness)
literate={ö}{{\"o}}1
{ä}{{\"a}}1
{ü}{{\"u}}1,
basicstyle=\tiny\ttfamily, % very small code
breaklines=true, % break long lines
commentstyle=\itshape\color{green!50!black}, % comments are green
keywordstyle=[1]\color{blue!80!black}, % instructions are blue
keywordstyle=[2]\color{orange!80!black}, % sections/other directives are orange
keywordstyle=[3]\color{red!50!black}, % registers are red
stringstyle=\color{mauve}, % strings are from the telekom
identifierstyle=\color{teal}, % user declared addresses are teal
frame=l, % black line on the left side of code
language=[RISC-V]Assembler, % all code is RISC-V
tabsize=4, % indent tabs with 4 spaces
showstringspaces=false % do not replace spaces with weird underlines
}
@jocaoth
Copy link

jocaoth commented Nov 8, 2021

please don't forget to use package xcolor when using this.

\usepackage{xcolor}

very useful code snippet!

Copy link

ghost commented Nov 28, 2021

How do I use it
\lstset{language=[RISC-V]{Assembler}}
\begin{lstlisting}
add x5, x3, x1
or x4, x1, x8
sub x2, x5, x4
jalr x1, 0x5234
\end{lstlisting}

doesnt work.

@jocaoth
Copy link

jocaoth commented Nov 28, 2021

If you have correctly included the above file and use all necessary packages then try this:

\begin{lstlisting}[language={[RISC-V]Assembler}

(the {} around assembler is to much I think)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment