Branch

Perform a test & branch based on that test

The branch instructions perform a test against one of the processor's flags. Depending on the instruction a branch is taken if it is either clear or set.

If the branch is taken, a 1-byte signed displacement in the second byte of the instruction is sign-extended to 16-bits and added to the Program Counter. If the branch is not taken then the instruction immediately following the 2-byte instruction is executed.

The allowable range of the displacement is -128 to +127 from the instruction immediately following the branch.

BCC - Branch if Carry Clear

BCC tests the Carry flag and branches if it is clear.

It can be used in several ways:

  • Test the result of a shift into the carry
  • Determine if the result of a comparison is less than

Some assemblers accept BLT (Branch if Less Than) as an alternate mnemonic for BCC.

BCS - Branch if Carry Set

BCS tests the Carry flag and branches if it is set.

It can be used in several ways:

  • Test the result of a shift into the carry
  • Determine if the result of a comparison is greater than or equal

Some assemblers accept BGE (Branch if Greater Than or Equal) as an alternate mnemonic for BCS.

BEQ - Branch if Equal

BEQ tests the Zero flag and branches if it is set.

It can be used in several ways:

  • Test the result of a comparison is equal
  • Test the result of an Increment or Decrement operation is zero, useful in loops.
  • Test the value just loaded is zero
  • Test the result of an arithmetic operation is zero

BNE - Branch if Not Equal

BNE tests the Zero flag and branches if it is clear.

It can be used in several ways:

  • Test the result of a comparison is not equal
  • Test the result of an Increment or Decrement operation is not zero
  • Test the value just loaded is not zero
  • Test the result of an arithmetic operation is not zero

BMI - Branch if Minus

BMI tests the Negative flag and branches if it is set. The high bit of the value most recently affected will set the N flag. On 8-bit operations this is bit 7. On 16-bit operations (65816 only) this is bit 15.

This is normally used to determine if a two's-complement value is negative but can also be used in a loop to determine if zero has been passed when looping down through zero (the initial value must be positive)

BPL - Branch if Positive

BPL tests the Negative flag and branches if it is clear. The high bit of the value most recently affected will set the N flag. On 8-bit operations this is bit 7. On 16-bit operations (65816 only) this is bit 15.

This is normally used to determine if a two's-complement value is positive or if the high bit of the value is clear.

BVC - Branch if Overflow Clear

BVC tests the Overflow flag and branches if it is clear.

On the 6502 only 3 instructions alter the overflow flag: ADC, SBC & CLV.

On the 65C02 the BIT instruction also alters the overflow flag.

The PLP & RTI alter the flags as they restore all flags from the stack.

On the 65816 the SEP & REP instructions modify the v flag.

On some processors there's a Set Overflow hardware signal available, but on many systems there is no connection to that pin.

BVS - Branch if Overflow Set

BVS tests the Overflow flag and branches if it is set. It has the same limitations as the BVC instruction.

Flags Affected
None.
Instructions
SyntaxBranch ifOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
BCC nearlabel Carry clear 90 x x x 2 21, 2 Program Counter Relative
BCS nearlabel Carry set B0 x x x 2 21, 2 Program Counter Relative
BEQ nearlabel Equal, z=1 F0 x x x 2 21, 2 Program Counter Relative
BNE nearlabel Not Equal, z=0 D0 x x x 2 21, 2 Program Counter Relative
BMI nearlabel Minus, n=1 30 x x x 2 21, 2 Program Counter Relative
BPL nearlabel Positive, n=0 10 x x x 2 21, 2 Program Counter Relative
BVC nearlabel Overflow clear, v=0 50 x x x 2 21, 2 Program Counter Relative
BVS nearlabel Overflow set, v=1 70 x x x 2 21, 2 Program Counter Relative

Notes:

  1. Add 1 cycle if branch taken
  2. Add 1 more cycle if branch taken crosses page boundary on a 6502, 65C02 or a 65816 in 6502 emulation mode (e=1)

Last modified November 5, 2021: Add instruction categories (6b74ff9)