@samitouri / QOSamiQemu / commits / 03bd8515a3

target/i386/emulate: rework string_rep emulation

Signed-off-by: Mohamed Mediouni <mohamed@unpredictable.fr> Link: https://lore.kernel.org/r/20260223233950.96076-5-mohamed@unpredictable.fr Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Mohamed Mediouni committed Feb 24, 2026 at 00:39 UTC 03bd8515a3bc5d2da7d95105e5fbc7ede8e658d3
1 file changed +10 -3
target/i386/emulate/x86_emu.c
+10 -3
@@ -466,18 +466,25 @@ static inline void string_increment_reg(CPUX86State *env, int reg,
466 write_reg(env, reg, val, decode->addressing_size);
467 }
468
469 +static inline int get_ZF(CPUX86State *env) {
470 + return env->cc_dst ? 0 : CC_Z;
471 +}
472 +
473 static inline void string_rep(CPUX86State *env, struct x86_decode *decode,
474 void (*func)(CPUX86State *env,
475 struct x86_decode *ins), int rep)
476 {
477 target_ulong rcx = read_reg(env, R_ECX, decode->addressing_size);
474 - while (rcx--) {
478 +
479 + while (rcx != 0) {
480 + bool is_cmps_or_scas = decode->cmd == X86_DECODE_CMD_CMPS || decode->cmd == X86_DECODE_CMD_SCAS;
481 func(env, decode);
482 + rcx--;
483 write_reg(env, R_ECX, rcx, decode->addressing_size);
477 - if ((PREFIX_REP == rep) && !env->cc_dst) {
484 + if ((PREFIX_REP == rep) && !get_ZF(env) && is_cmps_or_scas) {
485 break;
486 }
480 - if ((PREFIX_REPN == rep) && env->cc_dst) {
487 + if ((PREFIX_REPN == rep) && get_ZF(env)&& is_cmps_or_scas) {
488 break;
489 }
490 }