Why repeat and repne do the same until movsb?

I will have an assembly soon, and during the preparation I noticed something strange. repe movsbrepeats when ZF = 0, and I was taught that I repeshould repeat, but CX is not equal to zero, and ZF = 1.
I did some testing and found that before the movsbinstructions rep, repethey repnework the same way.
What is the explanation for this?

edit: here is the code:

.model small
    .data
    A db   '   This     is    a    test '
    N  db  27
    .stack 10h
    .code
    mov ax,@data
    mov ds,ax
    mov es,ax
    cld
    mov al,' '
    mov cl,N
    xor ch,ch
    mov di,offset  A
    next:  repe scasb
    jcxz cont        ; jump if cx=0
    dec di
    inc cx
    xchg  si,di      ; swap between si and di
    push  cx
    push  di
    repe  movsb
    pop   di
    pop   cx
    repne scasb
    mov si,di
    jmp next
    cont: .exit
    end
+4
source share
2 answers

In machine code, there are actually only two different bytes of the prefix.

  • 0xF3 called REP when used with MOVS / LODS / STOS / INS / OUTS (instructions that do not affect flags)
  • 0xF3 REPE REPZ CMPS/SCAS
  • 0xF2 REPNE REPNZ CMPS/SCAS .

Intel insn reference manual REP F3 REP MOVS, F2. , REP MOVSB, , CPU, .:)

. NASM, , F2 A4 REPNE MOVSB. ( tag wiki).


, , , , REPNE MOVSB ​​ MOVSB. TZCNT REP BSF, CPU, BMI1, BSF. ( , , .)

, REP RET - AMD K8/K10 , RET.

Intel , , , . LZCNT ( REP BSR) BSR, , REP BSR - , .

+2

rep repe ; rep INS, OUTS, MOVS, LODS STOS; repe/repne CMPS SCAS ( ).

, repe , rep ; repne MOVS, , , rep ( , , ).

TL; DR: repne movsb / , . . repe movsb , rep movsb.

.

+3

Source: https://habr.com/ru/post/1658692/


All Articles