Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with CMPSB instruction

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?

like image 223
Hudson Worden Avatar asked May 11 '12 13:05

Hudson Worden


People also ask

What is the use of cmpsb instruction?

The CMPSB(W) instruction can be used to compare a byte(word) in one string (DS:offset in SI) with a byte (word) in another string (ES:offset in DI). The comparison is executed by subtracting the byte (word) in DI from the byte (word) in SI.

What is CMPS instruction?

More Detail. The CMPS instruction compares two strings. This instruction compares two data items of one byte, word or doubleword, pointed to by the DS:SI and ES:DI registers and sets the flags accordingly. You can also use the conditional jump instructions along with this instruction.

Which of the following is used as an instruction prefix to execute comparison of two strings?

REPE/REPZ Prefix Example: Comparison of two strings using REPE prefix.


1 Answers

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.

like image 115
Alex Hornung Avatar answered Sep 27 '22 17:09

Alex Hornung