x86-64 Assembly test - jle

assembly, branch, x86

Solution

"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++;   
        }
    }
} 

Problem

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?

Original source