为什么条件移动工作不正常 [英] Why the conditional move doesn't work properly

查看:114
本文介绍了为什么条件移动工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译$ C $以下CS之后,功能似乎并不如预期的工作。

After I compiling the codes below, the function doesn't seem to work as expected.

int cread(int *xp){

     return (xp?*xp:0);

}

我提取如下程序集的版本相对应。

I extract the counterpart in the assembly version as below.

XP 注册%EDX

movl $0, %eax
testl  %edx, %edx
cmovne (%edx), %eax

谁能告诉我为什么指针 XP 还得到了由解引用的,即使测试失败 cmovne ?是不是 ZF 为test1 设置为 1 指令当%EDX 0

Could anyone tell me why the pointer xp still got de-referenced by the cmovne even when the test fails? isn't the ZF set to 1 by testl instruction when %edx is 0?

推荐答案

英特尔指令集手册似乎表明,如果条件不满足的CMOV操作数未读。 (我怀疑在手册中给出的指令中的算法,是不完全正确。)

The Intel instruction-set manuals seem to indicate that the CMOV operand isn't read if the condition isn't satisfied. (I suspect the "algorithm" for the instruction given in the manual, isn't quite right.)

显然,其他人不同意;看到明确的警告 rel=\"nofollow\">:

Apparently, other people disagree; see explicit caveat here:

如果源操作数是内存操作,那么它总是读,不管条件是否得到满足。这意味着,无论例外是已经从读取内存中产生,将获得生成的。如果内存读取会导致#GP或#PG,那就这样吧。

我怀疑的理由是这样的:指令去codeR读取指令,计算有效地址,并发出内存尽可能早地读取,以及之前的指令是完全去$ C $的CD和准备执行。因此,读取到内存中被调度/早执行,导致一个陷阱。它是直到CMOV实际上是由它知道阅读不需要存储器执行单元到达,并启动它,延迟将使指令很慢,以及该指令$ P $对 - 取逻辑复杂

I suspect the reasoning is this: the instruction decoder reads instructions, computes effective addresses, and issues memory reads as early as possible, well before the instruction is fully decoded and ready for execution. So, the read to memory gets scheduled/executed early and causes a trap. It isn't until the CMOV is actually reached by the execution unit that it knows the memory read isn't needed, and starting it that late would make the instruction really slow, as well as complicate the instruction pre-fetch logic.

我只用它在其登记册,登记表,这不能捕获。

I only use it in its register-register form, which can't trap.

这篇关于为什么条件移动工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