Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to go about writing a simple x86 assembler?

I'm interested in writing an x86 assembler for a hobby project.

At first it seemed fairly straight forward to me but the more I read into it, the more unanswered questions I find myself having. I'm not totally inexperienced: I've used MIPs assembly a fair amount and I've written a toy compiler for a subset of C in school.

My goal is to write a simple, but functional x86 assembler. I'm not looking to make a commercially viable assembler, but simply a hobby project to strengthen my knowledge in certain areas. So I don't mind if I don't implement every available feature and operation.

I have many questions such as: Should I use a one-pass or two-pass method? Should I use ad-hoc parsing or define formal grammars and use a parser-generator for my instructions? At what stage, and how do I resolve the addresses of my symbols?

Given my requirements, can anyone suggest some general guidelines for the methods I should be using in my pet-project assembler?

like image 824
mmcdole Avatar asked Oct 30 '09 19:10

mmcdole


People also ask

How hard is it to write an assembler?

It's just tedious to write Assembler using Assembly Language or even Machine Code. The purpose of this task is to understand how to translate Assembly Language to Machine Code, it's fine to use higher level language such as C, python, java etc as long as you understand the theory behind it.

How can I learn x86 in assembly language?

Learn what a register is, then learn some basic instructions. There's about 20 to 30 instructions in the original x86 instruction set. Those original instructions are for really basic things like ADD, SUB, jumps, calling functions, etc. Learn those first because they're still the backbone of modern x86/x64.

How is assembler written?

There's nothing special about how an assembler is written. All it does is parse an assembly syntax and spit out machine code for a particular architecture. If your preferred programming language can read text and write binary, you can create an assembler with it.

Is x86 assembly a programming language?

It is used to produce object code for the x86 class of processors. Regarded as a programming language, assembly is machine-specific and low-level. Like all assembly languages, x86 assembly uses mnemonics to represent fundamental CPU instructions, or machine code.


1 Answers

There is an excellent free e-book (pdf) on how to build assemblers and loaders by David Salomon. You can find it at:

http://www.e-booksdirectory.com/details.php?ebook=1311

like image 89
Paul Avatar answered Oct 24 '22 07:10

Paul