# Assembly basics

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

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

> Learn how CPUs execute assembly programs, with examples covering registers, memory, loops, subroutines and the stack.

By Specy

This is a beginner's guide to assembly and how CPUs execute code.

## Introduction

- [Introduction](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/introduction.md): What a CPU actually executes, and where assembly sits between the code you write and those numbers. It ends by taking one M68K instruction apart bit by bit.
- [Using the editor](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/using-the-editor.md): Build, Run, Step, Undo and Stop, a breakpoint, and where the memory, call stack and testcases are. Everything you need to run the programs in the rest of this course.
- [Registers](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/registers.md): The few, very fast slots inside the CPU where a program keeps what it is working on right now. How many each language has, what data and address registers are for, and a program you step through watching them change.
- [Memory](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/memory.md): Memory is one large array of bytes, and every byte has an address. How to read a memory table, and which order the bytes of a number two or four bytes long sit in.
- [Numbers and sizes](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/numbers-and-sizes.md): Bits, bytes, hexadecimal, and the sizes an instruction can touch. Then the same bits read as signed and as unsigned, two's complement, sign extension, and what happens when a number does not fit.
- [Instruction set](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/instruction-set.md): What an instruction set architecture is, and what the assembler does with the mnemonics you write. It ends on how instructions get their names, which is what makes a new language readable fast.
- [Addressing modes](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/addressing-modes.md): An operand can be a number, a register, an address, or an address worked out while the instruction runs. The five common ways of naming what an instruction works on, and one M68K program that uses all of them.
- [Flags](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/flags.md): The single bits a CPU keeps about how the last operation came out, and the conditional branches that read them. Zero, negative, carry and overflow, stepped through in the flags panel.
- [Structure and lifecycle of a program](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/structure-and-lifecycle-of-a-program.md): Code is data like everything else, and the sections a program is written in say where each part of it goes. Then the operating system loads it and the program counter walks through it, one instruction at a time.
- [Memory regions and executable files](https://asm-editor.specy.app/learn/courses/assembly-basics/introduction/memory-regions-and-executable-files.md): Read a simple memory map, distinguish sections from runtime regions, and see how an ELF executable tells the loader where code and data belong.

## Think in assembly

- [Branching and control flow](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/branching-and-control-flow.md): Instructions run one after the other until something jumps somewhere else. An `if` in C becomes a `goto`, and a `goto` becomes a compare and a conditional branch.
- [Loops](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/loops.md): Assembly has no `while` and no `for`, so a loop is a compare, a branch out, and a jump backwards. The same loop is written twice, in M68K and in RISC-V.
- [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/arithmetic-logic-and-bits.md): Add, subtract, multiply and divide, and then the operators that work one bit at a time: and, or, xor, not and the shifts. Masks, and how a program asks about a single bit.
- [Data in memory](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/data-in-memory.md): Arrays and strings, the directives that write them into memory or reserve room for them, and how a program walks one with a pointer. The memory panel is open next to both programs.
- [The stack](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/the-stack.md): A region of memory you push values onto and pop them back off, with one register keeping track of where the top is. Push and pop drawn step by step, then done by hand in M68K.
- [Subroutines](https://asm-editor.specy.app/learn/courses/assembly-basics/think-in-assembly/subroutines.md): Calling a piece of code and returning from it, where the return address is kept, and how the arguments and the return value travel. It carries straight on from the stack, so read that one first.

## Talking to the outside world

- [System calls and traps](https://asm-editor.specy.app/learn/courses/assembly-basics/talking-to-the-outside-world/system-calls-and-traps.md): Printing is not arithmetic, so a program has to ask the environment to do it. The two modes a CPU runs in, the one instruction that hands control over, and why the numbers a program asks with belong to the environment rather than to the CPU.
- [Memory-mapped I/O](https://asm-editor.specy.app/learn/courses/assembly-basics/talking-to-the-outside-world/memory-mapped-io.md): Devices that live at an address instead of behind a trap, a framebuffer written one word per pixel, and the status bit a program polls until the keyboard has something to give it.
- [Interrupts and exceptions](https://asm-editor.specy.app/learn/courses/assembly-basics/talking-to-the-outside-world/interrupts-and-exceptions.md): How exceptions, interrupts and traps transfer control to handlers, and how the editor's simulators support them.

## Optional

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