Skip to content

x86: use effective address size for the loop counter register - #3025

Open
phverg wants to merge 1 commit into
capstone-engine:nextfrom
phverg:x86-loop-counter-addrsize
Open

x86: use effective address size for the loop counter register#3025
phverg wants to merge 1 commit into
capstone-engine:nextfrom
phverg:x86-loop-counter-addrsize

Conversation

@phverg

@phverg phverg commented Aug 11, 2026

Copy link
Copy Markdown

Fixes #2823.

The implicit counter register of loop/loope/loopne (and its regs_read/regs_write detail) was selected from the CPU mode alone (CX/ECX/RCX for 16/32/64-bit), ignoring a 0x67 address-size prefix. The loop counter register is chosen by the effective address size, so 67 e2 c2 (loop) in 64-bit mode reported rcx instead of ecx.

This selects the counter register from the effective address size (insn->detail->x86.addr_size, which the 0x67 prefix already accounts for), while the instruction-pointer register keeps following the mode.

Before:

$ cstool -d x64 67e2c2 | grep 'Registers read'
	Registers read: rip rcx

After:

$ cstool -d x64 67e2c2 | grep 'Registers read'
	Registers read: rip ecx

The fix also corrects the 16-bit and 32-bit cases where 0x67 changes the effective address size (16-bit + 0x67 → ECX, 32-bit + 0x67 → CX). The three affected tests/details/x86.yaml fixtures (which carried # BUG: should be ecx markers) are updated to ecx, and cstest passes (40/40 cases).

The implicit counter register of loop/loope/loopne (and its regs_read/
regs_write detail) was chosen from the CPU mode alone (CX/ECX/RCX for
16/32/64-bit), ignoring a 0x67 address-size prefix.  In 64-bit mode
`67 e2 c2` (loop) therefore reported rcx instead of ecx.

Select the counter register from the effective address size
(insn->detail->x86.addr_size), which the 0x67 prefix already accounts
for, while the instruction-pointer register keeps following the mode.

Fixes capstone-engine#2823
@github-actions github-actions Bot added the X86 Arch label Aug 11, 2026
@Rot127

Rot127 commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

@hainest Would you mind taking a look?

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.

Incorrect registers in x86_64 loop/loope/loopne

2 participants