beq
Branch on equal
Branches to label if $t1 equals $t2, or an immediate.
Used in
- Comparing two numbersMIPS assembly
- LoopsMIPS assembly
- Fill an array with a guarded counting loopMIPS assembly
- Sum of an arrayMIPS assembly
- The largest elementMIPS assembly
- Reverse a string in placeMIPS assembly
- Binary searchMIPS assembly
- The snake gameMIPS assembly
- Flappy birdMIPS assembly
- Play a tuneMIPS assembly
Forms
beq , ,
Pseudo-instructions
The assembler turns these into one or more real instructions.
Operands
$t1- Any general-purpose register,
$0to$31, using its numeric or named spelling, such as$8or$t0($zerois$0;$rais$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 (
-32768to32767) or a 32-bit value (-2147483648to4294967295). The assembler chooses a supported encoding for the value and other operands; a pseudoinstruction may expand into several instructions.
Uses a PC-relative 16-bit signed offset (multiplied by 4) so the target must be within 32K instructions (±128 KB). Commonly the first half of an if/else test.
# Run beq 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, 2
li $t2, 0
beq $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, 9
li $t3, 0
beq $t0, $t1, taken_two
li $t3, 1
b second_done # Skip the other marker unconditionally.
taken_two: li $t3, 2
second_done: