# `bne`

[MIPS Documentation](https://asm-editor.specy.app/documentation/mips.md) › [Instructions](https://asm-editor.specy.app/documentation/mips/instruction.md)

Web page: https://asm-editor.specy.app/documentation/mips/instruction/bne

**Branch on not equal**

Category: Control flow

Branches to label if the first register does not equal the second, or an immediate.

Used in: [Comparing two numbers](https://asm-editor.specy.app/learn/courses/mips/introduction/comparing-two-numbers.md) (MIPS assembly), [Branch on compare](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/branch-on-compare.md) (MIPS assembly), [Loops](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/loops.md) (MIPS assembly), [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/mips/think-in-assembly/arithmetic-logic-and-bits.md) (MIPS assembly), [The bitmap display and keyboard registers](https://asm-editor.specy.app/learn/courses/mips/talking-to-the-outside-world/the-bitmap-display-and-the-keyboard.md) (MIPS assembly), [Fill an array with a guarded counting loop](https://asm-editor.specy.app/learn/courses/mips/examples/counting-loop.md) (MIPS assembly), [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/mips/examples/keyboard-control.md) (MIPS assembly), [The snake game](https://asm-editor.specy.app/learn/courses/mips/examples/snake.md) (MIPS assembly), [Flappy ASM](https://asm-editor.specy.app/learn/courses/mips/examples/flappy-asm.md) (MIPS assembly)

## Forms

- `bne $t1, $t2, label`

## Pseudo-instructions

The processor does not have these forms. The assembler replaces each of them with one or more real instructions. Open a row to see them.

- `bne $t1, imm, label`
  - `bne $t1,-100,label`: Branch to statement at label if $t1 is not equal to 16-bit immediate → `addi $at, $zero, imm`; `bne $at, $t1, label`
  - `bne $t1,100000,label`: Branch to statement at label if $t1 is not equal to 32-bit immediate → `lui $at, hi(imm)`; `ori $at, $at, lo(imm)`; `bne $at, $t1, label`

## Operands

- `$t1`: Any general-purpose register, `$0` to `$31`, using its numeric or named spelling, such as `$8` or `$t0` (`$zero` is `$0`; `$ra` is `$31`).
- `$t2`: A general-purpose register. It may be the same register as `$t1`.
- `label`: A named address in the program, such as the start of a loop or a variable in `.data`.
- `imm`: An immediate value: a signed 16-bit value (`-32768` to `32767`) or a 32-bit value (`-2147483648` to `4294967295`). The size your value needs decides which real instructions the assembler uses. Open the row to see them for each size.

## Description

Opposite of 'beq'. Uses a PC-relative 16-bit signed offset. Commonly used to branch out of loops or skip else blocks.

## Example

```mips
# Run bne with one true and one false condition. A taken branch skips the assignment of 1; inspect $t2=2 for taken and $t3=1 for fall-through.
li $t0, 2
li $t1, 9
li $t2, 0
bne $t0, $t1, taken_one
li $t2, 1
b first_done # Skip the other marker unconditionally.
 taken_one: li $t2, 2
first_done:
li $t0, 2
li $t1, 2
li $t3, 0
bne $t0, $t1, taken_two
li $t3, 1
b second_done # Skip the other marker unconditionally.
 taken_two: li $t3, 2
second_done:
```
