Confused with CMPSB instruction
Asked Answered
R

3

7

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?

Risley answered 11/5, 2012 at 13:43 Comment(3)
Um, why don't you see how CMPSB works in a CPU manual?Binns
Um @Alex I did. The confusion was due to the REP instruction.Risley
REP is not an instruction of its own. It's an instruction prefix, IOW instruction modifier. Documentation explains it too.Binns
T
17

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.

Typographer answered 11/5, 2012 at 14:15 Comment(0)
H
1

I think you have to use the REPE or REPNE prefix with cmpsb (It's been a while).

Higdon answered 11/5, 2012 at 14:6 Comment(1)
That's correct, you either use repe (repeat while equal) or repne (repeat while not equal).Foredeck
L
0

From my understanding, since I'm going through the same tutorial on brokenthorn, it will compare the first byte of Imagename with the first byte of the fat entry if they are the same.

It will continue until all 11 characters (filename and extension) at the first difference. It will set the ZF flag if they are the same, and the ZF flag will stay cleared.

So after comparing the entire filename, it will then jump to load that fat entry if they are the same. If not, it will load the next entry and compare that filename.

Lubalubba answered 24/1, 2017 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.