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;

}

相关文章:

  • 2022-12-23
  • 2022-01-23
  • 2021-04-22
  • 2021-07-19
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-14
  • 2021-07-26
  • 2021-07-18
  • 2021-09-08
  • 2022-01-23
  • 2022-12-23
  • 2021-04-10
相关资源
相似解决方案