Summory about size of addressing part of opcode.

Usually to calculate full size of opcode
(that uses memory and reg operands) you need
to know 2 things: size of code block and
size of address part.
Size of code for "old" general instructions
(such as mov, push\pop,inc\dec, add,sub,and,xor, etc.)
is 1 byte. Or 1 byte and extention in modr/m byte
wich we can ignore, 'cause we count it calculating
address part.
Size of "new" instructions usually preceeded with byte
0F and there for takes two bytes.

First thing to say about address part is:
size of addr part with one operand,
and 2 operands where one is and other reg
are the same.
for example:
push [12]
mov ecx,[12]
are opcode of the same size and their address parts are
also of the same size.

Before structurizing address formats and their size let's
refresh in memory that there are 5 instruction that
can use the single full reg operand included with code
block in one single byte with format 5:3 (5 bits for code and 3 for reg)
They are: inc\dec,push\pop, xchg.
We remember them as exeption and also say that
if those instructions uses some other operands than full regs,
their size can be calculated with general rules cod+address part.

Now let's enumurate all addr formats and their size along with
encoding format.


operands: size of addr. format
reg 1 byte modr/m=11 cod reg
------------------------------------------------------
[reg]
where reg !=
esp or ebp 1 byte modr/m=00 cod reg
------------------------------------------------------
[esp] 2 bytes modr/m=00 cod 100
+ sib = 00 100 100
------------------------------------------------------
[ebp] 2 bytes modr/m=01 cod 101
+ 1 byte displacement 0
------------------------------------------------------
reg,reg 1 byte modr/m=11 reg1 reg2
------------------------------------------------------
reg,[reg]
where reg
pointer !=ebp,esp ..........


Are you able to continue this table ;)
Posted on 2003-02-09 16:16:18 by The Svin
OK, I see nobody cares to finish my table, so
I have to finish it :)


operands: size of addr. format
reg 1 byte modr/m=11 cod reg
------------------------------------------------------
[reg]
where reg !=
esp or ebp 1 byte modr/m=00 cod reg
------------------------------------------------------
[esp] 2 bytes modr/m=00 cod 100
+ sib = 00 100 100
------------------------------------------------------
[ebp] 2 bytes modr/m=01 cod 101
+ 1 byte displacement 0
------------------------------------------------------
reg,reg 1 byte modr/m=11 reg1 reg2
------------------------------------------------------
[reg],reg
reg,[reg] 1 byte modr/m= 00 reg [reg]
where reg
pointer !=ebp,esp
------------------------------------------------------
reg,[ebp] 2 bytes modr/m= 01 reg 101
[ebp],reg + byte=0 as 1 byte displacement
-------------------------------------------------------
reg,[esp] 2 bytes modr/m= 00 reg 100
[esp],reg sib = 00 100 100
-------------------------------------------------------
reg,[imm32] 5 bytes modr/m = 00 reg 101
[imm32],reg 4 bytes imm32
-------------------------------------------------------
reg,[reg][imm8] 2 bytes modr/m = 01 reg [reg]
[reg][imm8],reg 1 byte imm8
where reg pointer
!= esp
-------------------------------------------------------
reg,[esp][imm8] 3 bytes modr/m = 01 reg 100
[esp][imm8],reg sib = 00 100 100
1 byte imm8
-------------------------------------------------------
reg,[reg][imm32] 5 bytes modr/m = 10 reg [reg]
[reg][imm32],reg 4 bytes imm32
where [reg]!=[esp]
-------------------------------------------------------
reg,[esp][imm32] 6 bytes modr/m = 10 reg 100
[esp][imm32],reg sib = 00 100 100
4 bytes imm32
-------------------------------------------------------
esp can not be 2 bytes modr/m = 00 reg 100
index!!! sib = ss iii bbb

reg,[indexreg*scale][basereg]
[indexreg*scale][basereg],reg
if base != [ebp]
-------------------------------------------------------
3 bytes modr/m = 01 reg 100
reg,[indexreg*scale][ebp] sib = ss iii 101
[indexreg*scale][ebp],reg byte = 0 as 1 byte displacement
--------------------------------------------------------
6 bytes! modr/m = 00 reg 100
reg,[indexreg*scale] sib = ss iii 101
[indexreg*scale],reg 4 zero bytes as dword displacement
---------------------------------------------------------
3 bytes
reg,[indexreg*scale][basereg][imm8] modr/m = 01 reg 100
[indexreg*scale][basereg][imm8],reg sib = ss iii bbb
byte for imm8 displacement
-----------------------------------------------------------
6 bytes
reg,[indexreg*scale][basereg][imm32] modr/m = 10 reg 100
[indexreg*scale][basereg][imm32],reg sib = ss iii bbb
4 bytes for imm32 displacement
------------------------------------------------------------

