0

In instruction encoding Default sizes are:

operand size is 32 bit
address size is 64 bit 

We can use the legacy prefix:

0x66 – Operand-size override prefix

to make operand size 16. What if I want to make it 8 bits not 16?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Please don't try to circumvent the question limit with multiple questions in one. Users get suspended for that. – Nate Eldredge Jul 31 '21 at 18:02
  • 1
    One of your classmates already got suspended a few days ago for the exact same thing, so take it seriously. As for your question: 8 bit instructions have separate opcodes. – fuz Jul 31 '21 at 18:06
  • 1
    To learn more about this, try assembling and disassembling a lot of examples by yourself, comparing with a table such as https://wiki.osdev.org/X86-64_Instruction_Encoding. – Nate Eldredge Jul 31 '21 at 18:09
  • 1
    Just this once: `movl $1234, 64(%rax, %rbx, 4)`. `$1234` is an immediate, `64` is a displacement, `%rax` base, `%rbx` index, `4` scale. – Nate Eldredge Jul 31 '21 at 18:15
  • [Is there a default operand size in the x86-64 (AMD64) architecture?](https://stackoverflow.com/q/68289333) covers this, but it's a broader question. Still, it came up as the 3rd hit for my `site:stackoverflow.com x86 8-bit operand size opcode prefix` search. – Peter Cordes Aug 01 '21 at 02:05

1 Answers1

3

What if I want to make it 8 bits not 16?

You can't do this with prefixes. Instructions that support an 8-bit operand size do so with an entirely separate opcode, not with an override prefix.

For instance:

   0:   01 d9                   addl    %ebx,%ecx
   2:   66 01 d9                addw    %bx,%cx
   5:   00 d9                   addb    %bl,%cl

The 32-bit add is opcode 01, with a mod/rm byte of d9. The 16-bit add is identical but with the 66 operand size prefix. However the 8-bit add is opcode 00 instead.

The explanation for this is historical. The 16-bit 8086 CPU supported 8-bit and 16-bit operands, and used separate opcodes for the two: 00 for addb and 01 for addw. (This is still what you get when you run a modern chip in real mode, like in a boot sector.) The 32-bit 80386 wanted to add 32-bit operands while still supporting both 8 and 16, but there was no room for so many more opcodes, so for 32-bit protected mode they made all the formerly 16-bit instructions act as 32-bit instead, with an override available to go back to 16 bit, and they left the 8-bit instructions alone. (In real mode the operand size override has the opposite effect: 01 is addw and 66 01 is addl.)

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Your answers are amazing, please take a look at this too :) https://stackoverflow.com/questions/68605218/understanding-instruction-encoding –  Jul 31 '21 at 19:23