2

otool on Mac gives this assembly for a strcmp

rep

cmpsb   %es:(%edi), (%esi)

movl    $__mh_bundle_header, %eax

je  0xe0eb

Some of this makes sense:

edi and esi are char pointers to the strings to be compared. cmpsb compares the first character of the two strings and increments edi and esi. rep repeats the following operation ecx times, so ecx contains the length of one of the strings.

What I don't understand:

rep is an unconditional repeat so will compare ecx characters of both strings and will only set flags for the last comparison. How does the loop stop when a mismatch is found?

What does es do? Is this used as an offset into both strings?

What is __mh_bundle_header?

Thank you for any help with this.

Dim St Thomas
  • 103
  • 1
  • 7
  • 2
    `REP` is the same as `REPE` when used with `CMPS`. You should use `REPE` for clarity, though. `es` is a segment register. Note that segmentation is not used, it just happens to be the default implicit segment for `CMPS` and your disassembler decided to show it. `__mh_bundle_header` is some unrelated symbol, no way we can tell from this much. – Jester Nov 18 '17 at 13:43
  • Thanks for the reply. Yes, looking at the hex dump the instruction is F3 A6, which is as you say repe cmpsb. I guess otool doesn't bother to be explicit as there is no rep cmpsb instruction anyway. I think __mh_bundle_header is some kind of offset used in mac bundles. – Dim St Thomas Nov 18 '17 at 15:32

1 Answers1

0

The REPE CMPS instruction is not simply unconditional. Apart from decrementing ecx until it is 0, it also checks the zero flag (ZF) which is set if the compare fails. See here for more details.

And like Jester said, the extended segment register is not really doing anything special and the __mh_bundle_header is a symbolic address name that serves as a store for the value in eax, whatever it is.

StarShine
  • 1,940
  • 1
  • 27
  • 45
  • What do you mean *serves as a store*? `movl $__mh_bundle_header, %eax` is a mov-immediate to `%eax` (of the address of the symbol). – Peter Cordes Nov 22 '17 at 03:28
  • Uhm yeah my academic lingo switch was still on. I meant that movl copies the value address $__mh_bundle_header to register eax – StarShine Nov 22 '17 at 08:52