Address part size for not mentioned 1 operand,
will be the same as for reg, where is
constructed as it was in one of the above two operands formats.
Posted on 2003-02-09 19:27:00 by The Svin
Heya,

Very good explanation! :-)
You talked about opcode length calculation, i had written some code to do that here it is. I tryed to optmize it for size, and it's kinda small, no code mixed with data and only 406 bytes(support general x86/fpu/mmx/cmov etc instructions)

So that may help, if somebody needs to understand how opcode length can be calculated.

uNdErX
Posted on 2003-02-18 01:34:54 by uNdErX
Would you mind if I copy and paste this as a text file? That way I can study it when the kids and the hubby are asleep and I might have a shot in hell of comprehending it.
Posted on 2003-02-21 18:32:56 by Kydwyn
Im searching on it all the Day, Looks like I have to change my Program Completely. Can I ask you a Favour? Would you mind to send all of this topic to myMail?
http://city.boleh.com/~vampire_strikes/DownLoad/Img.bmp (warning 600+k image)

edit: don't place such big images as image, place them as link. That way people on slow connections can decide wether or not to look at them.
Or better yet, take a free program such as irfanview and transform it to a compressed image format like gif or png before putting it on the web ;)
Posted on 2003-02-22 02:22:02 by realvampire
Kydwyn, of course I don't mind.
realvampire,
I'm sorry I don't have what I posted in "Opcode" topics as separate docs. It's kinda impovisation here in real time.
To test some material.
But I'll do it in the future, after all major parts of opcode system explained in
this "primal sketch", refined and supplyed with more exersizes and training programms.
I'm sure that to do "copy\paste" you can as seccesfull as me :)
Posted on 2003-02-23 14:41:15 by The Svin
General alpha, Private Reporting. I have found the formula. good use it, our second World war must start as soon as Posible, our High-Level language War been succesfull. Okay general. Here is the formula "A-C-D-B".



MOV EAX,EAX|8BC0|
MOV EAX,ECX|8BC1|
MOV EAX,EDX|8BC2|
MOV EAX,EBX|8BC3|
MOV EAX,ESP|8BC4|
MOV EAX,EBP|8BC5|
MOV EAX,ESI|8BC6|
MOV EAX,EDI|8BC7|
MOV ECX,EAX|8BC8|
MOV ECX,EDX|8BC9|
MOV ECX,ECX|8BCA|
MOV ECX,EBX|8BCB|
MOV ECX,ESP|8BCC|
MOV ECX,EBP|8BCD|
MOV ECX,ESI|8BCE|
MOV ECX,EDI|8BCF|
MOV EDX,EAX|8BD0|
MOV EDX,ECX|8BD1|
MOV EDX,EDX|8BD2|
MOV EDX,EBX|8BD3|
MOV EDX,ESP|8BD4|
MOV EDX,EBP|8BD5|
MOV EDX,ESI|8BD6|
MOV EDX,EDI|8BD7|
MOV EBX,EAX|8BD8|
MOV EBX,ECX|8BD9|
MOV EBX,EDX|8BDA|
MOV EBX,EBX|8BDB|
MOV EBX,ESP|8BDC|
MOV EBX,EBP|8BDD|
MOV EBX,EDI|8BDE|
MOV EBX,ESI|8BDF|
MOV ESP,EAX|8BD0|
MOV ESP,ECX|8BD1|
MOV ESP,EDX|8BD2|
MOV ESP,EDX|8BD3|
MOV ESP,ESP|8BD4|
MOV ESP,EBP|8BD5|
MOV ESP,ESI|8BD6|
MOV ESP,EDI|8BD7|
MOV EBP,EAX|8BE8|
MOV EBP,ECX|8BE9|
MOV EBP,EDX|8BEA|
MOV EBP,EDX|8BEB|
MOV EBP,ESP|8BEC|
MOV EBP,EBP|8BED|
MOV EBP,ESI|8BEE|
MOV EBP,EDI|8BEF|
MOV ESI,EAX|8BF0|
MOV ESI,ECX|8BF1|
MOV ESI,EDX|8BF2|
MOV ESI,EBX|8BF3|
MOV ESI,ESP|8BF4|
MOV ESI,EBP|8BF5|
MOV ESI,EDI|8BF6|
MOV ESI,EDI|8BF7|
MOV EDI,EAX|8BF8|
MOV EDI,ECX|8BF9|
MOV EDI,EDX|8BFA|
MOV EDI,EBX|8BFB|
MOV EDI,ESP|8BFC|
MOV EDI,EBP|8BFD|
MOV EDI,ESI|8BFE|
MOV EDI,EDI|8BFF|
MOV EAX,[EAX]|8B00|
MOV EAX,[ECX]|8B01|
MOV EAX,[EDX]|8B02|
MOV EAX,[EBX]|8B03|
MOV EAX,[ESP]|8B04|
MOV EAX,[EBP]|8B05|
MOV EAX,[ESI]|8B06|
MOV EAX,[EDI]|8B07|
MOV ECX,[EAX]|8B08|
MOV ECX,[ECX]|8B09|
MOV ECX,[EDX]|8B0A|
MOV ECX,[EBX]|8B0B|
MOV ECX,[ESP]|8B0C|
MOV ECX,[EBP]|8B0D|
MOV ECX,[ESI]|8B0E|
MOV EAX,[EDI]|8B0F|
MOV EDX,[EBX]|8B13|


