Help..How to Write an Assembler for a 8 bit RISC Microcontroller

Status
Not open for further replies.

blooz

Advanced Member level 2
Joined
Dec 29, 2010
Messages
560
Helped
121
Reputation
242
Reaction score
116
Trophy points
1,343
Location
India
Visit site
Activity points
4,985
Hi How to code a An Assembler in C targeting a 8 bit RISC Machine ..
Currently I am Hand Assembling the code to hex ...Is there any good material on Designing a simple Assembler ...
 

Surely there is an assembler made by the chip manufacturer you can use.

The basics of an assembler though are:
First pass -
Analyze each line of source code and put together the bits/bytes applicable to the instruction. If an address (as in a jump/branch/call instruction) is found, insert a dummy destination value for now, this will ensure sufficient space is there when the second pass puts the real value in its place. Usually, a look-up table is used to reference the assembler instruction to its hex code and the same table will say how long (how many bytes) the instruction is.
When a label is found, note it's address in the symbol table. Note that the value of labels can't be used in the first pass if they are further along the assembler file, they won't have been found yet.

Second pass -
Go through the code again, this time when a label is encountered, look it up in the symbol table and put its real value/address in the hex file.

More passes -
Two passes is usually enough, if you want to, you can now optimize the code. Look for things like jump instructions with full addresses that only go back or forward a few instructions, usually you can substitute a shorter branch instruction instead. It really depends on the instruction set of the processor as to how applicable additional passes are.

Brian.
 
Reactions: blooz

    blooz

    Points: 2
    Helpful Answer Positive Rating
That's a good summary, Brian.
I'm sure you would have intended to include variables (of all types) in there.

You'd treat them in a similar manner to code locations (jumps etc), by adding each instance to an array of variable definitions during the first pass, and then resolving them to actual addresses in the second pass (unless you force the writer to define all variables ahead of all code, which has been preferred, historically, simply to make the assembler simpler!).

But while I'm on, I'll add some further thoughts:
A more advanced assember would provide for including library functions, linking to other modules and providing 'hooks' to allow the code to be linked-to by other modules.
I'd also recommend making provision for debugging. At a minimum, that would just be a report of locations allocated to variables and labels in alphabetical sequence, and in more advanced debuggers, it would even insert code for breakpoints and provide access to variables through a user interface.
Then there's the option to include 'compiler directives' which can pre-define default sizes for variables, debug options, force global addresses, force standards for label and variable names, force array space in the absence of dimensions and inhibit certain instructions (eg interrupt mask control).

It might sound like a lot of extra work, but the time taken to write these additional features can be easily recouped by a faster (and less stressfull ! ) development time.

There are plenty of texts and resources out there to assist the assembler-author, but I must add my personal view that writing and using one's own assembler is a very excellent way to improve one's knowledge and skill in programming a particular device (though it might make you unpopular with your colleagues and future programmers who have to maintain your code!)

There seems to be a temptation for any programmer who writes their own assembler to 'invent' new instructions or new procedures. This can be done for private use, though I think should be resisted for wider circulation (just think about the chaos arising from someone who wnays to create a 'test and set' instruction by turning off and on the interrupt without checking its status beforehand).
 
Last edited:
Reactions: blooz

    blooz

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…