1
$\begingroup$

Can anyone explain how are conditionals implemented in the CPU?

Is special circuitry used?

$\endgroup$
  • 4
    $\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$ – David Richerby Nov 3 '17 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$ – Peter Leupold Nov 3 '17 at 15:48
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$
1
$\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 commutator is responsible to choose between the two ones, exploiting the control bit.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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