Channels ▼

Al Williams

Dr. Dobb's Bloggers

Universal Cross Assembler Redux

November 05, 2010

One disadvantage of rolling your own CPUs is that you have to make all your own tools. True, some people emulate existing instruction set architectures and then leech off the tools for the processor they are emulating. But what fun is that?

As part of my "One Instruction Wonder" articles here in DDJ, I published an article about my Universal Cross Assembler. It is a hack to be sure and it certainly works best under Linux or at least something like Cygwin since it depends on a hodgepodge of Unix-likek tools.

The principle is simple. An awk script transforms an assembler input file so that it looks like a bunch of C macro invocations. Then the system C compiler generates a program by combining these macro calls with some library code. The resulting program emits the object code. A shell script orchestrates the whole process. Once you have everything working, defining a new assembler language takes just a few minutes.

Case in point, I recently built an Altair 8800 replica (I'll talk about that more in the future) and I found I wanted an 8080 assembler. Of course, one answer was to fire up SIMH and emulate a CP/M system and use that. Authentic, but annoying. And yeah, there are plenty of cross assemblers out there like TASM that have 8080 definitions. But I decided it would be easy to retarget my assembler. The actual retarget took about 30 minutes -- a few more when I discovered some typos. However, I also wanted to add an 8 bit raw binary dump to the code. That required a short patch to the shell script driver, and the soloasm.c code. That was another 15 or 20 minutes, but really doesn't count against the total time since it was a general enhancement and not absolutely necessary.

The Altair switches are amenable to octal so I decided that dumping binary and using the od program to convert to octal would be nicer than trying to add a little used octal output mode to the assembler. So the invocation looks like:

axasm -p i8080 -8 test.asm | od -b > test.octal.txt

And a part of the definition file:

#define NOP DB(0)
#define MOV(d,s) DB(0x40+(((d)&7)<<3)+((s)&7))
#define MVI(d,i) DB(6+(((d)&7)<<3)); DB(i)

Granted, an 8080 assembler probably isn't useful to most people, but I thought it was a good example of how flexible the assembler is. I currently have it targeting the 8080, the PIC16F84, the RCA1802, and two custom CPUs of my own. I don't think any of the retargets took more than an hour. In fact, I'm not sure any of them took anywhere near that.

If you want the 8080 updates (including the 8 bit binary output mode patches) you'll need the code from the original article plus this file.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.