flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > number of bytes differs for simple instruction

Author
Thread Post new topic Reply to topic
trogne



Joined: 10 Sep 2018
Posts: 6
Using "use16" or "use32" or "use64", I don't understand why the number of bytes differs for this simple instruction :

"inc eax"

With use16, I get 2 bytes (using xxd -b file.bin, to see the binary) :

01100110 01000000

With use64, I get 2 bytes :

11111111 11000000


With use32, only 1 byte :

01000000



What can explain those differences, and why use32 is only 1 byte ?
Post 10 Sep 2018, 17:55
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 451
Location: Belarus
Different encodings. See Intel Software Developer’s Manual.

For 16-bit mode using eax (32-bit register) requires the $66 prefix. In 32-bit mode the prefix is not required since the operand is of default size, so it basically is equal to the one in 16-bit mode except for the size-override prefix.

In 64-bit mode the byte values $40–$4F that used to be INC/DEC instructions became so called REX prefixes which are specific to 64-bit mode. So, the encoding of INC is different here (uses $FF escape byte).
Post 10 Sep 2018, 18:18
View user's profile Send private message Visit poster's website Reply with quote
trogne



Joined: 10 Sep 2018
Posts: 6
Thanks, I understand the 16-bit prefix, but not the 64-bit prefix.

What do you mean by "byte values $40–$4F" and "REX prefix" ?

Why the extra 1 in second part : 11111111 11000000 instead of 11111111 01000000 ?
Post 10 Sep 2018, 18:55
View user's profile Send private message Reply with quote
trogne



Joined: 10 Sep 2018
Posts: 6
Oh I think I see.

The range "01000000" ($40) to "01001111" ($4F) pertains to 64-bit mode (REX prefix). So the encoding is different : the INC instruction is instead "11000000", and DEC is "11001000".

That's right ?
Post 10 Sep 2018, 19:09
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 451
Location: Belarus
The encoding of INC and DEC becomes a 2-byte opcode where the first byte has value $FF and it is used as the first byte for a few other instructions as well, and the second byte is used to determine which of those instructions is actually used. Note also that if you change the register the second byte value will change. AFAIR, it will be the least-significant 3 bits having values 000 = EAX, 001 = ECX, 010 = EDX 011 = EBX, etc.
Post 10 Sep 2018, 20:12
View user's profile Send private message Visit poster's website Reply with quote
Mikl___



Joined: 30 Dec 2014
Posts: 93
Hi, trogne!
look here examples of INC instruction in 64-bit mode
Post 11 Sep 2018, 00:02
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6999
Location: Kraków, Poland
Let me try provide a wider image.

There are two different ways to encode INC REG, one short and one long. Similarly, there are two ways of encoding DEC REG:

01000RRR - INC REG (RRR = 000 for EAX)
11111111 11000RRR - INC REG
01001RRR - DEC REG
11111111 11001RRR - DEC REG

Assemblers generate the shorter form whenever possible, the longer usually has no advantages.
But in long mode the short form no longer exists, these encodings have been taken over to mean REX prefix. Therefore the long one remains the only option.

In addition to that, instruction without prefixes (as the ones shown above) uses the size of register that is native to the mode.

66h prefix (01100110) changes the size to 32-bit when in 16-bit mode, or to 16-bit in all other modes.

USE16
01000000 - INC AX
01100110 01000000 - INC EAX
11111111 11000000 - INC AX
01100110 11111111 11000000 - INC EAX

USE32
01000000 - INC EAX
01100110 01000000 - INC AX
11111111 11000000 - INC EAX
01100110 11111111 11000000 - INC AX

USE64
(short form of INC/DEC does not exist)
11111111 11000000 - INC EAX
01100110 11111111 11000000 - INC AX
01001000 11111111 11000000 - INC RAX

The last instruction above uses REX prefix.
Post 11 Sep 2018, 06:59
View user's profile Send private message Visit poster's website Reply with quote
trogne



Joined: 10 Sep 2018
Posts: 6
Thank you Smile
Post 11 Sep 2018, 18:30
View user's profile Send private message 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-2018, Tomasz Grysztar.

Powered by rwasa.