tags:

views:

26

answers:

1

I am trying to acces values from an array of integers and have been trying for hours with no luck. Here is my code so far:

All I am trying to do is acces the values in the array "arr", i have seen how to do it 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;

}

+2  A: 

Let's assume the index of the item you want is in eax, you would write

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

This is equivalent to

edx = arr[eax]

Edit:

Sorry but I forgot that this is inline asm. lea edx, [arr] will load the effective address of the parameter arr on the stack, not the pointer itself. Try this:

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 this program executes. I believe that is what you are looking for.

Mario
Why not just **mov edx, [arr+eax*4]**?
Jens Björnhager
You can't do that inner computation with the mov instruction, only lea.
Mario
i can't seem to get it to work, my code is now:
Quentin
mov eax, 3 lea edx, [arr+eax*4]mov edx, [edx] and edx takes on a ridiculously huge value while the values or arr are integers from 0 to 10
Quentin
It works!!! Thank you so much for your help, i spent countless hours trying to figure it out!
Quentin
No prob! Vote this up please! =]
Mario
Vote up requires 15 reputation, il get it and vote it up :), 13/15, gotta figure out how to get the last 2
Quentin