# RISC-V assembly

[Courses](https://asm-editor.specy.app/learn/courses.md) › RISC-V assembly

Web page: https://asm-editor.specy.app/learn/courses/risc-v

> Write your first RISC-V programs, then explore arrays, subroutines and the move from 32-bit to 64-bit code.

By Specy

This course is about one architecture, RISC-V, and about writing programs for it.

If you have never written assembly before, read
[Assembly basics](https://asm-editor.specy.app/learn/courses/assembly-basics.md) first. It covers registers, memory, branching, the
stack and the rest once and quickly, using whichever language made each point clearest. This course
takes all of that for granted and goes over the same topics again in more depth, in RISC-V only.

## Introduction

- [Introducing RISC-V](https://asm-editor.specy.app/learn/courses/risc-v/introduction/getting-started.md): Where RISC-V came from, what makes its instruction set open, where it is used, and how this course will introduce it.
- [The 32 integer registers and their names](https://asm-editor.specy.app/learn/courses/risc-v/introduction/the-32-registers.md): What a register is, how RISC-V's numbered and ABI names refer to the same storage, and why zero is special.
- [Memory, little endian and alignment](https://asm-editor.specy.app/learn/courses/risc-v/introduction/memory-little-endian-and-alignment.md): How RISC-V arranges a multi-byte value at consecutive addresses, and how natural alignment matches a starting address to an access size.
- [Words, halves and bytes](https://asm-editor.specy.app/learn/courses/risc-v/introduction/words-halves-and-bytes.md): RISC-V byte, halfword and word sizes, their signed and unsigned ranges, and how sign extension and zero extension place smaller values in 32-bit registers.
- [The RISC-V instruction set](https://asm-editor.specy.app/learn/courses/risc-v/introduction/the-risc-v-instruction-set.md): Read the mnemonic and operands of a simple RISC-V instruction, distinguish destinations from sources, and see how assembler pseudo-instructions fit into the base instruction set and its extensions.
- [Loads, stores and immediates](https://asm-editor.specy.app/learn/courses/risc-v/introduction/loads-stores-and-immediates.md): Use immediate values, choose the right load or store size, calculate offset(base) addresses, and access array elements through registers.
- [Asking a question with a branch](https://asm-editor.specy.app/learn/courses/risc-v/introduction/asking-a-question-with-a-branch.md): Use RISC-V's six register branches, follow taken and fall-through paths, compare with zero, and choose signed or unsigned ordering.
- [.data, .text and directives](https://asm-editor.specy.app/learn/courses/risc-v/introduction/data-text-and-directives.md): How .text and .data organize a RISC-V source file, how labels and common directives lay out static data, and how this editor chooses main as the starting point.
- [Going 64-bit](https://asm-editor.specy.app/learn/courses/risc-v/introduction/going-64-bit.md): Choose RV64 mode, work with 64-bit registers and doublewords, and select the right extension or arithmetic width for 32-bit values.
- [Memory regions and executable files](https://asm-editor.specy.app/learn/courses/risc-v/introduction/memory-regions-and-executable-files.md): Inspect RISC-V data across sections, read the editor's memory map, and distinguish RV32/RV64 execution from ELF executable files.

## Think in assembly

- [if, else and jump tables](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/if-else-and-jump-tables.md): Lay out if/else paths with branches and fall-through, chain tests for several ranges, and use a checked table of addresses to select among consecutive cases.
- [Loops](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/loops.md): A loop is a branch backwards and a counter you keep yourself. Why the test belongs at the bottom, nested loops, and walking an array against an end pointer.
- [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/arithmetic-logic-and-bits.md): Build complete signed and unsigned products, handle defined division edge cases, and use masks, shifts and logical operations to extract, pack, count and select bits.
- [Arrays and strings](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/arrays-and-strings.md): Calculate byte addresses for indexed and two-dimensional arrays, then walk, copy, compare and edit zero-terminated strings.
- [The stack and sp](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/the-stack-and-sp.md): The stack is memory and sp is an ordinary register, so a push is a subtraction and a store that you write. Saving several registers with one adjustment, room for locals, and giving every byte of it back.
- [jal, ret and the calling convention](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/jal-ret-and-the-calling-convention.md): Call and return with jal, ra and ret; pass arguments and results; preserve registers in leaf, non-leaf and recursive subroutines; and build aligned stack frames.
- [Floating point and the F and D extensions](https://asm-editor.specy.app/learn/courses/risc-v/think-in-assembly/floating-point-and-the-f-and-d-extensions.md): Single and double precision, floating-point registers, NaN boxing, conversions, comparisons, status flags, and the calling convention.

## Talking to the outside world

- [ecall](https://asm-editor.specy.app/learn/courses/risc-v/talking-to-the-outside-world/ecall.md): The one instruction that asks the simulator for something, the service number in a7 that says what, and the registers each service reads and answers in. Printing, reading, the clock and ending a program.
- [The bitmap display and the keyboard registers](https://asm-editor.specy.app/learn/courses/risc-v/talking-to-the-outside-world/the-bitmap-display-and-the-keyboard.md): RISC-V uses lw and sw for a bitmap display backed by memory and for four console device registers at 0xffff0000. Configure the Screen panel with @screen, calculate a pixel address, and poll the Ready bit to echo keys.
- [When an instruction cannot be carried out](https://asm-editor.specy.app/learn/courses/risc-v/talking-to-the-outside-world/exceptions.md): Set up utvec and ustatus, then handle a misaligned load by reading its fault details, moving uepc past the instruction, and returning safely with uret.

## Examples

- [Moving values around](https://asm-editor.specy.app/learn/courses/risc-v/examples/moving-values.md): Calculate a rectangle's perimeter, copy it between registers, and check each result.
- [Variables in memory and constants](https://asm-editor.specy.app/learn/courses/risc-v/examples/variables-in-memory.md): Load labelled words, add an assembler constant, store the result, and check its little-endian bytes in memory.
- [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/risc-v/examples/if-else.md): Use a signed branch to pick the larger of two registers and turn their difference into a nonnegative distance.
- [Fill an array with the numbers from 1 to 10](https://asm-editor.specy.app/learn/courses/risc-v/examples/counting-loop.md): A loop that writes ten words into reserved room, stepping the pointer itself and counting down to zero, with the result read in the memory panel.
- [Sum of an array](https://asm-editor.specy.app/learn/courses/risc-v/examples/sum-of-an-array.md): Six numbers sitting in memory, added up with a pointer that steps through them until it reaches the end, and the total left in t2.
- [The largest element](https://asm-editor.specy.app/learn/courses/risc-v/examples/max-of-an-array.md): Find the largest signed value in a nonempty word array and keep the index where it first appears.
- [Length of a string](https://asm-editor.specy.app/learn/courses/risc-v/examples/string-length.md): A walk to the zero byte at the end of a string, with the length worked out as the difference between two addresses.
- [Reverse a string in place](https://asm-editor.specy.app/learn/courses/risc-v/examples/reverse-a-string.md): Find the end of a zero-terminated string, then reverse its bytes in place with two inward-moving pointers.
- [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/risc-v/examples/multiply-and-divide.md): Use mul, div, and rem; recover the high half of a product; and split seconds into hours, minutes, and seconds.
- [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/risc-v/examples/bit-tricks.md): Five questions about one 32-bit value answered with masks, shifts and bounded bit-counting loops.
- [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/risc-v/examples/subroutine-with-register-arguments.md): Call a leaf subroutine with aligned sp, pass arguments in a0 and a1, return a result in a0, and follow the caller-saved register rules.
- [Stack arguments and a stack frame](https://asm-editor.specy.app/learn/courses/risc-v/examples/subroutine-with-stack-arguments.md): A clearly labelled stack-argument convention, aligned caller and callee frames, a local that survives a nested call, and preserved fp and s1.
- [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/risc-v/examples/factorial-and-fibonacci.md): Recursive factorial and Fibonacci with aligned per-call frames, saved return addresses and arguments, and a frame-building exercise.
- [Bubble sort](https://asm-editor.specy.app/learn/courses/risc-v/examples/bubble-sort.md): Sort words in place with adjacent swaps, a shrinking inner loop, and an outer loop for the remaining passes.
- [A jump table](https://asm-editor.specy.app/learn/courses/risc-v/examples/jump-table.md): A checked index selects one of four handler addresses in memory, then jr jumps to the selected operation.
- [Binary search](https://asm-editor.specy.app/learn/courses/risc-v/examples/binary-search.md): Find a value in a sorted array by maintaining an inclusive range and discarding half after each midpoint probe.
- [A 2D array](https://asm-editor.specy.app/learn/courses/risc-v/examples/two-dimensional-array.md): Reach a word by zero-based row and column, then sum a column by advancing one row stride at a time.
- [Print a formatted line](https://asm-editor.specy.app/learn/courses/risc-v/examples/hello-world.md): Combine text, a signed number and individual characters into one line of console output with Playground services.
- [Read two numbers and print their sum](https://asm-editor.specy.app/learn/courses/risc-v/examples/sum-of-two-numbers.md): Read two decimal integers, save the first before reusing a0, and print their sum.
- [Print a nonnegative number in bases 2–36](https://asm-editor.specy.app/learn/courses/risc-v/examples/number-to-string.md): Use quotient and remainder to build a number string backward, then print values in hexadecimal, decimal, and binary.
- [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/risc-v/examples/drawing-on-the-screen.md): Build horizontal runs, rectangles and a disc from pixel stores, then combine the drawing subroutines into a house scene.
- [A bouncing ball](https://asm-editor.specy.app/learn/courses/risc-v/examples/bouncing-ball.md): Animate a ball with rectangle drawing, signed boundary checks, a clock-driven time bar, and a paced frame loop.
- [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/risc-v/examples/keyboard-control.md): The receiver registers polled once a frame for w, a, s and d, a direction kept in registers, and a square that keeps going until you steer it somewhere else.
- [The snake game](https://asm-editor.specy.app/learn/courses/risc-v/examples/snake.md): Build a complete snake game from packed board cells, an array-backed body, keyboard input, collision checks, food placement, and small screen updates.
- [Flappy ASM](https://asm-editor.specy.app/learn/courses/risc-v/examples/flappy-asm.md): Build a Flappy ASM game with keyboard input, gravity, moving pipes and collision checks.
- [Flappy ASM in C](https://asm-editor.specy.app/learn/courses/risc-v/examples/flappy-asm-in-c.md): Recreate the assembly Flappy ASM game in C using the simulator API.
- [Chiptune](https://asm-editor.specy.app/learn/courses/risc-v/examples/chiptune.md): Play a short tune on the Synth: a pulse lead, a triangle bass and noise drums on three channels, stepped by a table in memory.
- [Play a tune](https://asm-editor.specy.app/learn/courses/risc-v/examples/play-a-tune.md): Play the first phrase of Ode to Joy from a table of pitches and durations, one note at a time with service 33.
- [Sampled sound](https://asm-editor.specy.app/learn/courses/risc-v/examples/sampled-sound.md): Compute a plucked sound into memory, upload it into the Synth's Sample memory and play it at eight pitches by changing the playback rate.

## Optional

- [The whole RISC-V assembly course](https://asm-editor.specy.app/learn/courses/risc-v/llms-full.txt): every Lecture in order in one file, to search through