I will conquer Australia with this.
Posted on 2003-02-23 21:04:19 by realvampire
Here is the second educational demo\training application,
that comes along with the "Opcode" tutorials.
Purpose and aim of such software were declared at start of
the tutorials - along with plain text explonations, some exersizes
and other demonstartion matirial may have even greater effect
that simple staring at documentation text.

The app is much simular to the previous app.
This time (abstructing from complex variaty of possible opcode
constructions) it focuses on the following opcode issues:
1. Meaning and use of bit "W" in code field. That specifys operand size.
2. Usage two possible reg sets coded in the same field
- full registers set
- partial registers set
3. Usage of byte modr/m with
mod = 11
code or reg field used for "code extention"
mem or reg field used for register either full or parial.

First tab is used as refference - you can
change instructions and operands looking how bitfields are change according
to specifyed by the buttons pressed mnemonics.
To change partial\full registers sets - click on letter "W" that refer to W bit of code field
it will compliment the bit and changes texts on "registers - buttons" according
to value of the bit W set (W=0 - partial registers set; W=1 - full registers set)

Next two tabs- dialogs are served to test your understanding:
"Tell me mnemonic" tab asks you to set buttons both for instruction and operand.
When buttons of both groups pressed the programme assumes that you are done
with the task and checks if you are right.
If you are - you can see it in message text and the programm offers you next opcode to
decode, otherwise you can see text that you are incorrect.
The programm also displays results of your test incrimenting numbers reffering to statistics
of right and wrong answers.
To change full\partial registers set - click on button on the bottom of the dlg.

"Tell me opcode" tab asks you to encode displayed mnemonic both in bynary and hex.
Set bynary bottons and type opcode in hex (2 bytes without space), after that - press "test" button.
If you are right - you can see text about it, right answers statistic incremented,
bit buttons reset, hex text cleared and new mnemonic for you to encode in bynary
and hex.
Otherwise - you can see text that you are incorrect, and wrong answers statistic incremented.
You can also press space while in hex edit widow to go for test.
(Pressing space in the window generates the same message as test button )

If you have ideas on your own about exersizes to help understand opcode construction,
or might offer your own educational programms that relates to the discussed in the "Opcode"
material - please, share it here submitting you work.

I hope this next simple little app will be of some additinal help for those who carefully read
this thread and studying i32 opcode construction.
Posted on 2003-02-28 11:00:06 by The Svin
I have change my System. Choose TheCompiler Button, get there. You'll see two button upper left corner, the first button used to modify the source code. Click Activate to Activate this feature.

How to use:
First see 2 Button above the Compiler. First Button used to execute notepad. Click it. Modify the code, you can see the Binner and its Opcode.

Hope this is UnderstandAble. I change the Attachment.
Posted on 2003-03-01 07:37:17 by realvampire
realvampire,
I tried to understand your "Game" twice, but failed.
Could, you please explain - what is it about and how one could use it?
Posted on 2003-03-01 09:18:03 by The Svin
I clicked all buttons I found and none lanched notepad.
After clicking the button you mentioned - code just disappeared and some decimal (19\37) appears and nothing more.
Posted on 2003-03-02 10:31:39 by The Svin
:eek: so it cannot work? owww...., I dont know. NotePad used for editing the source. Yes it will disappears, bcause notepad are not always on top. I'll repair it as soon as possible. Thanks for telling me.
any suggestion ?
Posted on 2003-03-02 20:34:52 by realvampire
I wrote next part of "opcodes" in text OEM font file.
It uses some psuedo gfx so view it in consol txt editor
or in notepad with Terminal OEM font.
Posted on 2003-03-05 18:13:23 by The Svin
I'll added it on my website. Is that Okay ?

