Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86-64 Assembly test - jle

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?

like image 288
Fabio Carello Avatar asked Jul 01 '13 18:07

Fabio Carello


People also ask

What does JLE mean in assembly?

JLE. Jump if Less or Equal.

What is test AL AL in assembly language?

In x86 assembly, al is the least significant byte of eax register, which is typically used to return values from function calls. The test al,al is a bitwise AND operation between al and itself. If al & al == 0, the zero flag will be set to 1.

What does test mean in assembly code?

In the x86 assembly language, the TEST instruction performs a bitwise AND on two operands. The flags SF , ZF , PF are modified while the result of the AND is discarded. The OF and CF flags are set to 0 , while AF flag is undefined.

What does test %eax %eax do?

eax contains the return value of strcmp. test is like bitwise and except it only sets the flags. Anding a value with itself gives the same value, so test eax, eax sets the flags based on whatever eax contains. ZF is set when the result of an operation is zero.


1 Answers

"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++;   
        }
    }
} 
like image 156
Michael Avatar answered Oct 29 '22 07:10

Michael