RISC V

RISC-V (pronounced “risk-five”) is an open-standard Instruction Set Architecture (ISA) based on the principles of Reduced Instruction Set Computing (RISC). It was designed to be simple, modular, and extensible—ideal for research, education, and commercial use.

Core Components

Base ISA

RV32I, RV64I, and RV128I — 32-bit, 64-bit, and 128-bit versions.

Includes only essential instructions (load, store, arithmetic, control flow).

Standard Extensions

  • M: Integer Multiplication and Division
  • A: Atomic Instructions
  • F: Single-Precision Floating-Point
  • D: Double-Precision Floating-Point
  • C: Compressed Instructions
  • V: Vector Extension
  • H: Hypervisor Extension

Privilege Modes

  • User Mode (U)
  • Supervisor Mode (S)
  • Machine Mode (M)

Assembly

RISC-V assembly is a low-level programming language that directly maps to the RISC-V Instruction Set Architecture (ISA). It uses a load-store architecture, meaning that all computations are done using registers, and memory is accessed only with explicit load and store instructions.

📦 Registers

RISC-V defines 32 general-purpose registers: x0 to x31. These can also be referred to using ABI (Application Binary Interface) names:

RegisterABI NamePurpose
x0zeroConstant 0
x1raReturn address
x2spStack pointer
x3gpGlobal pointer
x4tpThread pointer
x5-x7t0-t2Temporary
x8s0/fpSaved / frame ptr
x9s1Saved register
x10-x17a0-a7Function arguments
x18-x27s2-s11Saved registers
x28-x31t3-t6Temporaries

🧪 RISC-V Toolchain

You can write and test RISC-V assembly using:

  • Assembler: riscv64-unknown-elf-as
  • Linker: riscv64-unknown-elf-ld
  • Simulator: spike, qemu-riscv64
  • Compiler (C to RISC-V): riscv64-unknown-elf-gcc

📜 RISC-V Instruction Cheat Sheet

🧮 Arithmetic

InstructionDescription
addAdd
subSubtract
mulMultiply (M extension)
divDivide (M extension)
remRemainder (modulo)
addiAdd immediate
andiBitwise AND immediate
oriBitwise OR immediate
xoriBitwise XOR immediate

🧠 Logical / Bit Manipulation

InstructionDescription
andBitwise AND
orBitwise OR
xorBitwise XOR
sllShift Left Logical
srlShift Right Logical
sraShift Right Arithmetic

📦 Memory Access

InstructionDescription
lwLoad word (32-bit)
lhLoad halfword (16-bit)
lbLoad byte (8-bit)
lbuLoad byte unsigned
swStore word (32-bit)
shStore halfword
sbStore byte
laLoad address (pseudoinstruction)

🔁 Branch / Control Flow

InstructionDescription
beqBranch if equal
bneBranch if not equal
bltBranch if less than (signed)
bgeBranch if greater or equal (signed)
jalJump and link
jalrJump and link register

🏗️ System / Function

InstructionDescription
ecallSystem call
retReturn from function (jalr)
nopNo operation (addi x0, x0, 0)
liLoad immediate (pseudo)
mvMove register (pseudo)

Hello World in RISC-V Assembly (Linux)

This program uses the Linux syscalls:

  • write (syscall 64) to write to stdout
  • exit (syscall 93) to terminate
# hello.S
.section .data
message:
    .ascii "Hello, world!\n"
len = . - message
 
.section .text
.globl _start
_start:
    li a7, 64             # syscall: write
    li a0, 1              # fd: stdout
    la a1, message        # buffer address
    li a2, len            # buffer length
    ecall                 # invoke syscall
 
    li a7, 93             # syscall: exit
    li a0, 0              # exit code 0
    ecall