问题
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?
回答1:
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.
回答2:
I think you have to use the REPE or REPNE prefix with cmpsb (It's been a while).
回答3:
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.
来源:https://stackoverflow.com/questions/10552579/confused-with-cmpsb-instruction