Compiling to Assembly from Scratch [free to read online]
"Compiling to Assembly from Scratch" by Vladimir Keleshev teaches compiler theory and assembly programming, focusing on ARM 32-bit instruction set, with resources available online and on GitHub.
Read original articleThe book "Compiling to Assembly from Scratch" by Vladimir Keleshev is designed for those interested in understanding how compilers and programming languages function. It guides readers through the process of creating a compiler from the ground up, utilizing a subset of TypeScript that resembles pseudocode and targets the ARM 32-bit instruction set. The content is divided into two main parts: the first covers the baseline compiler, including topics such as abstract syntax trees, parser combinators, and code generation, while the second part explores compiler extensions, including data types, garbage collection, and static type checking. The book is available in a hardcover edition of 207 pages and can also be read online for free. It includes illustrations by Katiuska Pino and provides source code on GitHub, along with Python and OCaml ports of the compiler. Keleshev, a software developer with experience in various fields, invites readers to reach out for questions or feedback.
- The book teaches compiler theory and assembly programming from scratch.
- It targets the ARM 32-bit instruction set using a TypeScript subset.
- The content is divided into baseline compiler concepts and extensions.
- Source code and additional resources are available on GitHub.
- The hardcover edition is available for purchase, while an online version can be read for free.
Related
'Writing a C Compiler' is a book
The "Writing a C Compiler" series transitions into a book by No Starch Press, focusing on building a C compiler with practical guidance, pseudocode examples, and essential compiler concepts for a broad audience.
Driving Compilers
The article outlines the author's journey learning C and C++, focusing on the compilation process often overlooked in programming literature. It introduces a series to clarify executable creation in a Linux environment.
Book – Writing a C Compiler: Build a Real Programming Language from Scratch 2024
Writing a C Compiler by Nora Sandler, releasing in July 2024, is a 792-page guide for building a C compiler, covering lexing, parsing, and code generation with a practical approach.
Writing a Lisp compiler (Lisp to assembly) from scratch in JavaScript (2018)
The article describes creating a Lisp compiler in JavaScript that converts Lisp expressions to assembly code, covering parsing, code generation, and assembly fundamentals, with source code available for exploration.
A Friendly Introduction to Assembly for High-Level Programmers
The article introduces assembly language to high-level programmers, focusing on x86-64 assembly with Intel syntax, covering instructions, registers, and file structure, while encouraging experimentation with code snippets.
Anyway, thank you, OP, for sharing this. I have been looking into picking up ARM as way to crawl out of burnout from my career. It's been a years since I even touched x86 with any seriousness. I will add this book to my list resources.
[1]: https://en.wikipedia.org/wiki/Scratch_(programming_language)
push {ip, lr}
ldr r0, =hello
bl printf
mov r0, #41
add r0, r0, #1 // Increment
pop {ip, lr}
bx lr
str r0, [r1] /* M[r1] = r0; */
ldr r0, [r1] /* r0 = M[r1]; */
str r0, [r1, #8] /* M[r1 + 8] = r0; */
ldr r0, [r1, #8] /* r0 = M[r1 + 8]; */
str r0, [r1, -r2] /* M[r1 - r2] = r0; */
ldr r0, [r1, -r2] /* r0 = M[r1 - r2]; */
with the destination on the left-hand side, with no "%" before the register names, and with the square brackets for addresses (with relatively sane looking expressions inside those brackets) — basically the same syntax that ARM's own assembler uses, — while x86 gets some absolutely unhinged syntax that looks like it just fell out of the sky (or an abyss for that matter) since it has almost no relation to what's written in the Intel's docs? I always assumed that GAS used some "unified" style for all its targets and disregarded the conventions of the CPU manufacturers but apparently no, it only did that for x86?Anyhow, so I’ve got a bunch of .bin files around for it, no C source code, just straight assembly output ready to be flashed. And the text and data segments are often interwoven, each fetch being resolved to data or instruction in real time by the rabbit processor. So I’ve been thinking of sitting down, going through the assembly/processor manual for the board and just writing a board simulator hoping to get it back to source code by blackbox reversing in a sense. I’d have to rummage through JEDEC for the standard used by the EEPROM to figure out what pins it’s using there and the edge triggering sequences. Once I can execute it and see what registers and GPIOs are written when, I can probably figure out the original code path. Not sure if anyone has tips or guides or suggestions here.
Though Why use 32bit instead of 64, why add so much friction for a first time learner.
In any case, I find it interesting to write a compiler and do want to continue with this book, so perhaps I'll just write the code on my MacBook and then transfer it to my Windows gaming PC to run it on WSL or something :)
Related
'Writing a C Compiler' is a book
The "Writing a C Compiler" series transitions into a book by No Starch Press, focusing on building a C compiler with practical guidance, pseudocode examples, and essential compiler concepts for a broad audience.
Driving Compilers
The article outlines the author's journey learning C and C++, focusing on the compilation process often overlooked in programming literature. It introduces a series to clarify executable creation in a Linux environment.
Book – Writing a C Compiler: Build a Real Programming Language from Scratch 2024
Writing a C Compiler by Nora Sandler, releasing in July 2024, is a 792-page guide for building a C compiler, covering lexing, parsing, and code generation with a practical approach.
Writing a Lisp compiler (Lisp to assembly) from scratch in JavaScript (2018)
The article describes creating a Lisp compiler in JavaScript that converts Lisp expressions to assembly code, covering parsing, code generation, and assembly fundamentals, with source code available for exploration.
A Friendly Introduction to Assembly for High-Level Programmers
The article introduces assembly language to high-level programmers, focusing on x86-64 assembly with Intel syntax, covering instructions, registers, and file structure, while encouraging experimentation with code snippets.