Re: Writing Assembler!

"JUKKA" <>
22 May 1997 22:16:58 -0400

          From comp.compilers

Related articles
Writing Assembler! (Khoo Kiak Wei) (1997-05-13)
Re: Writing Assembler! (1997-05-14)
Re: Writing Assembler! (Clark L. Coleman) (1997-05-16)
Re: Writing Assembler! (1997-05-17)
Re: Writing Assembler! (Ben Bullock) (1997-05-17)
Re: Writing Assembler! (1997-05-17)
Re: Writing Assembler! (Mr J R Hall) (1997-05-22)
Re: Writing Assembler! (JUKKA) (1997-05-22)
Re: Writing Assembler! (Charles Fiterman) (1997-05-22)
Re: Writing Assembler! (1997-05-25)
Re: Writing Assembler! (1997-05-25)
Re: Writing Assembler! (JUKKA) (1997-06-09)
Re: Writing Assembler! (1997-06-09)
Re: Writing Assembler! (1997-06-09)
[9 later articles]
| List of all articles for this month |

From: "JUKKA" <>
Newsgroups: comp.compilers
Date: 22 May 1997 22:16:58 -0400
Organization: GOOD
References: 97-05-156
Keywords: assembler

> I am planning to write a generic assembler, as a work of learning
> flex and bison. However, after reading a book on Lex and Yacc, I am
> still confused on how should I start!?

> If any of you know of any sample code on assembler or good books talking
> on the assembler construction, I would like to hear from you please!
> Thanks in advance.


You can use lot of from compiler books when making the assembler
scanner and parser. In assemblers you should treat also end of lines
and spaces as tokens and not just skip them.

I have never used Lex and Yacc for any assembler. I have always written
my parser and lexer manually. It is no too complicated.

Check pub\microprocs\etc.

There are many assemblers with sources. In Fred Fish Amiga library there
is 68k assembler C source code. Same ftp under FISH.

Check also MICROCONTROLLER FAQ texts .. use Internet search.


The assembler usually have at least 2 passes.

In first pass all "labels" are gathered. And all machine code
statements are solved to get the position counter to increment
correctly. At first pass we do not yet generate any code since there
can be unknown labels (forward ref).

At the pass 2 we use the label values and finally generate the code
with operands. Only externals are not solved. They are collected and
emitted in the obj file for the linker to solve at the final stage.

I usually use the same scanner for both passes and have pass specific
sections. I simply scan the file twice to avoid too complex data


I have used table driving with good results to solve if the assembly
statment is supported by the CPU. First you parse the user source to
get somthing like:

[label] mnemonic [[op1],op2]

Then you evaluated the adressing mode used by the user for both

Then you scan your table to see which modes are supported by the CPU
for this mnemonic. Sometimes you could solve the statment with several
CPU codes. Here the assembler can do some optimizer stuff. Use the
shortest possible coding.

You have to create your own object format or use some common format in
your system. You generate usually free relocatable object files. This
means that the linker can finally put them where it will. This is
because in this way we can divide the whole project to several files
which will be linked together to form a SINGLE LINEAR PACKED code and
data sections. Usually code in EPROM and data in RAM. At link time
you say where the data and code bases are in your target CPU
system. There are also stack and heap sections. Also absolute section
to allow the user define absolute adresses in his code (peripherial
addresses etc.).

After you have assembler running you have to make the linker which
reads assembler generated object files to make the binary file
(Motorola HEX or Intel HEX records). Which can be downloaded or

The assembler can also generate directly downloadable code .. but you
lose the modularity and have to all the time self take care where the
code is generated. In higher quality assemblers you just say in which
section to put the code and the linker does the hard work!

Send more questions if you need more info. I have written several
assemblers, linkers and compilers. Maybe should write a book about
this area. I have also never seen a good source of assemblers and
linkers. Do sombody know?

[Well, I am working on a linkers book right now. Stay tuned for
more info. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.