Simple Asm Q

Programming, for all ages and all languages.
Post Reply
Therx

Simple Asm Q

Post by Therx »

I'm working my way through bootf02 by john fine and I'm trying to "tidy" it up a bit and was wondering why:-

Code: Select all

find:     mov  si, file_name                      ; Pointer to Filename String
          mov  ecx, 11                            ; Compare 11 character string
          a32  rep cmpsb
          je   found                              ; Found file                                                  

   add   ecx, 21         ;Offset to next directory entry
   add     edi, ecx

          dec  bx                                 ; Loop through all entries
          jnz  find                               ; If BX > 0 goto find
wuold work while (replace add with mov)

Code: Select all

find:     mov  si, file_name                      ; Pointer to Filename String
          mov  ecx, 11                            ; Compare 11 character string
          a32  rep cmpsb
          je   found                              ; Found file                                                  

   mov   ecx, 21         ;Offset to next directory entry
   add     edi, ecx

          dec  bx                                 ; Loop through all entries
          jnz  find                               ; If BX > 0 goto find
wouldn't. Surelt ecx equals 0 after the rep

Pete
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Simple Asm Q

Post by Candy »

Surelt ecx equals 0 after the rep
Well, rep cmpsb quits checking after the first mismatch

The author assumed that it would be 11 after, IE, the first char wouldn't match. So, you found a bug.

Make it a mov ax, 32.
Therx

Re:Simple Asm Q

Post by Therx »

But the cmpsb increases the di pointer on each rep. Also from INtel manual:-

Code: Select all

Repeat Prefix Termination Condition 1 Termination Condition 2
REP            ECX=0                   None
REPE/REPZ      ECX=0                   ZF=0
REPNE/REPNZ    ECX=0                   ZF=1
This is rep not repe or repne

Pete

EDIT : Sorry abput table (was in WIKI mode ;D)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Simple Asm Q

Post by Candy »

Pete wrote: This is rep not repe or repne
according to the exact same manual, it can't be used with CMPSx, so your assembler probably assumes you want to use repe instead (repne is not logical for cmp, but at least it exists). Check your disassemblies, it's compiled to an F3 A6 opcode.
Therx

Re:Simple Asm Q

Post by Therx »

But that's not the bit I'm changing. And the first version works

Ok, yeh the disassembly shows it as a repe with confuses me even more. How come the first works?

Pete
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Simple Asm Q

Post by Candy »

Pete wrote: Ok, yeh the disassembly shows it as a repe with confuses me even more. How come the first works?
Chance.

Make a floppy with no files on it, dump the bootsector on it, dump a file on it with the first 4 characters (or so) the same as the file it wants to load, then dump that file on, then add another file with a similar start of the name, and watch it crash horribly.

Just a prediction.

It shows as a repe because the author meant a repe, didn't say it, and ended up with one. He tested it with his floppy with only the kernel file, which worked, and so he dismissed it as working. Obvious bad testing, but can you hold bad testing against a non-professional?
Therx

Re:Simple Asm Q

Post by Therx »

Nope. Still works
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Simple Asm Q

Post by Candy »

Pete wrote: Nope. Still works
ok, checked the full source this time, and it's not nice. Each time ecx is decreased by one, edi is increased by one. So you should add whatever's left of the search distance, plus 21, to edi. That's how it works.

don't see why it has to be written in such a confusing way though. You could spare a byte...
Therx

Re:Simple Asm Q

Post by Therx »

no, cx decreses but the pointers increase so i think it is going forwards
Post Reply