r/Compilers • u/Dappster98 • Jan 22 '26
How much assembly should one be familiar with before diving into compilers?
Hi all,
To make this short, I was just wondering what your thoughts are on how much assembly, or rather how familiar one should be with assembly before writing a custom back-end code generator. I'm wanting to dive into "Writing a C Compiler" (the book by Nora Sandler) this year (I'm currently writing some bytecode virtual machines for langdev practice), and have been learning a bit of assembly on the side. I'm still fairly new to it, and find it difficult to think of how to problem solve in it (this may be expected for my level of experience). I've heard some say you can just pick up on it as you go, others say it's the easier part of writing a back-end, et cetera. So I'm just wanting insights from more of you.
Thanks in advance!
31
u/OkSadMathematician Jan 22 '26
Good news - you don't need to be an assembly wizard to write a backend. The thing most people don't realize is that modern compilers do the heavy lifting at the IR (intermediate representation) level, not in assembly. Constant folding, dead code elimination, loop unrolling, inlining - all that happens before you ever touch target assembly.
The backend is honestly more mechanical than creative. You're mostly doing instruction selection (pattern matching IR nodes to target instructions), register allocation, and instruction scheduling. You're not hand-optimizing assembly - you're translating already-optimized IR into target code.
For Nora Sandler's book specifically, you'll be fine picking it up as you go. You need to understand calling conventions (how args get passed, stack frames), basic addressing modes, and the general instruction categories (arithmetic, memory, control flow). That's really it to start.
My advice: learn enough x86-64 to read disassembly output and understand what a function prologue/epilogue looks like. Then just dive in. You'll internalize the rest through practice. The book will guide you through what you actually need. Don't let assembly anxiety block you from starting - compiler work is mostly about the middle-end transforms anyway.
8
u/srvhfvakc Jan 22 '26
When I was writing my JIT compiler I didn't really know any assembly (short of the very basics) and just kinda learned on-demand.
I think it's quite helpful to read the compiler output of functions on godbolt; can be fun to try to optimize based on it, and it helps you learn.
3
u/DerekB52 Jan 22 '26
Dive right into the book. You'll be fine. You can brush up on it if you hit a wall somewhere in the book, but I don't think it's a strong requirement.
1
u/whatdoyoumeanusernam Jan 22 '26
You'll learn assembly on the way as you need to. You're better off getting into gradually since it will make more sense. Bytecode is a better place to start since the concepts are the same just assembly is messier and needlessly complicated.
1
u/Ifeee001 Jan 22 '26
Imo, it's a learn as you go kinda thing.
I had a very small "compiler" that emits jvm bytecode. The way I learned jvm bytecode was watching a crash course on YouTube, and using a flag with the javac on the type of code I want to emit (e.g. if statement). The flag would show me what if statements look like In bytecode, and then I used that in code gen.
It got easier as time went on
I assume you should be able to do something similar with assembly/God bolt compiler.
1
u/dnpetrov Jan 23 '26
It's good to be able to read assembler. Reverse engineering tools like Hydra or Radare can help you with that, especially with code that has some complex control flow. Writing your own assembly programs is less important to be effective as a compiler developer (although if you also do performance analysis, you will write in assembly).
In many cases, though, you can be an effective compiler engineer working with just compiler IR as input and output. That comes with experience. You need to understand what would happen to that IR later on, and how it should be modified to produce the assembly output you need. Compiler IR is just yet another language, though. If you can learn how to work with the IR, most likely you can get yourself familiar with the assembler, too.
1
u/Ronin-s_Spirit Jan 23 '26
None. You can compile source to source or source to some fancy IR.
1
u/ImYoric Jan 23 '26
I've worked in compilers. I don't think I've ever written a single line of asm since the end of my studies. Rather, my compilers have produced either IR (typically LLVM IR) or even code (C or JS).
Also, there are plenty of layers in a compiler. Much of my compiler work has been in the front-end (parsing, static analysis, early transformation passes). None of this requires asm.
0
u/[deleted] Jan 23 '26
[deleted] — view deleted comment
1
u/[deleted] Jan 22 '26
[removed] — view removed comment