3

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

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

const char *s1 and const char *s2 should contain the same string. If s2 points to the string "hello" and n is 4, how can I pass to s1 a decimal value that will also correspond to 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 passing in the decimal value for "h" in ASCII, and it seemed to be the right direction, but not fully.

Rio
  • 14,182
  • 21
  • 67
  • 107
  • `eax` needs to contain the address of a string, not a "decimal" value – Paul R May 13 '11 at 20:43
  • Following that line of thought, I tried passing in the decimal value of the 0x804bd80 (so that the address should be the same) - why wouldn't that work? – Rio May 13 '11 at 20:55
  • I'm not sure what the obsession is with "decimal" values - just put `0x804bd80` in eax and then both s1 and s2 should be pointing at the same string. – Paul R May 13 '11 at 21:26
  • The previous step tries to ensure that what %eax passes a sscanf "%d" query, so it has to be one that is %d, I think. I've added that line to the code. – Rio May 13 '11 at 21:51
  • 6
    you need to learn to program in a high level language before taking on assembly – David Heffernan May 13 '11 at 22:30
  • What exactly are you trying to do? Maybe if you explain what your end goal is, someone can help. As it stands, the question makes little sense. – Michael Burr May 14 '11 at 00:33
  • If you pass two equal strings, in case and length, then `strncmp` will return zero, by definition. A compiler may optimize the result to a condition code depending on the use of the return value. – Thomas Matthews May 14 '11 at 00:40
  • The goal is to provide to strcmp a piece of data into `%eax` such that when it gets moved into `%esp` and passed to strcmp, it returns 0. – Rio May 14 '11 at 01:56

1 Answers1

3

By definition, the return value of strncmp is zero for two strings that are the same in case and length.

Looking at your assembly code, the line:

test   %eax,%eax

is not part of the strncmp function.

Using a debugger, put a breakpoint at this instruction. Examine the EAX register, it should be zero (depending if the strncmp function returns its result in the EAX register).

The test assembly instruction will set condition codes depending on the value of the parameters. A popular condition code bit is the zero bit indicating an expression is zero. The next instruction may be a jump if condition code is zero.

If you use the result of the strncmp function in a mathematical statement or expression, the compiler may generate different code.

Try this fragment:

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

Is there a reason you need the compiler to save the value from strncmp?

Is there a reason you need the compiler to compare the value to constant numeric zero?

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • The goal is to provide to strcmp a piece of data into %eax such that when it gets moved into %esp and passed to strcmp, it returns 0. The `test %eax, %eax` checks to see if the result form strcmp is 0 or not, and the goal for this particular project is for it to be 0. – Rio May 14 '11 at 01:56