为什么会出现 "Access violation reading location" 错误?
Why does "Access violation reading location" error occur in assembly?
这是我的 C++ 代码,用于查找数组中的最大元素:
#include <iostream>
#include <conio.h>
using namespace std;
extern "C" int Max(int *i, int count);
int main() {
int i[10] = {1, 6, 7, 4, 8, 9, 6, 5 ,8 , 4};
cout<< "Max " << Max(i ,10) << endl;
_getch();
return 0;
}
未完成,但我在 mov eax, [esi]
行收到 访问冲突读取位置 错误。
我的 ASM 代码:
.code
Max proc
mov esi,ecx
mov ecx,edx
mov eax, [esi]
ret
Max endp
end
我该如何解决这个问题?
编辑:知道如何获取数组的第一个元素就足够了。
当我在调试模式下检查寄存器的值时,esi
不存在。有 rsi
寄存器代替它。然后我像这样重写代码;
mov rax,[rcx]
我删除了其他行。现在,这就像一个魅力。我可以获得数组的第一个元素。
感谢@JSF。
我看到的主要问题是您的代码更改了 esi 并且没有恢复对它的更改,这很危险。
您应该在函数的开头放置一个 "push esi",并在末尾添加一个 "pop esi"。
这是我的 C++ 代码,用于查找数组中的最大元素:
#include <iostream>
#include <conio.h>
using namespace std;
extern "C" int Max(int *i, int count);
int main() {
int i[10] = {1, 6, 7, 4, 8, 9, 6, 5 ,8 , 4};
cout<< "Max " << Max(i ,10) << endl;
_getch();
return 0;
}
未完成,但我在 mov eax, [esi]
行收到 访问冲突读取位置 错误。
我的 ASM 代码:
.code
Max proc
mov esi,ecx
mov ecx,edx
mov eax, [esi]
ret
Max endp
end
我该如何解决这个问题?
编辑:知道如何获取数组的第一个元素就足够了。
当我在调试模式下检查寄存器的值时,esi
不存在。有 rsi
寄存器代替它。然后我像这样重写代码;
mov rax,[rcx]
我删除了其他行。现在,这就像一个魅力。我可以获得数组的第一个元素。
感谢@JSF。
我看到的主要问题是您的代码更改了 esi 并且没有恢复对它的更改,这很危险。 您应该在函数的开头放置一个 "push esi",并在末尾添加一个 "pop esi"。