prototype of strcmp()
int strcmp(
const char *string1,
const char *string2
);
the function returns an int whose interpretation is as follows
so i think that answers your question of when it jumps and when not
it jumps if eax is either > or < 0
it does not jump if eax == 0
Return Value
The return value for each of these functions indicates
the lexicographic relation of string1 to string2.
< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2
test eax,eax does a binary and of both inputs
and for it to jump eax needs to be 0
if eax is 0 test eax,eax will set the ZF to 1
else it will set the ZF to 0
normally test eax will be used if the program in higher languages test
the result like this
if(!strcmp( a, b ) ) { do something }
see a sample program and disassembly below
>>> eax = -1
>>> print eax & eax
-1
>>> eax = 0
>>> print eax & eax
0
>>> eax = 1
>>> print eax & eax
1
>>>
sample program
#include <stdio.h>
#include <string.h>
int main (void) {
char *first="same";char *secon="same";char *third="diff";char *forth="tiff";
int fis = strcmp(first,secon);
int sec = strcmp(first,third);
int tid = strcmp(first,forth);
printf("%8x %8x %8x\n",fis,sec,tid);
if(!strcmp(first,secon)){
printf("trings are same \n");
}
if( strcmp(first,third) == 1 ) {
printf("second string has a chareceter that is greater than first string\n");
}
if( strcmp(first,forth) == -1 ) {
printf("second string has a chareceter that is lesser than first string\n");
}
}
disassembly of main
EAX
is the register used by IA32 calling conventions to either return an interger value or a memory address to the calling routine. By design,strcpy
can return either -1,0 or 1 in EAX with 0 indicating both strings match.TEST EAX,EAX
tests whetherEAX
is zero or not and sets or unsets theZF
bit.cmp eax,0
: Test whether a register is zero with CMP reg,0 vs OR reg,reg? and `testl` eax against eax?