i need tips for designing a one-pass and two pass assembler using c
i need tips for designing a one-pass and two pass assembler using c
It kinda depends on what you're assembling, but here's the general strategy...
Pass A: Getting the labels
1: Start at first address of assembly program
2: Read in a line
3: Decompose that line into its seperate parts
4: If there's a label, store that <label, address> pair
5: Figure out how much memory the instruction there will take
6: Increment the address counter
7: Repeat untill you reach the end of file.
Now you have a map from labels to addresses, so you can actually assemble the instructions.
Pass B: Assembly
1: Start at first address of assembly program
2: Read in a line
3: Decompose that line into its seperate parts
4: Put together the instruction (you'll use the <label, address> map here
5: Write the instruction to an object file
6: Repeat untill you reach the end of file
Decomposing the instruction is just an exercise in character-at-a-time string processing. How you do it depends on your instruction format.
You'll need to keep some information about the instructions and directives. This is done with a <mneumonic, information> map, where the mneumonic part is the string representation of the mneumonic, and the information part is a structure that contains information about that mneumonic (like, is it an instruction or a directive? How big is it? What is its bytecode? What kind of arguments does it accept?)
Callou collei we'll code the way
Of prime numbers and pings!