flat assembler
Message board for the users of flat assembler.

Index > Main > Designing an assembler - need help on instruction encoding

Author
Thread Post new topic Reply to topic
vivek



Joined: 21 Aug 2010
Posts: 2
vivek 21 Aug 2010, 13:08
Hello,

This may not be directly related to FASM but here it goes .

I am designing a simple 8086 assembler for a college project and to learn more about the intricacies of computers ,compilers et al . I have gone through the "Dragon Book" and have already developed most of the lexical scanner and parser using lex and yacc but I have now hit a roadblock . The process of generating code , ie , translating instructions to 8086 object code seems to be quite complicated and I can't find any good material on this . All I could find were some complex tables . I have no idea on how to interpret what's in those tables . Someone please guide me on this and suggest some good material .

For the time being I simply want to generate 8086 object code for a .COM file (just 8086,not 80x86).

Pleas help me out.Thanks

Vivek,
Second year EE student
Post 21 Aug 2010, 13:08
View user's profile Send private message Send e-mail Reply with quote
b1528932



Joined: 21 May 2010
Posts: 287
b1528932 21 Aug 2010, 14:36
First of all you need perfect knowleadge of ALL released cpus you want to support.

Instruction can be encoded diffrently (or not at all) on diffrent cpus.
Also, you need full cpu state, for example if cpu is in real mode, protected mode, long mode, or any other special mode that change instruction encoding.

Remember, that assembler is just a language - in reality you have opcodes and you just give them names.

I suggest you write emulator, in my opinion its bilion times easier.
I dont think that any human is capable of writing what you suggest. it is possible, but huge, cpu specyfic, and kind of useless.
Post 21 Aug 2010, 14:36
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 21 Aug 2010, 15:06
This kind of questions is very welcome here, and it really is somehow related to fasm - I was once at the similar position when started writing my first assembler (the one before fasm); and at the time I had no access to internet to look for help, so all I had were those complex tables and lots of spare time...

The original 8086 code was based on octal numbers, so it will help if you try too look at instruction codes expressed in octal base, as you will then easily notice their structure.

Look at the example: AND CL,DL

Its opcode expressed in octal base is: 40o, 321o

The digit 4 in the first byte means the AND operation (0 would be ADD, 1 - OR, 2 - SBB, etc. up to 7; you can look it up in the tables).
The digit 0 in the first byte means the byte-size operation with register as source (1 would mean word-size operation with register as source, 2 would mean byte-size operation with register as destination, etc.)

The digit 3 at the top of the second byte means that both operands are registers (other values would mean various types of memory addressing for the memory operand).
The digit 2 in the middle is the one that corresponds to register operand (2 in case of byte-size registers means DL, 0 would be AL, 1 - CL, 3 - BL, etc. - again, you will find a table for this).
The digit 1 at the end is the one that corresponds to operand that can be register or memory (this field is usually caled "r/m") - in this case we have selected that both operands are registers, so r/m field is interpreted as byte-size register, namely CL.

I encourage you to get some disassembler (for example the one by CandyMan; or some tool like BIEW) and experiment a little - try to write different codes and see the effects (you can use fasm for this; just put "DB 40o, 321o" in a source file and assemble it to get the example result).
Post 21 Aug 2010, 15:06
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 21 Aug 2010, 15:18
[offtopic]It really was the perfect question for me to answer now, when I'm enjoying a tea break in a middle of implementing AVX into fasm. Wink[/offtopic]
Post 21 Aug 2010, 15:18
View user's profile Send private message Visit poster's website Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 21 Aug 2010, 19:48
[oops]
what is an AVX
which coffee do you drink, i drink tea, green tee and kvass
[/oops]
Post 21 Aug 2010, 19:48
View user's profile Send private message Reply with quote
vivek



Joined: 21 Aug 2010
Posts: 2
vivek 22 Aug 2010, 14:05
Thanks , Tomasz .That helped ,btw, is there any other material / book you suggest besides the intel manuals
Post 22 Aug 2010, 14:05
View user's profile Send private message Send e-mail Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Aug 2010, 15:34
Post 22 Aug 2010, 15:34
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 22 Aug 2010, 20:15
edemko wrote:
[oops]
what is an AVX
[/oops]


i guess this: http://en.wikipedia.org/wiki/Advanced_Vector_Extensions

tomasz how will you test, as wikipedia states there aren't any CPUs that support this until 2011?
Post 22 Aug 2010, 20:15
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 22 Aug 2010, 22:05
coconut wrote:

tomasz how will you test, as wikipedia states there aren't any CPUs that support this until 2011?

Neither did I have access to any 64-bit hardware when I was implementing x86-64 (and even writing first example programs for it). And AVX is now even better documented than x86-64 was back then. Smile
Post 22 Aug 2010, 22:05
View user's profile Send private message Visit poster's website Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: US
coconut 23 Aug 2010, 01:41
Tomasz Grysztar wrote:
coconut wrote:

tomasz how will you test, as wikipedia states there aren't any CPUs that support this until 2011?

Neither did I have access to any 64-bit hardware when I was implementing x86-64 (and even writing first example programs for it). And AVX is now even better documented than x86-64 was back then. Smile


Shocked
Post 23 Aug 2010, 01:41
View user's profile Send private message Reply with quote
edemko



Joined: 18 Jul 2009
Posts: 549
edemko 23 Aug 2010, 10:10
Will they ever improve a quick copying instruction, is there such?
We use rep movsd, i'd like seeing rep movsQQQ, hoho
Post 23 Aug 2010, 10:10
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 24 Aug 2010, 03:54
REP MOV* is the quick copy - newer processors have special handling for (E|R)CX > N.

The best way to learn assembly is part-time with the computer and part-time away from the computer. One must test their understanding in this way, and in time the depth of code composed in the mind increases, imho. Tomasz is an excellent example of this - truly inspiring. There are many talented people here.
Post 24 Aug 2010, 03:54
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 24 Aug 2010, 09:15
[offtopic]
I think AVX even had a simulator/emulator thingy
...
here it is: http://software.intel.com/en-us/avx/
No need to have a CPU Smile

Tomasz, can you comment on implementing AVX being easy so-to-speak.
As far as I've read and understand it is really thought through and there are little to no exceptions
[/offtopic]
Post 24 Aug 2010, 09:15
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 24 Aug 2010, 12:15
Madis731 wrote:
Tomasz, can you comment on implementing AVX being easy so-to-speak.
As far as I've read and understand it is really thought through and there are little to no exceptions
As I already predicted when first analysing the AVX specifications, it was relatively easy. There is a dozen of exceptions, but they are never complex.
Post 24 Aug 2010, 12:15
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 01 Sep 2010, 02:02
Post 01 Sep 2010, 02:02
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.