那两个输出错误结果的语法是从书上看来的,书上运行的是MASM8.0,因为我找不到编译器同时也不会用,所以就只在VC2008里试验了
C/C++ codevoid main()
{
int *x = new int[5];
x[0] = 1;
x[1] = 12;
x[2] = 123;
x[3] = 1234;
x[4] = 12345;
int y = 789;
int a[] = {1,12,123,1234,12345};
int result1, result2, result3;
_asm
{
mov eax, y //输出结果正确
mov result1, eax
mov eax, [x+2] //输出结果错误
mov result2, eax
mov eax, [a+2] //输出结果错误
mov result3, eax
}
cout<<result1<<endl;
cout<<result2<<endl;
cout<<result3<<endl;
}
嵌入汇编指针需要加前缀
#include <iostream.h>
void main()
{
int *x = new int[5];
x[0] = 1;
x[1] = 12;
x[2] = 123;
x[3] = 1234;
x[4] = 12345;
int y = 789;
int a[5] = {1,12,123,1234,12345};
int result1=0;
int result2=0;
int result3=0;
_asm
{
mov eax , y //输出结果正确
mov result1 , eax
/*
xor eax , eax
mov eax , dword ptr [a+16] //这里对于a数组
mov result2, eax
*/
xor eax , eax
mov eax , [ x ]
mov edx , [ eax + 2 * 4 ]
mov result2 , edx
}
cout<<result1<<endl;
cout<<result2<<endl;
cout<<result3<<endl;
}