Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท First Steps

What Is Assembly Language?

Learn what assembly language is, how it differs from machine code and C, what an instruction set architecture is, and where assembly is still used today.

Every program you have ever run, whether it was written in Python, JavaScript, Java or C, ends up as a long list of tiny commands that the processor can execute directly. Assembly language is the human-readable way of writing exactly those commands. One line of assembly usually becomes one instruction for the CPU: copy this number, add these two values, jump over there if the result was zero.

Learning assembly does not mean you will write whole applications in it. It means you will finally see what the machine really does underneath your code. That makes you better at debugging crashes, reading compiler output, understanding performance, and understanding security problems such as buffer overflows. This tutorial teaches the ideas with a small runnable toy CPU, and shows you real x86-64 and ARM64 code so you can recognize it in the wild.

Three levels of the same program

Take the simplest possible job: add two integers. In C you write one line:

C
int add(int a, int b) {
    return a + b;
}

A compiler such as GCC or Clang turns that function into instructions for a specific processor. Here is a typical result for a 64-bit Intel or AMD processor, printed as assembly:

NASM
add:
        lea     eax, [rdi+rsi]
        ret

The same function for a 64-bit ARM processor (an Apple Silicon Mac, a phone, a Raspberry Pi 4 or a cloud server with an ARM chip) looks different:

ASM
add:
        add     w0, w0, w1
        ret

Neither listing is something the processor can read. The processor only understands machine code: numbers. An assembler program converts each line into its numeric form. For the ARM add w0, w0, w1 line that number is the four bytes 00 00 01 0b. For the x86-64 ret instruction it is a single byte, c3.

flowchart LR
    A["C source<br/>return a + b"] -->|compiler| B["Assembly text<br/>add w0, w0, w1"]
    B -->|assembler| C["Machine code<br/>bytes in an object file"]
    C -->|linker and OS loader| D["Running program<br/>in memory"]
    D -->|fetch and execute| E[CPU]

From source code to a running program: assembly is the last text form before the bytes.

So there are three levels, and each one is a different way of describing the same behavior:

Level Example Who reads it
High-level language return a + b; People; the same source works on many CPUs
Assembly add w0, w0, w1 People who work close to the hardware; specific to one CPU family
Machine code 00 00 01 0b Only the processor

Assembly sits at a special point: it has a nearly one-to-one relationship with machine code. A compiler, by contrast, may turn one C line into zero instructions, or fifty. That is why assembly is the right level to ask "what did my program really do?".

What an instruction set architecture is

A processor family defines a contract called its instruction set architecture (ISA). The ISA lists the instructions that exist, how they are encoded as bytes, which registers the programmer can use, how memory is addressed, and what each instruction does to the machine state. Software written for an ISA runs on any chip that implements that ISA, even if the chips are built very differently inside.

Three ISAs matter most today:

ISA Where you find it Style
x86-64 (also called AMD64 or x64) Most desktop, laptop and server PCs from Intel and AMD Large, old, with variable-length instructions (1 to 15 bytes)
ARM64 (AArch64) Phones, tablets, Apple Silicon Macs, many servers, Raspberry Pi Cleaner, with fixed 4-byte instructions
RISC-V Embedded chips, research, a growing number of products Small, open and modular

The big difference in philosophy is often summarized as CISC (complex instruction set computer, the x86 tradition) versus RISC (reduced instruction set computer, the ARM and RISC-V tradition). x86 instructions can read memory and compute in a single instruction. RISC-style designs separate loading from computing, which makes each instruction simpler. The lesson x86-64 vs ARM64: Side by Side compares them properly. For now, remember that the names change but the ideas do not: registers, memory, arithmetic, comparisons, jumps, a stack and function calls exist everywhere.

Note: "Assembly language" is not one language. Each ISA has its own, and even for one ISA there are several syntax styles (Intel and AT&T for x86). Concepts transfer between them far better than spelling does.

Reading real machine code

You do not have to take the idea of "assembly is just bytes with names" on faith. The next example is a tiny disassembler written in JavaScript. It knows the handful of real x86 encodings used by the byte list below and turns the bytes back into text. The bytes are genuine x86 machine code, but the code only decodes them; it cannot run them.

