ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Follow publication

Member-only story

RISC-V Instruction-Set Cheatsheet

Erik Engheim
ITNEXT
Published in
9 min readMay 15, 2022

--

A NUMBER OF people have tried to make sheets giving an overview of the RISC-V instruction-set, so here is my variant. I have tried to find a balance between being useful and easy to read. That means some things I have excluded from this overview. For instance, most instructions dealing with immediate values do sign-extension, and I chose to not document that in this overview. What is sign-extension you ask?

Many of the arithmetic operations do sign extension. rd is destination register. rs1 and rs1 are input registers. imm refers to an immediate value. imm20 is a 20-bit immediate value while imm12 is a 12-bit immediate value.

Computers only store binary numbers with ones and zeros. Thus to represent negative numbers we utilize the fact that integer numbers wrap around. Consider the classic mechanical counter shown below. If it says 9999, then what does adding 4 to it result in? It gives you the number 0003, because numbers wrap around. In essence 9999 acts as if it was -1. What did it say 9998 and you added 4? Then the result would be 0002, hence 9998 acts as if it was -2. Thus we can use high number values to represent negative numbers.

Digital computers work much the same. Thus binary numbers wrap around once you get to the max value. One complicating factor when dealing with this is that what constitutes -1, -2, -3 and so on depends on the number of digits used. With the decimal number system, if you only get to represent numbers with a single digit then 9 represents -1 and 8 represents -2, while if you got two digits then 99 represents -1, 98 is -2, 97 is -3, and so on.

The exact same thing applies to binary numbers. Sign extension for decimal numbers would mean that you add 9s as the first digit each time you add digits if the number was negative. Hence if 9 represents a signed number then 09 would not work as it would be a positive number. You would have to turn it into 99. For binary numbers, you check if the first digit is a 1 and then add 1s to the front as you add bits. Sign extensions apply even to logical operations such as AND and OR.

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Erik Engheim

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Write a response