Skip to main content
πŸŽ“ Claude Code Masterclass Learn AI-assisted development on Udemy β€” plus the companion book on Leanpub & Amazon. Start Learning
Developers exploring RISC-V technology at RISC-V Summit Europe 2026
RISC-V

RISC-V Assembly Tutorial: Your First Program

Learn RISC-V assembly from scratch β€” registers, instruction formats, and a working hello-world you can run in QEMU. A beginner-friendly intro to the open ISA.

LB
Luca Berton
Β· 3 min read

If you want to truly understand a processor, write assembly for it β€” and RISC-V is the best ISA in decades to learn on. It is clean, regular, and free of legacy cruft, which is exactly why universities are adopting it to teach computer architecture. This tutorial gets you from zero to a running RISC-V program, with no RISC-V hardware required.

Developers exploring RISC-V technology at the Summit

Why Learn on RISC-V?

Compared to x86’s decades of accumulated complexity, RISC-V is a breath of fresh air: a small, orthogonal instruction set, fixed-length encodings, and a tidy register model. You can hold the whole base ISA in your head. That is why it shines for education β€” and why it is a joy to learn.

The Registers

RV32I/RV64I have 32 general-purpose registers, x0–x31. Each also has an ABI name describing its conventional role:

RegisterABI nameRole
x0zeroHardwired to 0
x1raReturn address
x2spStack pointer
x5–x7t0–t2Temporaries
x8–x9s0–s1Saved registers
x10–x17a0–a7Arguments / return values
x18–x27s2–s11Saved registers
x28–x31t3–t6Temporaries

The trick worth remembering: x0 is always zero. Writes to it are discarded, reads always return 0. This single design choice eliminates the need for many special instructions β€” a mov is just addi rd, rs, 0, and a nop is addi x0, x0, 0.

Instruction Formats

Every base instruction is 32 bits wide and falls into one of a few regular formats β€” R, I, S, B, U, J. You do not need to memorize the bit layouts to start, but the key idea is regularity: the source and destination register fields sit in the same place across formats, which makes the hardware decoder simple (and the microarchitecture cheaper). The optional compressed (C) extension adds 16-bit forms for code density.

The Core Instructions

A handful of instructions covers most of what you will write:

addi  a0, a0, 5     # a0 = a0 + 5   (add immediate)
add   a0, a1, a2    # a0 = a1 + a2
sub   a0, a1, a2    # a0 = a1 - a2
li    a0, 42        # load immediate (pseudo-instruction)
mv    a1, a0        # copy a0 -> a1 (pseudo: addi a1, a0, 0)
ld    a0, 0(sp)     # load 64-bit word from address in sp
sd    a0, 0(sp)     # store 64-bit word to address in sp
beq   a0, a1, label # branch if equal
jal   ra, func      # jump and link (call), saving return addr in ra
ret                 # return (pseudo: jalr x0, 0(ra))
ecall               # environment call (syscall / SBI request)

Note how many are pseudo-instructions β€” li, mv, ret, nop β€” that the assembler expands into real ones. They make assembly readable without bloating the ISA.

Your First Program: Hello, RISC-V

Here is a complete Linux hello world using the write and exit syscalls via ecall:

.section .data
msg:    .ascii "Hello, RISC-V!\n"
.equ    len, 15

.section .text
.global _start
_start:
    li   a7, 64          # syscall number: write
    li   a0, 1           # fd = stdout
    la   a1, msg         # buffer address
    li   a2, len         # length
    ecall                # invoke kernel

    li   a7, 93          # syscall number: exit
    li   a0, 0           # exit code 0
    ecall

The Linux RISC-V syscall convention: the syscall number goes in a7, arguments in a0–a5, and ecall traps into the kernel (in supervisor mode). The return value comes back in a0.

You need a RISC-V toolchain and QEMU β€” no RISC-V hardware:

# Assemble and link
riscv64-linux-gnu-as -o hello.o hello.s
riscv64-linux-gnu-ld -o hello hello.o

# Run under QEMU user-mode emulation on your x86/Arm machine
qemu-riscv64 ./hello
# -> Hello, RISC-V!

That is the entire loop. Edit, assemble, run β€” watch your instructions execute. For deeper inspection, run it under GDB and single-step to see registers change.

Reading Compiler Output

A powerful way to learn: write tiny C functions and inspect what the compiler emits.

riscv64-linux-gnu-gcc -O2 -S add.c -o add.s   # see the assembly
riscv64-linux-gnu-objdump -d a.out            # disassemble a binary

Comparing your hand-written assembly to the compiler’s output teaches the ABI, calling conventions, and clever optimizations quickly.

The Bottom Line

RISC-V assembly is approachable precisely because the ISA was designed to be clean: 32 regular registers (with x0 hardwired to zero), fixed-width instructions, and a small core set rounded out by readable pseudo-instructions. Write the hello-world above, run it in QEMU, then disassemble some C β€” you will understand the machine far better than any diagram could teach. It is the single best way to feel why RISC-V is special.


Part of my RISC-V series. Next: Debugging RISC-V with GDB & OpenOCD and the memory model & atomics.

Frequently Asked Questions

Is RISC-V assembly easy to learn?

RISC-V is widely considered one of the easiest assembly languages to learn. Its clean RISC design has a small set of regular instructions, a simple register model with 32 general-purpose registers, and no legacy baggage β€” which is exactly why universities increasingly teach computer architecture with it.

How many registers does RISC-V have?

RV32I and RV64I have 32 general-purpose integer registers (x0 to x31). x0 is hardwired to zero. The rest have ABI roles and names: ra (return address), sp (stack pointer), a0 to a7 (arguments and return values), t0 to t6 (temporaries), and s0 to s11 (saved registers).

How do I run RISC-V assembly without a RISC-V computer?

Assemble and link with a RISC-V toolchain (riscv64-linux-gnu-as / -gcc), then run the binary under QEMU user-mode emulation (qemu-riscv64) on your x86 or Arm machine. No physical RISC-V hardware is required to learn and experiment.

#RISC-V #assembly #tutorial #programming #low-level
Share:

πŸ“¬ Don't miss the next one

Get AI & Cloud insights delivered weekly

Join engineers getting practical tips on AI, Kubernetes, Ansible, and Platform Engineering.

Subscribe Free β†’
Luca Berton β€” AI & Cloud Advisor, Docker Captain

Luca Berton

AI & Cloud Advisor Β· Docker Captain Β· KubeCon Speaker

18+ years in enterprise infrastructure. Author of 8 technical books, creator of Ansible Pilot (1M+ YouTube views, 648K site users). Former Red Hat engineer. Speaker at KubeCon EU 2026 and Red Hat Summit 2026.

Free 30-min AI & Cloud consultation

Book Now