Quantcast
Channel: User Alex Lop. - Stack Overflow
Viewing all articles
Browse latest Browse all 39

Answer by Alex Lop. for Compiler optimization of strcmp I don't understand, against a constant string

$
0
0

The compiler inlined strcmp against a known-length string using repe cmpsb which implements memcmp.

It loads into register esi the address of the constant literal string "LETMEWIN\n". Note that the length of this string is 10 (with the '\0' at the end).Then it loads the address of buf into edi register, then it calls for the x86 instruction:

repz cmps BYTE PTR ds:[esi],BYTE PTR es:[edi]

repz repeats the following instruction as long as zero flag is set and up to the number of times stored in ecx (this explains you the mov ecx,0xa ; what is this?).

The repeated instruction is cmps which compares strings (byte by byte) and automatically increases the pointers by 1 on each iteration.When the compared bytes are equal, it sets the zero flag.

So per your questions:

Where is the strcmp call? And why is it like that?

No explicit call for strcmp, it is optimized out and replaced with inlined code:

 80484fc:   ba 46 86 04 08          mov    edx,0x8048646 ; "LETMEWIN\n" address 8048501:   b8 60 a0 04 08          mov    eax,0x804a060 ; buf address 8048506:   b9 0a 00 00 00          mov    ecx,0xa ; number of bytes to compare 804850b:   89 d6                   mov    esi,edx 804850d:   89 c7                   mov    edi,eax 804850f:   f3 a6                   repz cmps BYTE PTR ds:[esi],BYTE PTR es:[edi] ;

Actually it misses the part where it should check if the returned value of strcmp is zero or not. I think you just didn't copy it here. There probably should be something like je ... / jz ... / jne ... / jnz ... right after the repz ... line.

What does this 8048506: b9 0a 00 00 00 mov ecx,0xa do?

It sets the maximum number of bytes to compare.


Viewing all articles
Browse latest Browse all 39

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>