News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to implement small assembler/emulator

Started by tetsu-jp, April 01, 2009, 09:58:25 PM

Previous topic - Next topic

tetsu-jp

I'd like to get more data on this, right now, I think i know how to implement assembler.

I've seen a few sources, one time maybe ten years ago, interpreted BASIC!
the structure was not so good (spaghetti code).

that's NOT how i want to write it.

also i have WINUAE source, 6mbyte C++ (VS), which is basically an emulator for 68000.

it is done in a certain way: there are about 1000 C functions, one for each possible op-code!

that's also NOT the way how i want to write it.

I want compact as possible, modular, and clean to read.
speed is not so important!

and there is no support to comfort the programmer, there will be one single format allowed to spell the source.

no options or memory models either.

any ideas/data? for instance names for the required modules, and a single line of text what they are supposed to do.

after some years, i have derived the concept to encode allowed variants of instructions, for instance, type of addressing, type of registers. this will not be formulated via program code!

and there can be twenty passes, does not matter either. no artistic programming required, to do everything in one pass.

I made one Windows form quite a year ago, which reads opcode and "register rights" from files, but then abandoned this
prototype because of a few uncertainties.
Now i have started with a new one, can set all the allowed variants via Windows GUI! practically, it is using a listbox provided by Windows.

You maybe know it is not an easy task, and I have priority on clean design. not coding tricks.
for instance how to encode the call's and jump's. I have an idea how to do it, but it is not coded right now.

brethren

hi tetsu-jp:)

I once found a pdf on the techniques for implementing an emulator (which i could upload), i can also give you a link for a free book on techniques for coding an assembler.

Interested?


tetsu-jp

yes, i have collected a lot of PDFs from bitsaver, tube manuals, glow lamp book, about 600 to 700 IC datasheets.

so your data is more than welcome.

xmetal

If you are serious about engineering your assembler in a "proper" way, then use standard tools like Lex & Yacc for scanning & parsing the input. You can then concentrate on the real issues.

brethren

Here's that link to the book about the techniques for implementing an assembler

http://www.davidsalomon.name/assem.advertis/AssemAd.html


tetsu-jp

wow. one short look tells me this will be an impression for my work (i want to gather more impressions before start coding)

yes, I've read about Yacc (UNIX).

however, nowadays CPUs are more powerful, and can employ different techniques.

the 68000 operands are not so complicated, so the idea is to build a small state machine.
no parsing! i have scribbled numerous folders, and have read the 68K manuals many times,
and exercised all opcodes (on paper).

indeed the number of possible branchings is small!

and i want to keep the source code reasonable small!

my state machine can parse all the operands using a few 100 lines!

what's really difficult is the computation of the jump, and the addressing.

but that's also solved to some degree, the assembler can classify all opcodes via table, so
it can know which addressing variants are allowed. simply, it will verify against these types,
and no other processing is neecessary!

that's pretty much about it.

but now i will read this book. thanks!

x86 assemblers are different- the address generation is more complex, the SIB for instance, and the modifying prefixes.

it might be, in the end my logic will also put labels in a list, sort them, examine the generated code, and then insert numeric data.

but is there a need for 150,000 lines code, a dissertation, and a university degree?

indeed, the scanning and parsing of the source code is THE relevant task to build an assembler (or compiler).

it might also be, without knowing it, i will maybe build some sort of specialized Yacc type code...
must i feel guilty because it is not exactly the same, and it is not referencing to UNIX?

i know the history of C language...a little folklore about the project.

so let say, the method of parsing is THE issue...

i want it small, nice, and maintainable, do not care about optimal code, or programmer comfort, not at all.

xmetal

If you have never implemented a scanner/parser before, then I guess it is alright; its fun and you do get to learn while reinventing the wheel.

But there really is no fun or purpose in doing it the second time. Scanning & parsing are the most routine parts of a compiler/assembler/interpreter. And your code really does become smaller and more maintainable if you use the tools.

tetsu-jp

hmm yes.
how can they be used (on Windows)?
they are UNIX tools, right?

i do not agree to GPL, so can't use these "free" tools (even i have LINUX distros around)

if there is anything else, please give a link.


TmX

If you don't want to use Bison (GNU version of yacc), you could try byacc (Berkeley YACC). It's public domain, written in ANSI C. Building it on Windows is pretty easy.

tetsu-jp


dsouza123

If you want to see how others have implemented x86 assemblers,
there are some available with source (some in assembly some in C etc),
FASM at flatassembler.net which is self assembling and has been used to write OSes, available for multiple OSes.
JWASM at japheth.de/JWasm.html in C, very MASM compatible
HLA at webster.cs.ucr.edu some versions assemble to a secondary assembler, available for multiple OSes.

tetsu-jp

yes, i had NASM, also i think FASM, a while ago. FASM was for DOS, right?
i have one source, 960kbyte 68000, for a complete assembler IDE.

thanks, i am getting a lot of related materials, which will be supportive for my work!
including the obscure Yacc  :bg

zcoder

I wrote one for the Z80 about a month ago, it runs ALL Z80 code.
I am still working on it to make it into a trainer of some sort, so people
need not really build a Z80 computer to learn Z80 programming, I am
then going to write a Windows Z80 assembler.

Zcoder.
Back in 1979, My computer ran so fine.
And there was no such thing,
As a Microsoft Crashed Machine.
http://zcoder.110mb.com
http://www.dietzel.com/partner/idevaffiliate.php?id=345_6  Free Domain Names