Re: Writing my first compiler

Dobes Vandermeer <>
21 Mar 2002 21:59:25 -0500

          From comp.compilers

Related articles
Writing my first compiler (2002-03-17)
Re: Writing my first compiler (Joachim Durchholz) (2002-03-19)
Re: Writing my first compiler (john thrum) (2002-03-19)
Re: Writing my first compiler (Dobes Vandermeer) (2002-03-21)
| List of all articles for this month |

From: Dobes Vandermeer <>
Newsgroups: comp.compilers
Date: 21 Mar 2002 21:59:25 -0500
Organization: Compilers Central
References: 02-03-072
Keywords: code
Posted-Date: 21 Mar 2002 21:59:25 EST

Yarrago wrote:

> front end however the backend is causing me a bit of concern. I want
> my compiler to output 16bit dos exe's. I have several books on the
> Would
> it be best if I write an assembler or just integrate this into the
> back end?

Besides macros & labels, the main difference between assembly language and
machine code is the encoding. For example:
printf("ADD AX,5");
or in machine code:

If you already know the instruction you want to emit, it is very easy
to figure out the machine-code equivalent byte-codes (given the intel
instruction set reference, available from intel's web site somewhere).

Assemblers also provide some minor instruction selection services by
choosing the "best" matching instruction with the given name, which
you would have to emulate yourself, probably by making macros for each
op & addressing mode combo you want to use, like: #define
emit_ADD_reg_imm(reg,imm) (if(imm <= 127 && imm >= -128 && reg ==
reg_EAX) printf("\04%c", imm);)

Or somesuch. Its likely you could come up with a better idea than
this, I'm just trying to illustrate that its actually fairly simple.

> help me with this final step in either writing an assembler or adding
> the execute output to the backend of my compiler? Is an assembler
> basically a matter of converting the mnemonics into opcodes and
> linking the file with a header to make it into an execute or is there
> more to it?

If you are using direct interrupts to communicate with the BIOS, and
not linking with any libraries, then that is all you need. If your
curious about the contents of that header you can, of course, compile
a simple (or empty) program with an existing compiler and disassemble
it to see what they put there.

Post a followup to this message

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