JS Disassemble real x86 bytes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice two things. First, the instructions have different lengths: a mov with a 32-bit constant takes five bytes, ret takes one. That is the variable-length design of x86. Second, xor eax, eax is a real, very common way to set a register to zero; you will see why in Logic, Shifts and Bit Tricks.

Now compare ARM64. Every instruction is exactly four bytes, and its bits are split into fixed fields: some bits say which operation, others say which registers. The decoder below pulls those fields out with shifts and masks.

JS Decode fixed-width ARM64 words

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The bytes appear reversed compared with the written word because ARM64 on common systems stores multi-byte values little-endian: the least significant byte first. Memory, Addresses and Endianness explains that in detail.

Why assembly is not "just a harder C"

Assembly has no variables with types, no loops, no functions and no if statement. It only has a fixed set of registers (tiny, very fast storage slots inside the CPU), a big array of bytes called memory, and instructions. Everything else is built by convention:

  • A "variable" is a register or a memory address you decided to use for something.
  • A loop is a comparison followed by a jump backwards.
  • A function call is saving a return address and jumping; a return is jumping back.
  • A "type" exists only in the programmer's head: the same four bytes can be an integer, a float or a pointer, and the CPU will not stop you from mixing them up.

This freedom is exactly why assembly is powerful and dangerous. The processor trusts every instruction. There is no bounds check on arrays and no warning about unused values.

Where assembly is still used

Almost nobody writes large programs in assembly today, because compilers are very good and C or Rust is easier to maintain. Assembly still matters in these places:

  • Boot code and operating-system kernels. The first instructions after power-on, interrupt entry, and context switches must touch registers directly.
  • Performance hot spots. Video codecs, cryptography and math libraries sometimes contain hand-tuned loops using SIMD instructions.
  • Compilers and virtual machines. Developers of compilers, JIT engines and debuggers read and generate assembly daily.
  • Embedded and tiny devices. Startup code and interrupt handlers on microcontrollers.
  • Security work. Reverse engineers analyze programs without source code, and exploit defenders study what a crash really means.
  • Understanding performance. When a loop is slow, the disassembly tells you why.

Even if you never write a line for production, being able to read it is the skill that pays off.

How this tutorial works

A web page cannot assemble and run real x86-64 or ARM code, so this tutorial uses two tools:

  1. Read-only real code. Listings of genuine x86-64 and ARM64 assembly, shown with comments, so you learn the real names and rules.
  2. A runnable toy CPU. A small simulator written in JavaScript, with registers, flags, memory and a stack, whose instructions are modeled on the real ones. You will meet it in Meet the Toy CPU. Programs for it really run in the editors, so you can experiment, single-step and break things safely.

The toy is a teaching tool, not an emulator of Intel or ARM, and each lesson says where the real chip behaves differently. When you finish, you will be able to read compiler output and write real assembly on your own machine with the instructions in Assemblers, Syntax Flavors and Object Files.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the relationship between assembly language and machine code?

    An assembler converts each assembly line into the numeric machine code instruction the CPU executes, so the two correspond almost one to one.
  2. 2. What does an instruction set architecture (ISA) define?

    The ISA is the contract between software and processor. Any chip that implements it can run software built for it.
  3. 3. Which statement about x86-64 and ARM64 instruction lengths is correct?

    x86-64 instructions range from 1 to 15 bytes, while every ARM64 instruction is exactly 4 bytes, which makes decoding simpler.
  4. 4. Why does a single C line not always correspond to a single assembly instruction?

    A compiler optimizes and expands code freely: one line can become many instructions, and a line with no effect can disappear entirely.

Summary

  • Assembly is the readable form of machine code: roughly one text line per CPU instruction.
  • A compiler turns C into assembly, an assembler turns assembly into machine code, and the CPU executes the bytes.
  • An ISA (x86-64, ARM64, RISC-V) defines instructions, registers, encodings and memory rules; each has its own assembly dialect.
  • x86-64 has variable-length instructions; ARM64 has fixed 4-byte ones.
  • Assembly has no types, loops or functions; those are conventions built from registers, memory, compares and jumps.
  • Assembly is still used for boot code, kernels, hot loops, embedded work, security analysis and reading compiler output.
  • This tutorial pairs read-only real code with a runnable toy CPU.

Next: How a CPU Runs Instructions, where you will build the loop at the heart of every processor.