Why take CMP ECX, ECX?

I am looking through some assembly and I see the line

CMP ECX, ECX

What does not make sense to me, because it is not always true A == A? (Reflective property)

Not sure if this will help, but it is used in this context:

CPU Disasm
Address   Hex dump          Command                                         Comments
00414A24  |.  39C9          CMP ECX,ECX
00414A26  |.  F3:A6         REPE CMPS BYTE PTR DS:[ESI],BYTE PTR ES:[EDI]
00414A28  |.  0F92C0        SETB AL
00414A2B  |.  0F97C2        SETA DL
00414A2E  |.  28C2          SUB DL,AL
00414A30  |.  0FBEC2        MOVSX EAX,DL
+4
source share
2 answers

If the ECX register is zero, then it repe cmpsbdoes not execute at all. This means that the following instructions setband setawill cause garbage!

Thus, the programmer decided to make sure that the flags have a certain state. In this case cmp ecx,ecxleads to CarryFlag = 0 and ZeroFlag = 1. setbmake AL zero and setamake DL zero.

@user35443 repe cmpsb ECX . , ECX , -.

@Hans Passant repe cmpsb Z , .

+2

@user3144770, CMP ECX, ECX CF ZF ( ), , , REPE CMPS ... , not, .

Intel REP/REPE/etc:

WHILE CountReg ≠ 0
  DO
    Service pending interrupts (if any);
    Execute associated string instruction;
    CountReg ← (CountReg – 1);
    IF CountReg = 0
      THEN exit WHILE loop; FI;
    IF (Repeat prefix is REPZ or REPE) and (ZF = 0)
    or (Repeat prefix is REPNZ or REPNE) and (ZF = 1)
      THEN exit WHILE loop; FI;
  OD;

:

  • ECX CMP ECX, ECX ... CMP EDX, EDX , !

  • , ZF REPE... , ECX != 0 .

  • , REPE... , , , ECX == 0 , CMPS, .

+1

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


All Articles