3
$\begingroup$

Can anyone explain how are conditionals implemented in the CPU?

Is special circuitry used?

$\endgroup$
4
  • 6
    $\begingroup$ I'm not really sure what you mean. The whole of the CPU is "special circuits" and implementing conditional statements isn't really any different from implementing anything else. (In the sense that implementing different instructions is obviously different in the details but there aren't really any conceptual differences.) $\endgroup$ Nov 3, 2017 at 13:40
  • $\begingroup$ There are "special" assembler commands, the conditional jumps mentioned by several people here. But these do not require any special hardware circuits - they run on the same ones as non-conditional commands. $\endgroup$ Nov 3, 2017 at 15:48
  • $\begingroup$ Jumps / branches are special in some ways: unlike any other instruction, they're a control dependency: the result of the branch affects what instruction needs to run next. In a pipelined CPU (lighterra.com/papers/modernmicroprocessors), this is a challenge, and various tricks to hide branch latency are necessary for it to not suck. (Most notably branch prediction, but CPUs like MIPS have used branch delay slots to hide 1 cycle of branch latency which was sufficient in classic MIPS R2000. $\endgroup$ Apr 7, 2021 at 3:00
  • $\begingroup$ Exceptions are another way the program-counter can change other than simply incrementing, but they're rare so CPUs can afford to handle them a lot less efficiently than branches. What exactly happens when a skylake CPU mispredicts a branch? $\endgroup$ Apr 7, 2021 at 3:01

2 Answers 2

3
$\begingroup$

The semantics of any non-jump CPU instruction is to increment the program counter PC (a.k.a. instruction pointer) so that the subsequent instruction can be executed next.

Uncoditional jumps overwrite PC with a new, fixed value, written in the instruction. (Or, for relative jumps, add to PC said value)

Conditional jumps instead consider two possible values for PC: one pointing to the next instruction, another written inside the instruction (possibly as a PC increment, for relative jumps). Then, they test some given register, obtaining a bit. For instance, the register might be a flag, which is taken as is. Otherwise, the register might be tested for parity, sign, zero, etc. so to obtain a single bit. This bit controls which one of the two PC values is the one to write in the PC register. Usually, a hardware 2-to-1 multiplexer is responsible to choose between the two ones, exploiting the control bit.

$\endgroup$
1
$\begingroup$

The basis of the if condition is the conditional jump. That means that a jump is taken only if a certain condition it true.

A non-conditional jump is a write to the program counter register. So a condition jump inhibits the write if the condition is not true.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.