The problem: to define conditional jump we need a conditional jump? I mean in order to evaluate an IF the processor has to evaluate condition and IF it's true, then jump, otherwise not. It's an endless recursion.
We can define a conditional jump in terms of a conditional move. For example, a conditional jump can be:
- Increment the program counter to point to the next instruction.
- Test the condition.
- Conditionally load the program counter with the jump target if the condition tested true in step 2.
- Go to the instruction the program counter refers to.
Notice that step 3 is executed unconditionally. It is a conditional move, but we always execute the conditional move. No jump capability of any kind is required here.
So conditional jumps are built out of conditional moves which are not implemented using any kind of jump at all. The conditional move always takes place, it just doesn't always have the semantic affect of moving anything.
It is important to understand that conditional moves can be built out of unconditional moves. If we do some math that results in the value we unconditionally move being the same as the previous value we are moving on top of, the effect is the same as if the move never occurred. So we can build the semantic effect of a conditional move entirely out of unconditional code.
Here's an example of a way to implement a conditional move that doesn't involve any conditions:
- Multiply the current value of the program counter by the condition.
- Invert the condition.
- Multiply the current value of the jump target by the condition.
- Add the result of step 3 to the program counter.
You can see that this leaves the value of the program counter unchanged if the condition is zero and loads the program counter with the jump target if the condition is one. This code is a straight-through flow with no conditional code at all.
Because a conditional move can easily be built out of unconditional operations, reducing a conditional branch to a conditional move is sufficient to implement conditional branches.