Array of integers in x86 embedded assembly

I am trying to get values ​​from an array of integers and have tried for hours with no luck. Here is my code:

All I'm trying to do is get the values ​​in the "arr" array, I saw how to do this with characters, but not with integers.

int  binarySearch (int* arr, int arrSize, int key, int* count)
{
    int result=-1;
    int tempCount=0;
    __asm
{
    push esi;
    push edi;
    push eax;
    push ebx;
    push ecx;
    push edx;

    // START CODING HERE
     mov edx, dword ptr arr[1] ;

    // move the value of appropriate registers into result and tempCount;
    // END CODING HERE

    pop edx;
    pop ecx;
    pop ebx;
    pop eax;
    pop edi;
    pop esi;
}
*count = tempCount;
return result;
}
+3
source share
1 answer

Suppose the index of the element you want is in eax, you should write

lea edx, [arr+eax*4]
mov edx, [edx]

It is equivalent

edx = arr [eax]

Edit:

Sorry, but I forgot that this is a built-in asm. lea edx, [arr] will load the effective address of the arr parameter onto the stack, not the pointer itself. Try the following:

mov eax, 1;   //index you want to access
mov ebx, arr;
lea edx, [ebx+eax*4];
mov edx, [edx];



int  binarySearch (int* arr)
{
    int test;

    __asm
    {
        push eax;
        push ebx;
        push edx;

        mov eax, 2;
        mov ebx, arr;
        lea edx, [ebx+eax*4];
        mov edx, [edx];
        mov test, edx

        pop edx;
        pop ebx;
        pop eax;
    }

    return test;
}

int main(void)
{
    int a[5];

    a[0] = 0;
    a[1] = 1;
    a[2] = 21;

    int t = binarySearch(a);

    return 0;
}

t == 21 after running this program. I believe that this is what you are looking for.

+3
source

Source: https://habr.com/ru/post/1770781/


All Articles