For everyone who understand how to use my program, pls check its mnemonic and Opcode, is there any wrong with it?
Posted on 2003-03-05 18:41:19 by realvampire
I'll added it on my website. Is that Okay

It's OK, though I think it has sence only with previous tuts together.
Many things could be unclear without previous material.
You could also add both training apps to your site if you wish.
Posted on 2003-03-05 19:49:08 by The Svin
Here are some first simple question to test if you
understand what was discussed in the thread.
Let's see who's the bravest man here to answer them :)
Test questions:

1. Let reg1 - is any of general purpose registers
and reg2 is any of general purpose registers exept for
ebp and esp.
Why opcode
instr reg1,
is one byte shorter than
instr reg1, or instr reg1,

2. Let x is some value that -128 >= x <=127
Is there difference in size of opcodes
instr reg,
and
instr reg,
Why?

3. Let x is one of 1,2,4 or 8
Is there difference in size of opcodes:
instr reg,
and
instr reg,
Why?

4. You look at opcodes of two instructions.
byte[0] and byte[2] of both instructions are identical
byte[1] differs in the instructions.
Could you tell wich instruction is longer if you know
-That byte[1] in both instructions is byte modr/m
-First (most significant) hex digit
in the first instruction (x) 4 <= x < 8
in the second instruction (y) 8 <= y < C
Posted on 2003-03-06 14:19:29 by The Svin
:stupid: I'll try it. Dion could you translate the question above please?.



1. instr reg , [reg2] = 000000.
instr reg, [esp] = 66000000.

It is 1 byte more long

2. Yes because it's need an Imm value to hold adddress or value for it.
instr reg, [ebp][x] is more long.

3. Instr reg,[esp] <=> 66 67 00 04.
Instr reg,[esp][esp*reg] <=> unknown.

4. Byte[0] at 66 67 00 04 is 04 and byte[2] is 67. Yes it is identical because
instruction byte is at byte[1].
Instruction with 2 operand have a same format at Bit 0-5. And memory is
at bit 6-7. if it was a memory refference (using '[' and ']') the bit at 6-7 would
be zero.
Ex:
instr reg,reg <=> 0000-0000-11-000-000
instr reg,[reg] <=> 0000-0000-00-000-000



:confused: Am I right ? :stupid: (I have to studied more. Nice you give us some test)
Posted on 2003-03-06 18:39:19 by realvampire
Am I right ?

No.
Please, carefully read questions, and all the thread.

Anyone else could give a try?
Posted on 2003-03-06 19:15:24 by The Svin
Ok, I'll give it a quick try before heading of to the physics test at school, so I'llbe a bit short of words.
1. Since the reg code for esp an ebp has a special meaning.
If mod field = 00 and r/m is 101 = ebp then there is no regiser used to calculate the address, instead an immediate value follows the modR/M byte.
If mod filed = 00 and r/m is 100 = esp then a SIB (scale index base) byte follows the modR/M byte.

2. Yes, since inst reg, would require thre bytes (if the instruction is one byte), instruction byte,modr/m byte, SIB byte.
when intr reg, would require an immediate value after the SIB byte (instruction byte, modR/M byte, SIB byte, immediate displacement byte).

3 No, since both would use a modR/M and a SIB byte. Encoding is shoter to encode then , since in the first case you don't have the 0 displacement.

4. XX ab XX
(4<=x<8) <=> (0100<=x<1000)
(8<=y<C) <=> (1000<=y<1100)
x is modR/M => mod is 01 ==> a one byte displacement
y is modR/M => mod is 10 ==> a dowrd displace ment follows
The instruciton wiht the modR/M y is longer then the instruction with x.

(I hope I got it right, any I'll have to go to the bus now so I can make the physics test at school, I hope I'll survive the test... :))
Posted on 2003-03-06 23:49:12 by scientica
x is modR/M => mod is 01 ==> a one byte displacement

:stupid: :confused:

mod is bit 6-7 is it?. (00)-000-000. So you mean 01-000-000 is a one byte displacement and 10-000-000 is dword byte displacement.
Posted on 2003-03-07 01:53:47 by realvampire