关于X86 SDM中ADD指令格式与8086差异及04指令码的技术问询
ADD 04 ib Instruction Format in x86 SDM vs. 8086 General Format Great question—this is a super common confusion when transitioning from learning the original 8086 instruction set basics to diving into the full x86 Architecture Software Developer Manual (SDM). Let’s break this down clearly:
1. Your 8086 "general format" isn’t the only 8086 format
The OPCODE | DIRECTION | WIDTH | MOD | REG | R/M structure you’re thinking of is the general-purpose instruction format used for operations that involve registers, memory, or both (e.g., ADD AX, BX or ADD [BX], CL). But the 8086 instruction set also includes short-form, specialized opcodes designed to save space and speed up common operations—like immediate-value additions to the accumulator register (AL/AX).
2. Breaking down 04 ib
The 04 ib entry in the SDM refers to one of these specialized 8086-era instructions:
04: This is the single-byte opcode that explicitly encodes the operationAL = AL + imm8(8-bit addition of an immediate value to theALregister). No MOD/RM fields are needed here because the destination is fixed asAL, and the source is an immediate byte.ib: The suffixibstands for immediate byte—this is exactly what you guessed: the second byte in the instruction is the 8-bit immediate value to add toAL.
Example in action
If you write the assembly instruction:
ADD AL, 0x15
The corresponding machine code will be 04 15—where 04 is the opcode, and 15 is the ib immediate byte.
3. Why this doesn’t match your general format
The general format you learned applies to instructions where the CPU needs to decode which registers/memory locations to use. For accumulator-specific immediate operations, the opcode itself already defines the destination (AL/AX/EAX/RAX, depending on mode), so there’s no need for the MOD/RM fields. This was a deliberate design choice in 8086 to optimize common operations (since the accumulator was heavily used in early assembly code).
For completeness, there’s also a 16/32/64-bit version of this instruction:
05 iw(8086):AX = AX + imm16- In 32-bit mode,
05 idbecomesEAX = EAX + imm32 - In 64-bit mode,
05 idextends toRAX = RAX + imm32(sign-extended to 64 bits)
Wrap-up
The SDM’s instruction format descriptions include all x86 instructions—both the general-purpose ones with MOD/RM fields and the specialized short-form ones like 04 ib. This is why it looks different from the single general format you initially learned: you’re looking at a purpose-built opcode for accumulator immediate additions, which was part of the 8086 set from day one.
内容的提问来源于stack exchange,提问作者mahmood

