Confused with CMPSB instruction

Hudson Worden picture Hudson Worden · May 11, 2012 · Viewed 11.4k times · Source

I have been looking at this code and I'm confused about the rep cmpsb line.

.LOOP:
      push    cx
      mov     cx, 0x000B                            ; eleven character name
      mov     si, ImageName                         ; image name to find
      push    di
 rep  cmpsb                                         ; test for entry match
      pop     di
      je      LOAD_FAT
      pop     cx
      add     di, 0x0020                            ; queue next directory entry
      loop    .LOOP
      jmp     FAILURE

I understand that it repeats cmpsb cx times but how does this compare the two strings? Say for example was comparing "Hey\0" and "hey\0" and this loop was comparing 4 character strings. The first characters are different and the EFlags register would be set accordingly. However, the cmpsb instruction is repeated and the next characters would be the same. I may be misunderstanding how cmpsb works but it looks like this loop does not correctly compare two strings. Does this loop in fact work?

Answer

Alex Hornung picture Alex Hornung · May 11, 2012

The reason REP works is because rep has the same encoding as REPE (F3h). In principle REPE is the right thing to use here, but depending on your assembler it might just take REP as correct.

So in reality you have a REPE cmpsb there, it's just that your (dis)assembler doesn't really know.