# x86-64 assembly

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

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

> Write Linux programs in NASM syntax, from basic instructions to function calls, system calls and vector operations.

By Specy

Write x86-64 assembly programs in NASM syntax and run them in the editor's Linux environment.

If you have never written assembly before, read
[Assembly basics](https://asm-editor.specy.app/learn/courses/assembly-basics.md) first. It introduces registers, memory,
branching and the stack. This course builds on those ideas with detailed explanations and
runnable x86-64 programs.

## Introduction

- [Getting started with x86-64](https://asm-editor.specy.app/learn/courses/x86/introduction/getting-started.md): Meet x86-64, NASM syntax, and the Linux-style environment used by this course. Run a first program and see what you will learn in the course.
- [The 16 registers and their halves](https://asm-editor.specy.app/learn/courses/x86/introduction/the-16-registers-and-their-halves.md): The sixteen general-purpose integer register families, their overlapping 64-, 32-, 16-, and 8-bit names, and how narrow writes affect the remaining bits.
- [Memory, little endian and sizes](https://asm-editor.specy.app/learn/courses/x86/introduction/memory-little-endian-and-sizes.md): Labels and byte addresses, little-endian integers, operand widths, and explicit memory sizes in NASM.
- [Bytes, words, dwords and qwords](https://asm-editor.specy.app/learn/courses/x86/introduction/bytes-words-dwords-and-qwords.md): The four x86 integer widths, signed and unsigned values, arithmetic wrapping, and widening values with movzx and movsx.
- [The x86-64 instruction set](https://asm-editor.specy.app/learn/courses/x86/introduction/the-x86-instruction-set.md): Learn about mnemonics, register, immediate, and memory operands, and how NASM determines operand sizes. Check which operand forms each instruction accepts.
- [Effective addresses](https://asm-editor.specy.app/learn/courses/x86/introduction/effective-addresses.md): Calculate x86-64 addresses, access array elements by index, and use lea to calculate an address without reading memory.
- [The flags register](https://asm-editor.specy.app/learn/courses/x86/introduction/the-flags-register.md): How x86 records zero, sign, unsigned carry or borrow, and signed overflow at an operation's chosen width.
- [Sections, directives and labels](https://asm-editor.specy.app/learn/courses/x86/introduction/sections-directives-and-labels.md): Use sections to lay out code and data, labels to name addresses, and equ, $, and $$ to calculate sizes.
- [Memory regions and executable files](https://asm-editor.specy.app/learn/courses/x86/introduction/memory-regions-and-executable-files.md): Inspect x86 data addresses and learn how source sections and ELF files describe a program’s memory.

## Think in assembly

- [cmp and the conditional jumps](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/cmp-and-the-conditional-jumps.md): Use cmp, conditional jumps, fall-through and labels to compare signed and unsigned values. Combine conditions with short-circuit evaluation and make three-way comparisons.
- [Loops](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/loops.md): Build pre-tested and countdown loops, handle zero counts safely, traverse arrays, and reset state correctly in nested loops.
- [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arithmetic-logic-and-bits.md): Fixed-width arithmetic, full and truncated products, preparing dividends, bit masks, tests, shifts and rotates in x86-64.
- [Arrays, strings and the string instructions](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arrays-strings-and-the-string-instructions.md): Walk arrays by index or pointer, distinguish fixed-length data from zero-terminated strings, and use movs, stos, scas and cmps with repeat prefixes and element counts.
- [The stack, push and pop](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/the-stack-push-and-pop.md): Use rsp, push and pop on a downward-growing stack, then reserve and release temporary slots with matching sub and add instructions.
- [call, ret and the System V convention](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/call-ret-and-the-system-v-convention.md): Use call and ret, pass integer and pointer arguments, preserve registers, align the stack, and save values during recursive calls with the System V convention.
- [Floating point and the x87 stack](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/floating-point-and-the-x87-stack.md): Read binary32 and binary64 values, calculate with the x87 register stack, and branch on floating-point comparisons.
- [SSE2 scalar and packed arithmetic](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/sse-and-packed-arithmetic.md): Use x86-64's 128-bit XMM registers for scalar and packed binary floating-point arithmetic, conversions, comparisons, and System V function calls.
- [AVX and 256-bit vectors](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/avx-and-wider-vectors.md): Use 256-bit YMM registers for eight binary32 or four binary64 lanes per instruction. Learn three-operand forms, upper-half behaviour, permutations between halves, broadcasts, fused multiply-add, horizontal sums and alignment.

## Talking to the outside world

- [syscall and the Linux x86-64 ABI](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/syscall-and-the-linux-abi.md): Use Linux x86-64 system calls to write output and exit. Learn the argument registers, file descriptors, error results, and partial writes.
- [Ports, memory-mapped devices, and kernel access](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/ports-devices-and-the-kernel.md): How x86 port I/O and memory-mapped devices work, how Linux controls a process's access, and how to map and release anonymous pages safely with mmap.
- [Interrupts, exceptions and signals](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/interrupts-exceptions-and-signals.md): How the CPU and Linux handle exceptions, interrupts and system calls, and how a program receives a signal.

## Examples

- [Moving values around](https://asm-editor.specy.app/learn/courses/x86/examples/moving-values.md): Follow values through registers and memory, then copy a qword and store a result in a short exercise.
- [Variables in memory and constants](https://asm-editor.specy.app/learn/courses/x86/examples/variables-in-memory.md): Use variables in memory and an equ constant to calculate an area, then write an immediate value to an eight-byte variable.
- [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/x86/examples/if-else.md): Find the larger of two signed qwords with one conditional jump, then practice the pattern with memory variables.
- [Fill an array with the numbers from 1 to 10](https://asm-editor.specy.app/learn/courses/x86/examples/counting-loop.md): Fill ten qwords with a counting loop and inspect their bytes in memory.
- [Sum of an array](https://asm-editor.specy.app/learn/courses/x86/examples/sum-of-an-array.md): Add the qwords in an array using a pointer, then extend the array and check the sum.
- [The largest element](https://asm-editor.specy.app/learn/courses/x86/examples/max-of-an-array.md): Find the largest signed element of a qword array by keeping the largest value seen so far.
- [Length of a string](https://asm-editor.specy.app/learn/courses/x86/examples/string-length.md): Find a string's length with an ordinary loop and a bounded repne scasb scan, then practice handling a missing terminator.
- [Reverse a string in place](https://asm-editor.specy.app/learn/courses/x86/examples/reverse-a-string.md): Reverse a string in place by swapping the characters at its two ends and moving towards the middle.
- [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/x86/examples/multiply-and-divide.md): Compare unsigned and signed division, inspect a full multiplication product, and practise preparing rdx:rax.
- [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/x86/examples/bit-tricks.md): Use test to check whether a number is even, count its set bits by clearing the lowest one, multiply by ten with two shifts, and read a single bit with bt.
- [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/x86/examples/subroutine-with-register-arguments.md): Pass arguments in rdi and rsi and return a result in rax using the System V calling convention. Follow the return address on the stack through call and ret.
- [Stack arguments and a stack frame](https://asm-editor.specy.app/learn/courses/x86/examples/subroutine-with-stack-arguments.md): Pass a seventh argument on the stack, use a frame pointer for local variables, and save and restore a callee-saved register.
- [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/x86/examples/factorial-and-fibonacci.md): Recursive factorial and Fibonacci subroutines, with stack space and saved registers to keep values across calls.
- [Bubble sort](https://asm-editor.specy.app/learn/courses/x86/examples/bubble-sort.md): Sort six numbers in memory using bubble sort, nested loops and one index register to access neighbouring elements.
- [A jump table](https://asm-editor.specy.app/learn/courses/x86/examples/jump-table.md): Use a jump table to select a case, and check that the index is in range before jumping.
- [Binary search](https://asm-editor.specy.app/learn/courses/x86/examples/binary-search.md): Use binary search on a sorted qword array. Halve a half-open range to find the target's index or report that it is absent.
- [A 2D array](https://asm-editor.specy.app/learn/courses/x86/examples/two-dimensional-array.md): Use row-major indexing to find an array element and loop through a row or column.
- [Print a string](https://asm-editor.specy.app/learn/courses/x86/examples/hello-world.md): Print fixed text in the console with Linux write and add newline bytes for line breaks.
- [Print an unsigned number in bases 2 through 36](https://asm-editor.specy.app/learn/courses/x86/examples/number-to-string.md): Convert an unsigned number to character bytes with repeated division, then print it in decimal, hex, binary, or another base through 36.
- [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/x86/examples/drawing-on-the-screen.md): Draw a house and a sun with rectangles, pixel addresses and rep stosd.
- [A bouncing ball](https://asm-editor.specy.app/learn/courses/x86/examples/bouncing-ball.md): Animate a bouncing ball with framebuffer pages, partial redraws and vertical-blank timing.
- [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/x86/examples/keyboard-control.md): Read keyboard events without waiting and track held arrow keys to move a square.
- [Paint with the mouse](https://asm-editor.specy.app/learn/courses/x86/examples/mouse-paint.md): Find the Screen pointer, convert its positions to pixels, and paint with button and wheel events.
- [Beep a tune on the PC speaker](https://asm-editor.specy.app/learn/courses/x86/examples/pc-speaker.md): Play a tune on the PC speaker using input events, a table of frequencies and durations, and nanosleep.
- [Play a tune](https://asm-editor.specy.app/learn/courses/x86/examples/play-a-tune.md): Play a piano tune with drums by writing MIDI messages to the synthesizer.
- [Sampled sound](https://asm-editor.specy.app/learn/courses/x86/examples/sampled-sound.md): This program generates fading square-wave samples and plays a scale through /dev/dsp.

## Optional

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