嵌入式x86汇编中的整数数组

3

我正在尝试访问一个整数数组中的值,但是已经尝试了几个小时都没有成功。这是我到目前为止的代码:

我只是想访问数组"arr"中的值,我已经知道如何对字符进行操作,但不知道如何对整数进行操作。

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;
}
1个回答

3
假设您想要的项的索引在eax中,您应该编写:

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

这相当于

edx = arr[eax]

编辑:

抱歉,我忘记了这是内联汇编。 lea edx,[arr]将在堆栈上加载参数arr的有效地址,而不是指针本身。 尝试这个:

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。我相信这就是你要找的结果。

1
为什么不直接使用 mov edx, [arr+eax*4] 呢? - Jens Björnhager
你不能使用mov指令来进行内部计算,只能使用lea指令。 - Mario
mov eax, 3 lea edx, [arr+eax*4] mov edx, [edx]当arr的值为0到10之间的整数时,edx会取一个非常巨大的值。 - Quentin
1
它行了!!!非常感谢你的帮助,我花了无数个小时来解决它! - Quentin
没问题!请投赞成票!=] - Mario
投票需要 15 点声望,我会努力获取并投票的 :),目前是 13/15 ,还要想办法获取最后两点。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接