top decor
Instruction set Conditional branches


The traditional logic of computer conditional branch is the following: if the specified condition is true, the instruction works out and the natural sequence of commands in program is override. But if condition is found to be false, branch instruction is ignored and the next command in the program is executed as usual.

The relative branch commands calculate the address of the next instruction adding an instruction's parameter (called bias) to current value of program counter. So processor skips several instructions or returns over several ones.

MMIX has 16 very strictly structured branch instructions. Half of them go forward (to greater address) using positive bias. Another half go backward and use negative bias. Note that their operation codes are 1 greater.

Every 8 instructions for forward and backward branches checks 8 conditions:

  • if negative (< 0)
  • if zero (= 0)
  • if positive (> 0)
  • if odd
  • if nonnegative (>= 0)
  • if nonzero (<> 0)
  • if nonpositive (<= 0)
  • if even
You can see all 6 mathematical case of inequality here; 2 rest are used for even control.

The table below shows all kinds of MMIX branches.

CodeMnemonicName Comments
40 (50)(P)BN $X, YZ (probable) branch if negative, forward branch forward if $X < 0
41 (51)(P)BNB $X, YZ (probable) branch if negative, backward branch backward if $X < 0
42 (52)(P)BZ $X, YZ (probable) branch if zero, forward branch forward if $X = 0
43 (53)(P)BZB $X, YZ (probable) branch if zero, backward branch backward if $X = 0
44 (54)(P)BP $X, YZ (probable) branch if positive, forward branch forward if $X > 0
45 (55)(P)BPB $X, YZ (probable) branch if positive, backward branch backward if $X > 0
46 (56)(P)BOD $X, YZ (probable) branch if odd, forward branch forward if $X is odd
47 (57)(P)BODB $X, YZ (probable) branch if odd, backward branch backward if $X is odd
48 (58)(P)BNN $X, YZ (probable) branch if nonnegative, forward branch forward if $X >= 0
49 (59)(P)BNNB $X, YZ (probable) branch if nonnegative, backward branch backward if $X >= 0
4A (5A)(P)BNZ $X, YZ (probable) branch if nonzero, forward branch forward if $X <> 0
4B (5B)(P)BNZB $X, YZ (probable) branch if nonzero, backward branch backward if $X <> 0
4C (5C)(P)BNP $X, YZ (probable) branch if nonpositive, forward branch forward if $X <= 0
4D (5D)(P)BNPB $X, YZ (probable) branch if nonpositive, backward branch backward if $X <= 0
4E (5E)(P)BEV $X, YZ (probable) branch if even, forward branch forward if $X is even
4F (5F)(P)BEVB $X, YZ (probable) branch if even, backward branch backward if $X is even

And one more detail. MMIX has additional 16 branches with codes 50-5F, structured exactly the same manner as 40-4F (in above table they are shown in brackets). They differ only in running time: longer when the branch is not taken (instructions with codes 40-4F in opposite are longer when the branch is taken!) For introductory purpose this dissimilarity is not significant, although from theoretical point of view it's interesting when organizing cycles with postcondition or precondition.

To applet
Examples:

  • instruction BNZ $1, 2 will test $1 and if it is not zero, skip 2 instructions; note that it means 4*2=8 byte!
    (for example if branch instruction has address 100, next command will have address 100+4*2=108)
  • BNZB $1, FFFE - all the same as previous, but the displasement is negative: -2 instructions backward
    (from 100 to F8)
  • any branch with zero displacement is dangerous: it leads to infinite looping (at least when condition is true)
  • any branch with bias 1 is absolutely useless: in all cases processor will execute the next instruction of the program


Related topics:

"MMIX basics" page
 

  (C) 2003, Evgeny Eremin. rEd-MMI project documentation
1