4 ms·
Hi HN, I wrote an assembler for the LC-3. This is part of my journey of learning how to write a compiler. The assembler is written in Python, and tries to sti
by cellularmitosis 6y ago
Hi HN, I wrote an assembler for the LC-3. This is part of my journey of learning how to write a compiler.
The assembler is written in Python, and tries to stick to as simple a subset of Python as possible (essentially, just functions and dictionaries with dot syntax sugar). All control flow leaf terminations are explicit (e.g. continue, break).
Idiomatic Python style is not a goal. Rather, the goal is to minimize the amount of syntax knowledge needed to follow the code. The code could be made much shorter with some clever refactoring, but brevity is a non-goal.
This trivial coding style will hopefully make hacking on the code more approachable for others just learning about lexers / parsers / assemblers / compilers.
The assembler is a simple two-pass implementation: create the symbol table (determine the addresses of all labels) in pass 1, then generate the machine code in pass 2. This is described in chapter 8 of the LC-3 book (Introduction to Computing Systems: From Bits and Gates to C and Beyond).
The LC-3 ISA consists of only 15 instructions, making this a simple task. One oddity is that the ISA is word-addressable, not byte-addressable.
The lexer is a simple regex-based implementation, and the parser is simple recursive descent. This approach is described in Gary Bernhardt's "A Compiler From Scratch" https://www.destroyallsoftware.com/screencasts/catalog/a-compiler-from-scratch https://www.destroyallsoftware.com/screencasts/catalog/a-com...
The lexer and parser output can be dumped (in JSON format) via command-line flags.
Additionally, the assembler also _accepts_ pre-parsed JSON input. This allows one to build a custom assembly syntax front-end and use this assembler as the back-end: all they have to do is generate the appropriate JSON output. Again, hackability is a goal! (How I wish other compilers allowed AST input!)
A good practice project would be a reimplementation in Golang, focusing on speed.
If you enjoyed this, you may also enjoy my trivial lisp interpreter in C series: https://gist.github.com/cellularmitosis/d8d4034c82b0ef817913a01138b115bf https://gist.github.com/cellularmitosis/d8d4034c82b0ef817913... or my lang-related list of links: https://gist.github.com/cellularmitosis/1f55f9679f064bcff02905acb44ca510 https://gist.github.com/cellularmitosis/1f55f9679f064bcff029...
Happy hacking!
- carapace 6y agoYou might enjoy http://pythonwise.blogspot.com/2012/06/python-based-assembler.html http://pythonwise.blogspot.com/2012/06/python-based-assemble... https://raw.githubusercontent.com/tebeka/pythonwise/master/assembler.pdf https://raw.githubusercontent.com/tebeka/pythonwise/master/a...
- bogomipz 6y agoYour first link unfortunately appears to be broken. If you click on the link "You can view the presentation I gave on this here." It shows an error message that the repository has been deleted.
- cellularmitosis 6y agoVery cool idea!!! Thanks for sharing this :)
- carapace 6y agoCheers
- bogomipz 6y agoThis is fantastic! Could you or anyone else share general resources that helped you in writing assemblers from scratch?
- cellularmitosis 6y agoBelieve it or not, once the lexing and parsing was out of the way, the two-page description of the two-pass process in the book was really all it took: make a list of all of the goto labels, then calculate the address of each. I’d be happy to turn this into a couple of tutorial blog posts. If you removed all of the lexing and parsing code, and chopped the ISA down to just 1 or 2 instructions, it would really highlight how simple the core problem really is! One technique I’ve used before (and a technique echoed by David Beazley in his compilers course), was to skip the lexer and parser and jump straight to working on something which is handed an already-parsed AST. Let’s you skip right to the interesting part, especially if you are already bored with writing parsers by hand.
- bogomipz 6y ago>"I’d be happy to turn this into a couple of tutorial blog posts. Yes that would be great. Please do! I think there would genuine interest in more posts. Please post it here if so. Cheers.