mercredi 9 mai 2018

if satatement in assembly does not work

Please, help me make it wright. I wrought this code in C:

include

int findArrayValue( int array[], int value, int size ){

int result = -1;

int i = 0;

while(i < size){

    if(array[i] == value) result = i;

    i++;

}



return result;  

}

and i tried to transform this code to assembly 32 bit:

findArrayValue:

    push   %ebp
    mov    %esp, %ebp
    mov    %edi, -24(%ebp)
    mov    %esi, -28(%ebp)
    mov    %edx, -32(%ebp)
    movl    $-1, -4(%ebp)
    movl    $0, -8(%ebp)

    jmp     whilestate

if:

    mov    -8(%ebp), %eax
    lea    0(,%ecx,4), %ebx
    mov    -24(%ebp), %ecx
    add    %ebx, %ecx
    mov    (%ecx), %eax
    cmp    %eax, -32(%ebp)
    jne     increment
    mov    -8(%ebp), %eax
    mov    %eax, -4(%ebp)

increment:

    incl    -8(%ebp)

while:

    mov    -8(%ebp), %eax
    cmp    -28(%ebp), %eax
    jl      ifstate
    mov    -4(%ebp), %eax
    pop    %ebp
    ret

My conclusion, is that i can not make if(array[i] == value) result = i; work, because output is always -1(initial result). Thank you very much for help!

Aucun commentaire:

Enregistrer un commentaire