寒江独钓

博客园 首页 新随笔 联系 订阅 管理

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;

}

posted on 2012-06-23 15:53  X.W.LIU  阅读(993)  评论(0编辑  收藏  举报