处理器如何处理条件? [英] How does the processor handle conditions?

查看:78
本文介绍了处理器如何处理条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,超级底层的IF()是什么样的,它如何由x86处理器处理?

So, SUPER low-level what does an IF() look like, how is it handled by an x86 processor?

推荐答案

处理器具有如果...则分支指令,当满足特定条件时会分支,否则继续执行下一条指令。

The processor has "Branch if" instructions that when a certain condition is met it branches, and otherwise it continues on to the next instruction.

所以

if(A)
{
    dosomething;
}

将成为

load A into register 0
if the zero flag is set (ie, register 0 contains 0x00) then jump to endcondition)
dosomething
endcondition:

更复杂的条件( if(A || B& C) )成为使寄存器处于0或非零状态的指令序列,因此branchif指令可以根据条件标志跳转或不跳转。

More complex conditions ( if(A || B && C) ) become a sequence of instructions that leaves a register in a 0 or non zero state, so the branchif instruction can jump or not jump based on the conditional flags.

有许多条件标志(零,进位,负,溢出等),并且某些branchif指令还可以在更复杂的条件下运行(即,它实际上可能会检查某个寄存器是否等于另一个寄存器,而不是简单地查看标志)。每种架构都是不同的,需要权衡取舍,因此指令集既完整又快速,紧凑。

There are many conditional flags (zero, carry, negative, overflow, etc), and some branchif instructions also operate on more complex conditions (ie, it might actually check to see if a register is equal to another register, rather than simply looking at flags). Each architecture is different and makes tradeoffs so the instruction set is complete, but also speedy and compact.

正如moocha在评论中指出的那样,某些架构允许您应用对于某些,许多甚至全部指令都是有条件的,因此您可能不仅具有分支if指令,还可能具有 and if, add if, move if等。

As moocha points out in the comments, some architectures allow you to apply a conditional to some, many, or even all instructions, so you might not only have 'branch if' instructions, but also 'and if', 'add if', 'move if' etc.

一旦您进入流水线,无序执行,缓存,微代码和所有其他高级主题,x86就会非常非常非常复杂。对于大多数目的,以上解释就足够了。但是,如果您要编写手工制作的非常紧密缠绕的算法,则必须考虑这些因素,以实现最佳性能和吞吐量。

The x86 is very, very, very complex beyond this simple explanation once you get into pipelining, out of order execution, caching, microcode, and all the other advanced topics. For most purposes the above explanation is sufficient. If you're writing a hand crafted very, very tightly wound algorithm, though, you'll have to take these things into account for maximum performance and throughput.

话题是另一个问题……

-亚当

这篇关于处理器如何处理条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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