1. Encoding x86 Instructions (excerpt from)



5. x86 ADD Instruction Opcode



6. Encoding x86 Instruction Operands, MOD-REG-R/M Byte






9. MOD R/M Byte and Addressing Modes

  1. Addressing modes with 8-bit displacement fall in the range -128..+127 and require only a single byte displacement after the opcode (Faster!)

  2. The size bit in the opcode specifies 8 or 32-bit register size. To select a 16-bit register requires a prefix byte.

  3. The so-called scaled indexed addressing modes, SIB = scaled index byte mode.

  4. Note that there is no [ ebp ] addressing. It's slot is occupied by the 32-bit displacement only addressing mode. Intel decided that programmers can use [ ebp+ disp8 ] addressing mode instead, with its 8-bit displacement set equal to zero (instruction is a little longer, though.)


10. SIB (Scaled Index Byte) Layout


SIB index register encoding SIB base register encoding

11. Scaled Indexed Addressing Mode






14. Encoding ADD ECX, EAX Instruction



15. Encoding ADD EDX, Displacement Instruction










16. Encoding ADD EDI, [EBX] Instruction



18. Encoding ADD EBX, [ EBP + disp32 ] Instruction











20. Encoding ADD ECX, [ EBX + EDI*4 ] Instruction



21. Encoding ADD Immediate Instruction


    There are three rules that apply:
  1. If opcode high-order bit set to 1, then instruction has an immediate constant.
  2. There is no direction bit in the opcode:
    • Indeed, you cannot specify a constant as a destination operand!
    • Therefore, destination operand is always the location encoded in the MOD-R/M bits of the the MOD-REG-R/M byte.
    • In place of the direction bit d, the opcode has a sign extension x bit instead:
      • For 8-bit operands, the CPU ignores x bit.
      • For 16-bit and 32-bit operands, x bit specifies the size of the Constant following at the end of the instruction:
        • If x bit contains zero, the Constant is the same size as the operand (i.e., 16 or 32 bits).
        • If x bit contains one, the Constant is a signed 8-bit value, and the CPU sign-extends this value to the appropriate size before adding it to the operand.
      • This little x trick often makes programs shorter, because adding small-value constants to 16 or 32 bit operands is very common.
  3. The third difference between the ADD-immediate and the standard ADD instruction is the meaning of the REG field in the MOD-REG-R/M byte:

    • Since the instruction implies that
      • the source operand is a constant, and
      • MOD-R/M fields specify the destination operand,
      the instruction does not need to use the REG field to specify an operand.
    • Instead, the x86 CPU uses these three bits as an opcode extension.
    • For the ADD-immediate instruction the REG bits must contain zero.
    • Other bit patterns would correspond to a different instruction.

28. ISA Design Considerations



AltStyle によって変換されたページ (->オリジナル) /