# MIPS assembly

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

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

> Learn MIPS through runnable examples, from calculations and strings to reading input, drawing graphics and playing sound.

By Specy

This course is about one architecture, MIPS, 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 is a short tour of the ideas every
assembly language is built on: what a register is, what memory is, how a program branches and how it
keeps a stack. This course assumes you have those and goes back over the same ground slowly, in MIPS.

## Introduction

- [Getting started with MIPS](https://asm-editor.specy.app/learn/courses/mips/introduction/getting-started.md): Build and step through one MIPS instruction, then learn what the architecture is and why it is useful for studying assembly.
- [The 32 registers and their names](https://asm-editor.specy.app/learn/courses/mips/introduction/the-32-registers.md): Learn the numbered and named forms of MIPS registers, use $zero in simple calculations, and choose safe registers for beginner programs.
- [.data, .text and directives](https://asm-editor.specy.app/learn/courses/mips/introduction/data-text-and-directives.md): Use .data and .text to separate declared bytes from instructions, name addresses with labels, and apply the small set of directives needed for words, bytes, strings, reserved space, alignment, and the entry point.
- [Memory, little endian and alignment](https://asm-editor.specy.app/learn/courses/mips/introduction/memory-little-endian-and-alignment.md): How the Playground addresses individual bytes, lays out a word in little-endian order, and aligns labels on four-byte boundaries.
- [Words, halves and bytes](https://asm-editor.specy.app/learn/courses/mips/introduction/words-halves-and-bytes.md): Byte, half and word sizes, decimal and hexadecimal values, and signed and unsigned readings of fixed-width two's-complement patterns.
- [The MIPS instruction set](https://asm-editor.specy.app/learn/courses/mips/introduction/the-mips-instruction-set.md): Read destination-first arithmetic instructions, use immediate constants, and see why one source line can produce more than one 32-bit machine instruction.
- [Loads, stores and immediates](https://asm-editor.specy.app/learn/courses/mips/introduction/loads-stores-and-immediates.md): Use register, immediate, and memory operands; move bytes, halves, and words with loads and stores; and calculate array addresses with offset(base).
- [Comparing two numbers](https://asm-editor.specy.app/learn/courses/mips/introduction/comparing-two-numbers.md): Compare MIPS register values by branching immediately with beq, bne, and comparison pseudo-branches, or by keeping a 0-or-1 result with slt and sltu.
- [Memory regions and executable files](https://asm-editor.specy.app/learn/courses/mips/introduction/memory-regions-and-executable-files.md): Connect MIPS declarations to the Playground memory map, distinguish static data from stack and heap space, and compare Build with ELF loading.

## Think in assembly

- [Branch on compare](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/branch-on-compare.md): Build one-armed if, if/else, and ordered else-if chains from comparison branches, fall-through, labels, and unconditional jumps.
- [Loops](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/loops.md): Build MIPS loops from initialization, a test, a body, an update, and a backward jump. Practice top-tested, guarded bottom-tested, and nested loops.
- [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/arithmetic-logic-and-bits.md): Use hi and lo for multiplication and division results, apply bitwise logic and fixed shifts, and extract fields with masks.
- [Arrays and strings](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/arrays-and-strings.md): Calculate array addresses from a base, index, and element size; walk and copy zero-terminated strings; and flatten row-major grids into linear memory.
- [The stack and $sp](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/the-stack-and-sp.md): Use MIPS register $sp to reserve aligned word slots, push and retrieve values in LIFO order, create local space, and restore the stack pointer exactly.
- [jal, jr, and the course calling convention](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/jal-jr-and-the-calling-convention.md): Use jal and jr to call leaf and non-leaf subroutines, preserve return addresses and saved registers, and pass values with the course's simplified calling convention.
- [Floating point and Coprocessor 1](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/floating-point-and-coprocessor-1.md): Load and calculate with single and double precision, then convert, compare, and pass single precision values.

## Talking to the outside world

- [syscall in the Playground](https://asm-editor.specy.app/learn/courses/mips/talking-to-the-outside-world/syscall.md): Use the Playground's MIPS syscall services to print and read values, preserve returned results and end a program.
- [The bitmap display and keyboard registers](https://asm-editor.specy.app/learn/courses/mips/talking-to-the-outside-world/the-bitmap-display-and-the-keyboard.md): Draw through framebuffer memory, poll the keyboard and write console characters through MIPS memory-mapped registers.
- [Exceptions and coprocessor 0](https://asm-editor.specy.app/learn/courses/mips/talking-to-the-outside-world/exceptions-and-coprocessor-0.md): How this Playground records synchronous exceptions in coprocessor 0, enters a handler, and returns with eret. EPC may be advanced when the handler chooses to skip the faulting instruction.

## Examples

- [Moving values around](https://asm-editor.specy.app/learn/courses/mips/examples/moving-values.md): Place values in registers, preserve the inputs while computing a perimeter, copy a result, and observe subtraction order.
- [Variables in memory and constants](https://asm-editor.specy.app/learn/courses/mips/examples/variables-in-memory.md): Load two initialized words, add a named constant, and store the result in reserved memory while tracing addresses and little-endian bytes.
- [Maximum and unsigned distance](https://asm-editor.specy.app/learn/courses/mips/examples/if-else.md): Trace two routes through a signed maximum and unsigned distance, then write a branch that selects the maximum.
- [Fill an array with a guarded counting loop](https://asm-editor.specy.app/learn/courses/mips/examples/counting-loop.md): Fill a word buffer by tracking its next address, next value, and remaining count, with a guard for zero writes.
- [Sum of an array](https://asm-editor.specy.app/learn/courses/mips/examples/sum-of-an-array.md): Walk a word array to its end label, add its values in $t2, and safely handle an empty array.
- [The largest element](https://asm-editor.specy.app/learn/courses/mips/examples/max-of-an-array.md): A boundary-safe indexed walk that keeps the first signed maximum and its position, including an explicit result for an empty array.
- [Byte length of a string](https://asm-editor.specy.app/learn/courses/mips/examples/string-length.md): Measure a zero-terminated string in bytes by subtracting its start address from its terminator address.
- [Reverse a string in place](https://asm-editor.specy.app/learn/courses/mips/examples/reverse-a-string.md): Scan to the zero terminator, then swap bytes from the ends toward the middle to reverse the string in place.
- [Multiply and divide with hi and lo](https://asm-editor.specy.app/learn/courses/mips/examples/multiply-and-divide.md): Convert days to hours and seconds to minutes using MIPS multiplication and division, then copy results from hi and lo.
- [Masks, shifts, popcount, and leading zeroes](https://asm-editor.specy.app/learn/courses/mips/examples/bit-tricks.md): Answer five questions about one 32-bit pattern with masks, shifts, a fixed 32-pass popcount, and clz.
- [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/mips/examples/subroutine-with-register-arguments.md): Euclid's greatest common divisor for nonnegative integers that are not both zero, using a leaf subroutine with register arguments.
- [Stack arguments and a stack frame](https://asm-editor.specy.app/learn/courses/mips/examples/subroutine-with-stack-arguments.md): Fifth and sixth arguments on the stack, a local and saved return address in a frame, and a subroutine that restores the register it borrows.
- [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/mips/examples/factorial-and-fibonacci.md): Two subroutines that call themselves, each call with a frame of its own on the stack, and the one $ra that makes saving it the first thing either of them does.
- [Bubble sort](https://asm-editor.specy.app/learn/courses/mips/examples/bubble-sort.md): Sort eight words in memory by swapping neighbouring values, then shorten each pass as the sorted end grows.
- [A jump table](https://asm-editor.specy.app/learn/courses/mips/examples/jump-table.md): Use an in-range index to load a code address from a table and jump to the selected operation.
- [Binary search](https://asm-editor.specy.app/learn/courses/mips/examples/binary-search.md): Search twelve sorted words by halving the candidate range; the result is an index or -1 when the value is absent.
- [A 2D array](https://asm-editor.specy.app/learn/courses/mips/examples/two-dimensional-array.md): Three rows of four words, one element reached by row and column, and a whole column added up by stepping a row at a time.
- [Print a string](https://asm-editor.specy.app/learn/courses/mips/examples/hello-world.md): Print text and a number in the Playground console using MIPS syscall services.
- [Read two numbers and print their sum](https://asm-editor.specy.app/learn/courses/mips/examples/sum-of-two-numbers.md): Read two decimal integers, save the first result from $v0, and print their sum.
- [Print a number in any base without help](https://asm-editor.specy.app/learn/courses/mips/examples/number-to-string.md): Repeated division for the digits, a buffer filled backwards from its end, and the same number printed in base 16, 10 and 2.
- [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/mips/examples/drawing-on-the-screen.md): A house, a sun and a sky drawn onto the bitmap display with sw and nothing else, by two subroutines that work out which word each pixel is.
- [A bouncing ball](https://asm-editor.specy.app/learn/courses/mips/examples/bouncing-ball.md): Move a ball within a 32 by 32 display, bounce it at the play-area bounds, and pace each frame with a timed wait.
- [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/mips/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/mips/examples/snake.md): Steer a snake around a 32 by 32 board, tracing how packed cells, body shifts, collision checks, keyboard input, and food placement work together.
- [Flappy ASM](https://asm-editor.specy.app/learn/courses/mips/examples/flappy-asm.md): Build a Flappy ASM game with keyboard input, gravity, moving pipes and collision checks.
- [Play a tune](https://asm-editor.specy.app/learn/courses/mips/examples/play-a-tune.md): Play the first phrase of Ode to Joy from pairs of pitches and durations, then compare waiting and overlapping notes.
- [Chiptune](https://asm-editor.specy.app/learn/courses/mips/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.
- [Sampled sound](https://asm-editor.specy.app/learn/courses/mips/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 MIPS assembly course](https://asm-editor.specy.app/learn/courses/mips/llms-full.txt): every Lecture in order in one file, to search through
