Assembly & the CPU

assembly language

Machine code — the actual bytes the CPU executes — is unreadable to humans: an add instruction might be the byte 0x01 followed by an operand byte. Assembly language is a thin, human-readable spelling of that same machine code. Instead of 0x01, you write add; instead of a register number, you write rax. Each line of assembly corresponds, almost one-to-one, to a single machine instruction.

Assembly is the lowest-level language a person normally writes or reads. It is specific to one instruction set architecture — x86-64 assembly and ARM assembly are different languages — because it is just a textual stand-in for that architecture's instructions. A program called an assembler translates assembly text into the object-file bytes of machine code; the reverse, turning machine code back into readable assembly, is called disassembly. Because the mapping is so direct, assembly exposes everything the C language hides: which registers hold what, every memory access, the exact stack manipulation around a call.

Almost nobody writes whole programs in assembly today — it is tedious and not portable — but reading it is a genuinely useful skill. When you compile C with optimisations and look at the assembly, you see exactly what the machine will do, which settles questions a high-level reading cannot: whether a loop was vectorised, whether a check was optimised away, why a microbenchmark is fast or slow. Compilers also let you drop small inline assembly snippets when you need an instruction the language has no other way to reach.

The C statement x = x + 1; might compile to the single assembly line add DWORD PTR [rbp-4], 1 — readable shorthand for the machine bytes that increment x in memory.

Assembly is a near one-to-one human spelling of machine code, specific to one ISA.

Assembly is not a single universal language: each ISA has its own, and even x86-64 has two common syntaxes (Intel and AT&T) that swap operand order and notation, so the same instruction can look quite different. Assembly is also not 'faster than C' by default — a good optimising compiler usually matches or beats hand-written assembly.

Also called
assemblyasm組譯語言