How do I write an assembler?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I write an assembler?

LBSCR
I already know how to program in Python but I do not know how to write an assembler, please help is appreciated :D!
Reply | Threaded
Open this post in threaded view
|

Re: How do I write an assembler?

ybakos
Did you read Chapter 6? Did you complete chapter 5?
Reply | Threaded
Open this post in threaded view
|

Re: How do I write an assembler?

svajoklis
In reply to this post by LBSCR
Chapter 6 doesn't say very much on the exact implementation details, that's why it might be a bit hard to wrap your head around it.

I have found basically two ways of doing it:

Either you go the naive way and use something like regular expressions and, after splitting your whole program into separate lines, use them to make sense of the code. That would include detecting whether the line is an a command, a label or a c command. Then, using regular expressions again you could break each command down into its parts and interpret from there.

Another, more complicated way is the "proper" method, where you first tokenize your source code, discarding comments, and turning @a D=1 to (operator, AT), (identifyer, "A"), (NEWLINE), (identifyer, D), (operator, EQUALS), (integer, 1). Then, having the token stream, you use a parser to again detect and organize the tokens into a syntax tree. Then, from that syntax tree you would be able to generate code.

I took the second route, it's much harder, but I'm learning a lot of stuff on the way. Looking at it I don't see many reasons not to go the first way if you just want to make something that works for this project.