10

This:

testl   %esi, %esi
jle .L3
movl    %esi, %eax

If testl do a logical AND on esi the result can't never be less but only equals, either if esi is 0. In this way movl can't be reached. It's that true, or I'm missing somethings.

Step two:

f1:
    pushq   %rbp
    movq    %rsp, %rbp
    testl   %esi, %esi
    jle .L3
    movl    %esi, %eax
.L2:
    incb    (%rdi)
    incq    %rdi
    decq    %rax
    jne .L2
.L3:
    popq    %rbp
    ret

In a hypothetical C translation if .L3 consists of pop then ret and the branch take place it's possible to determine the value returned by the function?

0

1 Answer 1

17

"Less than or equal" is defined as: ZF=1 or SF != OF

The TEST instruction sets ZF and SF based on a logical AND between the operands, and clears OF.

So in effect you end up with the condition ZF or SF, meaning "Less than or equal to zero" (i.e. the jump would be taken if (signed int)esi <= 0 in this case).

Edit: For the second part of your question, it looks like it's doing something along these lines:

void f1(char *c, int len)
{
    if (len > 0) {
        for (i = len; i != 0; i--) {
            (*c)++;
            c++;   
        }
    }
} 
6
  • In a hypothetical C translation if .L3 consists of leave and ret and the branch take place, this means that the function return 0? Otherwise if jle fails and after the movl instruction there's an epilogue it woulds mean that the function return the integer value eax? Jul 1, 2013 at 18:46
  • Integral values are returned in eax on IA-32, so in the scenario you describe it would return the value of esi if esi > 0, otherwise it would return whatever was already in eax.
    – Michael
    Jul 1, 2013 at 18:53
  • If I don't have this information I can't determine what value is returned? Jul 1, 2013 at 19:05
  • Not sure which information you're referring to. Given a complete disassembly of a function it should be possible to determine what (if anything) it will return for a given input, unless it calls other functions that you neither have the code for or a description of.
    – Michael
    Jul 1, 2013 at 19:15
  • I don't think that code returns anything meaningful. See my updated answer.
    – Michael
    Jul 1, 2013 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.