From C64-Wiki
Jump to navigationJump to search

BNE (short for "Branch if Not Equal") is the mnemonic for a machine language instruction which branches, or "jumps", to the address specified if, and only if the zero flag is clear. If the zero flag is set when the CPU encounters a BNE instruction, the CPU will continue at the instruction following the BNE rather than taking the jump.

Since the zero flag is set if the result of an operation, or a byte retrieved from memory, equals zero, one of the uses for BNE is to check for such zero results. In this example, BNE is used to keep a loop "going" until the "count" in the X index register reaches zero:

       LDA #42     42 is the PETSCII code for an asterisk; "*"
       LDX #10     Let's print 10 of those on the screen,
Loop:  JSR CHROUT  using the CHROUT routine in KERNAL
       DEX         Count down for each one printed
       BNE Loop    Done all 10 yet? If not, go back to Loop.

The zero flag is also affected as a result of comparisons (see CMP, CPX, and CPY), and so BNE, and its counterpart BEQ, is often used after a comparison to redirect program execution depending on whether the compared values are equal or not, e.g.:

LDA NumA    Read the value "NumA"
CMP NumB    Compare against "NumB"
BNE Differ  Go to label "Differ" if "NumA" <> "NumB"
...         Execution continues here if "NumA" = "NumB"

Addressing mode[edit | edit source]

Opcode Addressing
in bytes
Number of
Dec Hex
208 D0 Relative BNE nn 2 2*

BNE only supports the Relative addressing mode, as shown in the table at right. In the assembler formats listed, nn is a one-byte (8-bit) relative address. The relative address is treated as a signed byte; that is, it shifts program execution to a location within a number of bytes ranging from -128 to 127, relative to the address of the instruction following the branch instruction.
The execution time for BNE is not a fixed value, but depends on the circumstances. The listed time is valid only in cases where BNE does not take the branch. If it does take the branch, execution takes one additional clock cycle. Furthermore, if the branching crosses a page boundary, yet another cycle must be added to the execution time listed.

CPU flags[edit | edit source]

BNE does not affect any of the CPU's status flags.