由
int cread(int *xp)
{
return xp ? *xp:0;
}
翻译成汇编指令
movl $0,%eax
testl %edx,%edx
cmovne (%edx),%eax
这种实现是非法的。主要是vmovne指令的问题,参阅相关资料,我们可以得到:
For the memory-based forms of CMOVcc, memory-related exceptions may be reported even if the condition is false.
In 64-bit mode, CMOVcc with a 32-bit operand size will clear the upper 32 bits of the destination
register even if the condition is false.
可以做以下测试
int main()
{
int a=12;
//int *xp=&a;
int *xp=NULL;
int *x=NULL;
_asm
{
pushad
mov eax,0
mov edx,xp
test edx,edx
cmovne eax,[edx] //当edx为零时,也就是指针xp为零时,此指令会抛出异常
mov x,eax
popad
}
printf("%d\n",x);
return 0;
}