Skip to content

x86: fix operand and immediate sizes and 16-bit decoding in v5 - #3171

Closed
ricardojrdez wants to merge 7 commits into
capstone-engine:v5from
reverseame:v5-x86-imm-16bit-fixes
Closed

ricardojrdez wants to merge 7 commits into
capstone-engine:v5from
reverseame:v5-x86-imm-16bit-fixes

Conversation

@ricardojrdez

@ricardojrdez ricardojrdez commented Nov 17, 2026 •

Copy link
Copy Markdown

Your checklist for this pull request

  • I've documented or updated the documentation of every API function and
    struct this PR changes.
  • I've added tests that prove my fix is effective or that my feature works
    (if possible)

Detailed description

  • 00 -- x86: fix COMISD memory operand size
    • COMISD (66 0F 2F /r) compares 64-bit scalar doubles, not 128-bit values. Fixed printf64mem and operand details so the memory operand is printed and reported as qword (64 bits).
  • 01 -- x86: fix PUSH and ENTER immediate sizes
    • PUSH imm8 (6A) in 32-bit mode was sign-extending the immediate to 64 bits (0xffffffffffffffff instead of 0xffffffff).
    • ENTER (C8) printed both immediates as signed with size 4/4. They are now unsigned (enter 0xfefe, 0xfe) with actual sizes of 2 and 1 bytes.
  • 02 -- x86: honor mandatory 66 prefix in 16-bit mode
    • 0x66 in 16-bit mode was only handled as an operand-size override. It is now also recognized as a mandatory opcode prefix where required, while keeping its override behavior for GP instructions like bswap.
  • 03 -- x86: fix XSAVE and PTWRITE decoding in 16-bit mode
    • Reject 66 0f ae 20 in 16-bit mode (and 67 66 0f ae 20 in 32/64-bit) instead of decoding it as xsave, and decode f3 0f ae 20 as ptwrite dword ptr [...].
  • 04 -- x86: fix far JMP and CALL immediate sizes
    • imm_size was left uninitialized on direct far JMP (EA) and CALL (9A), and the offset size was always reported as 4 bytes. imm_size is now set to 2 (selector size) and the offset size is 2 bytes when applicable (16-bit mode without prefix, or 32-bit mode with 0x66).
  • 05 -- x86: fix hint NOP and CMOV operand sizes
    • Accept reserved hint NOPs across the full 0F 18–0F 1E range (including register forms and cldemote with 66/F2/F3). Also prevent ignored F2/F3 prefixes in 16-bit mode from changing operand sizes (e.g., on CMOVcc).

Test plan

Added test cases to suite/cstest/issues.cs covering:

  • 66 0f 2f 00 in 32-bit mode printing comisd xmm0, qword ptr [eax] with a 64-bit (qword) memory operand.
  • 6a ff (32-bit) and c8 fe fe fe immediate values and sizes.
  • 66 0f 2f 00 (comisd) and 66 0f ef c0 (pxor) in 16-bit mode.
  • Invalid 66 0f ae 20 / 67 66 0f ae 20 rejection and f3 0f ae 20 (ptwrite) in 16-bit mode.
  • Far JMP/CALL (EA/9A) imm_size and offset sizes in 16-bit and 32-bit modes (with and without 0x66).
  • 0F 18–0F 1E hint NOPs (0f 1c 08, 66 0f 1c 00) and 0xF3-prefixed CMOVO in 16-bit mode.

Run with cstest -f issues.cs.

Closing issues

N/A

- 66 0f 2f 00 (32): COMISD compares a scalar double, so its memory
  operand is now reported as qword instead of xmmword.

Backport of capstone-engine#2750.
- 6a ff (32, unsigned): the sign-extended imm8 of PUSH is now masked to
  the 32-bit operand size when printed, giving 0xffffffff instead of
  0xffffffffffffffff.
- c8 fe fe fe (32): ENTER immediates are no longer sign-extended; they
  now print as 0xfefe and 0xfe, with sizes 2 and 1 instead of 4 and 4.
- 66 0f 2f 00 (16): the 66 prefix is now taken as the mandatory prefix
  in 16-bit mode when it selects another instruction, so this decodes as
  COMISD with a qword operand instead of COMISS.
- 66 0f ef c0 (16): for the same reason, PXOR now uses the SSE2 xmm
  registers instead of the MMX ones.
- 66 0f ae 20 (16): XSAVE has no 66-prefixed form, so this encoding is
  now rejected as invalid.
- f3 0f ae 20 (16): PTWRITE was rejected in 16-bit mode; it now decodes
  with a dword memory operand.
- 66 ea 11 22 33 44 (32): far JMP with an operand-size prefix now
  reports imm_size 2 and a 2-byte offset, instead of an uninitialized
  imm_size and a 4-byte offset.
- 0f 1c 08 (32): 0F 1C /1 is a reserved hint NOP and now decodes as nop
  dword ptr [eax] instead of being rejected.
- 66 0f 1c 00 (16): CLDEMOTE takes no 66 prefix, and in 16-bit mode the
  66 prefix selects a 32-bit operand, so the hint NOP operand is now
  dword instead of word.
- f3 0f 40 00 (16): the F3 prefix no longer changes the operand size of
  CMOVO, which is word again, the 16-bit default.
@Rot127

Rot127 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

See #3170 (review)

@ricardojrdez

Copy link
Copy Markdown
Author

BTW do you want me to split this in smaller PRs? Perhaps they are easy to review individually

@Rot127

Rot127 commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Yes, that is certainly helpful.

@ricardojrdez

Copy link
Copy Markdown
Author

Closing this in favor of smaller PRs:

The remaining PRs will be opened as backports once the PRs in next are merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

X86 Arch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants