How to make strcmp return 0 in assembly

I want the strcmp function call to return 0, which means

int strncmp(const char *s1, const char *s2, size_t n);

const char *s1and const char *s2must contain the same string. If it s2points to the string "hello" and nequals 4, how can I go to the s1decimal value, which will also match hello?

 8049e87:       c7 44 24 08 04 00 00    movl   $0x4,0x8(%esp) // 4
 8049e8e:       00
 8049e8f:       c7 44 24 04 80 bd 04    movl   $0x804bd80,0x4(%esp) // the constant is "hello"
 8049e96:       08 
 8049e97:       89 04 24                mov    %eax,(%esp) // The contents of %eax are a decimal (%d)
 8049e9a:       e8 61 ec ff ff          call   8048b00 <strncmp@plt>
 8049e9f:       85 c0                   test   %eax,%eax // I want this to be 0!

I tried to pass the decimal value for "h" in ASCII, and that seemed like the right direction, but not completely.

+3
source share
1 answer

By definition, the return value strncmpis zero for two strings that are the same for the case and length.

Having looked at the assembly code, the line:

test   %eax,%eax

not part of the function strncmp.

, . EAX, (, strncmp EAX).

test . , , . , .

strncmp , .

:

  volatile int result = 0x55;
  volatile int a_value = 3;
  result = (strncmp("Hausaufgaben", "Hausaufgaben", 256) + 27) / 3;
  printf("Result is: %d\n", result);

, strncmp?

, ?

+3

All Articles