Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.misty.com!news.iecc.com!.POSTED!nerds-end From: glen herrmannsfeldt Newsgroups: comp.compilers Subject: Re: Best tools for writing an assembler? Date: Mon, 24 Feb 2014 20:42:56 +0000 (UTC) Organization: Aioe.org NNTP Server Lines: 34 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <14-02-033@comp.compilers> References: <14-02-018@comp.compilers> <14-02-025@comp.compilers> <14-02-027@comp.compilers> NNTP-Posting-Host: news.iecc.com X-Trace: miucha.iecc.com 1393277874 86029 64.57.183.58 (24 Feb 2014 21:37:54 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Mon, 24 Feb 2014 21:37:54 +0000 (UTC) Keywords: assembler, tools Posted-Date: 24 Feb 2014 16:37:54 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1112 Hans-Peter Diettrich wrote: (snip) > Depending on your instruction set and addressing modes, it may be > simpler and more instructive to write your own assembler. During this > discussion I remembered that I wrote my first assemblers (and > disassemblers) in Forth, (snip) > Unfortunately Forth writes the generated code directly into RAM, which The traditional assembler makes (at least) two passes through the program, the first to assign addresses to all the labels, and later to generate the code using those addresses. But that was when memories were smaller. If you just generate into memory, you can easily fixup the addresses once they are defined. Then, at the end, write the whole thing out to a file. Both those cases work will if all instructions (and constants) have a known length, even when the actual addresses aren't yet known. There are some interesting cases where the instruction choice (and length) depends on the size of an address, which (in the case of a forward reference) depends on the instruction length. If you can avoid that, it is pretty easy. -- glen [Variable length instructions aren't that hard. You start by assuming everything will be the long form, then iterate over the code a few times shrinking what you can. That isn't always perfectly optimal (which is known to be NP complete), but it's pretty close. -John]