Skip to content
systemcoding.dev
Assembly

What Assembly Language Is and Why Systems Programmers Still Read It

Assembly is a readable spelling of machine code, one line per instruction. See what it is, how to view it for any C function, and when reading it pays off.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the GNU, Intel and RISC-V logos.

Almost nobody writes assembly for a living, and almost every systems programmer ends up reading it. The reason is a single fact: the compiler's output is the only version of your program the processor ever sees, so when the source and the behavior disagree, the assembly settles the argument. A crash with a bare address, a loop that is slower than it should be, an optimizer that deleted your check: all of them are answered one level down. This post explains what that level is, how to look at it for any function you own, and where the habit stops paying off.

Assembly is machine code with names

A processor executes machine code, a stream of bytes. On x86-64, the bytes 8d 47 03 mean "add 3 to the value in the rdi register and put the result in eax". Nobody wants to read bytes, so an assembler accepts a text spelling of the same instruction and turns it into those bytes:

asm
lea eax, [rdi+3]

That line is assembly language. Each line is one instruction (mostly), each instruction has a short name called a mnemonic (lea, mov, add, ret), and the names after it are operands: registers, constants or memory references. There is no loop keyword, no type, no variable. A register is a tiny storage slot inside the processor, and the instructions move values between registers and memory, combine them, and jump around.

Assembly is therefore not one language. It is a family of spellings, one per instruction set. x86-64 assembly is not ARM assembly is not RISC-V assembly. Within x86-64 there are even two competing spellings of the same instruction, Intel syntax and AT&T syntax, which a later post in this series compares. We use Intel syntax throughout, the one the Intel and AMD manuals use.

See the assembly for your own function

You do not need to write anything to start reading. Give GCC 14 a small C file and ask it to stop after compiling:

add3.cc
int add3(int a)
{
    return a + 3;
}
Shell
gcc -O2 -S -masm=intel -o add3.s add3.c

The -S flag stops before assembling and writes the assembly text, and -masm=intel selects Intel syntax. Strip the directives and the function body is two lines:

asm
add3:
        lea     eax, [rdi+3]
        ret

The argument a arrived in edi, the low half of rdi, because of the calling convention (the rules for which register holds which argument, covered later in this series). lea computes rdi + 3 and stores it in eax, which is where a 32-bit return value goes, and ret returns. The compiler chose lea rather than add because lea can write to a different register than it reads, saving a mov. That one choice is already a small lesson: the compiler picks instructions by what the hardware does well, not by how the C reads.

Assemble that file and print the bytes next to the text:

Shell
gcc -O2 -c add3.c -o add3.o
objdump -d -M intel add3.o
Text
add3.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <add3>:
   0:	8d 47 03             	lea    eax,[rdi+0x3]
   3:	c3                   	ret

The middle column is the machine code: 8d 47 03 for lea and c3 for ret. The tool reading bytes back into text is a disassembler. The mapping is nearly one to one, which is what makes assembly different from every other language: nothing is hidden, and nothing is added.

What you can answer from assembly that you cannot from C

Four situations come up repeatedly, and each is a place where the C source is silent.

A crash with no source. A core file or a log line gives you an instruction address and a register dump. A debugger can show the faulting instruction, for example mov eax, DWORD PTR [rdx] with rdx equal to 0x0, which says "a null pointer was dereferenced here" before you have found the line. Without the ability to read that one instruction you are guessing.

What the optimizer did. An optimizer is allowed to change anything that does not change the program's defined behavior. A check for signed overflow written as if (a + 1 < a) is undefined behavior when a is INT_MAX, so GCC may compile the whole test away. Reading the assembly is the quick way to find out that your safety check no longer exists.

Why something is slow. Whether a loop was vectorized, whether a function was inlined, whether a division by a constant became a multiply: each is visible in a dozen lines of assembly and invisible in the source.

Code with no source at all. A vendor binary, a firmware blob, a suspicious sample, or a library you only have as a .so. A reader of assembly can still work.

Assembly is also the interface between everything. System calls, interrupt handlers, context switches, and the first instructions after a processor resets are written in it because no higher-level language can express "load this special register". Operating system and embedded work meet it whether they want to or not.

How little you need to learn

Reading is much cheaper than writing. A reader needs three ideas:

  1. The registers: which names exist and what each is conventionally used for. The next post covers x86-64's sixteen general-purpose ones.

  2. The operand order. In Intel syntax the destination comes first: mov rax, rbx copies rbx into rax.

  3. The memory brackets. [rdi+3] means "the memory at the address rdi + 3", while rdi+3 without brackets (only legal in lea) means the number itself.

Here is a loop with those three ideas applied. It sums n 64-bit integers starting at the address in rdi:

sum.sasm
; long sum(const long *p, long n)   p in rdi, n in rsi, result in rax
sum:
        xor     eax, eax            ; total = 0
        test    rsi, rsi            ; is n zero?
        jle     .done               ; if n <= 0, return 0
.loop:
        add     rax, [rdi]          ; total += *p
        add     rdi, 8              ; p++ (8 bytes per long)
        dec     rsi                 ; n--
        jnz     .loop               ; repeat while n != 0
.done:
        ret

Reading it top to bottom, xor eax, eax is the idiom for setting a register to zero, test rsi, rsi followed by jle handles the empty case, and the three-instruction body is the whole loop. Every jump target is a label starting with a dot. If you call sum with a pointer to the array {4, 5, 6} and n equal to 3, the function returns:

Text
15

You can follow rax through the loop: 0, then 4, then 9, then 15, and the dec/jnz pair stops after the third pass.

Where reading assembly stops paying

Be honest about the limits. Assembly tells you what the processor will do, not why the programmer wanted it, so it is a poor first tool for a logic bug that is visible in the source. It is also a poor guide to speed on its own: modern cores reorder, fuse and predict, so two instruction sequences with the same length can differ by a large factor, and only a measurement settles it. Write assembly by hand only when a language cannot express the operation, which is rarer every year. Reading it is the durable skill.

The rest of this series builds on this post: the registers, the instructions that move and combine data, the stack, and the calling conventions that let separately compiled functions cooperate.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.