Why Assembly?
Assembly Language · Assembly & OS track
6 min read
In this module
The Bridge Between Worlds
Assembly language sits exactly between the code you write and the binary the CPU executes. It's a human-readable rendering of machine code, where each line corresponds to one (or a small handful of) machine instructions. Python and JavaScript hide the hardware. Assembly hands you the keys.
Why It Still Matters
A fair question: why would anyone write assembly when compilers happily produce it on demand? Because there's still a stubborn list of places where it remains the only realistic option:
- Bootloaders. The first instructions executed at power-on. There's no high-level runtime yet, no library calls, not even a stack until the code sets one up. Hand-written assembly or nothing.
- Device drivers. Some hardware operations need specific CPU instructions that C simply has no way to spell.
- Performance-critical code. Game engine inner loops, cryptography primitives, DSP kernels: domains where a person who really knows the pipeline can still beat the compiler.
- OS kernel internals. Context switches, interrupt entry stubs, and similar low-level surgery demand direct control of registers and the stack.
Assembly is a human-readable form of machine code, with each instruction corresponding directly to a CPU operation. It earns its keep in bootloaders, drivers, OS kernels, and performance-critical code that demands direct hardware control.
What Assembly Looks Like
Here is a taste of assembly (x86-64, Intel syntax) that adds two numbers:
mov rax, 5 ; load 5 into register raxmov rbx, 3 ; load 3 into register rbxadd rax, rbx ; add rbx to rax, result (8) is in raxEach line is one CPU instruction. No variables. No functions in any high-level sense. No if. Everything you do is a direct operation on a register or a memory address, and that's the entire vocabulary.
Worked Example: One C Function, Two Compilations
Here's about the smallest useful C function there is. Compile it with gcc -O0 (no optimisation) for x86-64 Linux and you get something close to the listing on the right. Hover over a line on either side to see which instructions came from which line of C: the signature line alone accounts for five of them, setting up the frame and parking both arguments on the stack.
add() at gcc -O0
x86-64Hover over code lines or mapping badges to see correspondences
A few things jump out once you know where to look. The arguments arrive in registers, a in edi and b in esi, because that's what the Linux calling convention says. The answer leaves in eax, because that's where the caller expects a return value. And at -O0 the compiler copies everything onto the stack and straight back off again, which looks silly but makes each variable easy for a debugger to find.
Now turn optimisation on with -O2:
add: lea eax, [rdi+rsi] retTwo instructions. lea ("load effective address") was designed to calculate memory addresses, but it's really just a fast adder that doesn't touch the flags, so compilers use it for plain arithmetic all the time. Spotting tricks like this is half of reading compiled code.
Predict: what does gcc -O2 make of int square(int x) { return x * x; }?
Something like this:
square: mov eax, edi imul eax, edi retx arrives in edi, gets copied into eax, and imul multiplies it by x again, leaving the result where the caller expects it. Clang 21 emits the same pair, but other compilers and versions are free to pick registers or order the work differently (imul edi, edi then mov eax, edi does the same job), so don't expect identical output everywhere. Same calling convention, same three-part shape: arguments in, work, result out.
Reading It Is the Skill You Need
You'll write very little assembly in this course. You'll read a lot of it. When there's no source code (a crash dump from production, a suspicious binary, a closed-source library with a bug), the disassembly is the only honest account of what the program does. Reverse engineers, exploit developers and anyone debugging an optimised build all spend their days reading the output above, not writing it.
So practise. Each listing below is gcc -O2 output for a tiny function (your gcc version may differ slightly). The arguments are ints arriving in edi and esi, and the result leaves in eax. Work out the C before you open the answer.
f1: cmp edi, esi mov eax, esi cmovge eax, edi retWhat does f1 compute?
The larger of its two arguments: int max(int a, int b) { return a > b ? a : b; }. cmp compares a with b, eax starts out as b, and cmovge ("move if greater or equal") overwrites it with a when a >= b. No jump anywhere, so there's nothing for the branch predictor to get wrong.
f2: mov eax, edi neg eax cmovs eax, edi retWhat does f2 compute?
The absolute value: int f2(int x) { return x < 0 ? -x : x; }. eax becomes -x. If that came out negative (the sign flag is set), x must have been positive, so cmovs puts the original x back.
f3: mov eax, edi not eax and eax, 1 retWhat does f3 compute?
Whether x is even: int is_even(int x) { return x % 2 == 0; }. An even number has a 0 in its lowest bit. not flips every bit, so that 0 becomes a 1, and and eax, 1 throws away everything else. The result is 1 for even and 0 for odd, with no division in sight.
Understanding, Not Memorising
You don't have to become a hardened assembly programmer to get value from this. Even a passing fluency in assembly transforms how you read your high-level code. Once you can see that a function call quietly pushes a return address onto the stack, or that an innocent-looking loop turns into a conditional jump, your intuition for what the machine is really doing improves overnight. Assembly is where abstraction stops apologising and the silicon takes over.