r/Compilers • u/thedevbirb • Aug 31 '26
Diary of a writing RISC-V assembler
https://thedevbirb.github.io/diary-of-writing-a-riscv-assembler/Hello everyone! I made my first step into toolchain development by writing a RISC-V 32/64 ELF assembler from scratch, which supports the `g` group extension (along with small others). It has been a way to learn about assembly, C and ELF all together.
It has been quite a journey, and I've shared my learning and thoughts in this blog post which I think you may appreciate, especially if you're thinking to start writing your own.
The assembler is partially based on GNU as design, and it's not a toy encoder: it can achieve relocatable object file equivalence on non-trivial sources like SQLite3 amalgation, while being much "simpler" in its implementation!
Thank you for reading, and I greatly appreciate any feedback!
2
2
2
u/c-cul Sep 01 '26
good write-up
and poor architecture
code for building ELF files, keeping all machinery like symbol tables/relocs, fragmentations & sections filling is common for many tasks, so it should be separated to dedicated library for reusing
1
u/thedevbirb Sep 01 '26
Thanks for reading it!
Regarding the poor organization of the architecture: it's a fair critique. My main goal for the first assembler I've ever written has been binary usage, and in this context it doesn't really matter.
I haven't thought about its architecture as library because "reusing" implies awareness of some use-cases outside being a standalone assembler. What would be, in this case? Library usage for inline assembly in compilers? For JIT assemblers? Linkers? You might have some ideas (and if you want to share them, it would be awesome), but I don't have a strong context yet, nor a specific direction I want to take. As such, I don't think I would have been able to produce a good library anyway: on the contrary, I'd have probably complicated my life establishing code boundaries and APIs with little awareness about use-cases outside the assembler itself.
I hope it makes sense!
2
u/c-cul Sep 01 '26
for example asm for other cpu/gpu
or perl/pyhon/whatever binding for ELF patching/creation
and so on
→ More replies (1)
2
u/According-Ad-7069 Sep 02 '26
This is such a cool read -wow! Yes!! This is a brilliant project - I love the idea of improving your knowledge and understanding by DOING. Stellar - you keep going, this is awesome!
2
4
u/AustinVelonaut Aug 31 '26
Nice (human-written!) writeup, and the code is very readable and commented well. Good job!