From: Mohamed Mediouni Date: Mon, 23 Feb 2026 23:39:26 +0000 (+0100) Subject: target/i386/emulate: rework string_rep emulation X-Git-Tag: v11.0.0-rc0~43^2~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03bd8515a3bc5d2da7d95105e5fbc7ede8e658d3;p=thirdparty%2Fqemu.git target/i386/emulate: rework string_rep emulation Signed-off-by: Mohamed Mediouni Link: https://lore.kernel.org/r/20260223233950.96076-5-mohamed@unpredictable.fr Signed-off-by: Paolo Bonzini --- diff --git a/target/i386/emulate/x86_emu.c b/target/i386/emulate/x86_emu.c index 4409f7bc134..bf96fe06b45 100644 --- a/target/i386/emulate/x86_emu.c +++ b/target/i386/emulate/x86_emu.c @@ -466,18 +466,25 @@ static inline void string_increment_reg(CPUX86State *env, int reg, write_reg(env, reg, val, decode->addressing_size); } +static inline int get_ZF(CPUX86State *env) { + return env->cc_dst ? 0 : CC_Z; +} + static inline void string_rep(CPUX86State *env, struct x86_decode *decode, void (*func)(CPUX86State *env, struct x86_decode *ins), int rep) { target_ulong rcx = read_reg(env, R_ECX, decode->addressing_size); - while (rcx--) { + + while (rcx != 0) { + bool is_cmps_or_scas = decode->cmd == X86_DECODE_CMD_CMPS || decode->cmd == X86_DECODE_CMD_SCAS; func(env, decode); + rcx--; write_reg(env, R_ECX, rcx, decode->addressing_size); - if ((PREFIX_REP == rep) && !env->cc_dst) { + if ((PREFIX_REP == rep) && !get_ZF(env) && is_cmps_or_scas) { break; } - if ((PREFIX_REPN == rep) && env->cc_dst) { + if ((PREFIX_REPN == rep) && get_ZF(env)&& is_cmps_or_scas) { break; } }