Home/Instructions/Branch if Equal
BEQ

RISC-V BEQ Instruction Details

Instruction ManualB-type

Branch to PC-relative offset if rs1 equals rs2

Instruction Syntax

beq rs1, rs2, offset
Operand Breakdown
Source rs1: first comparison operand.
Source rs2: second comparison operand.
Immediate offset: 13-bit signed branch offset (bit 0 is implicitly 0, ±4 KiB range).
RV32IControl TransferBranch

Instruction Encoding

31..25
imm[12|10:5]
24..20
rs2
19..15
rs1
14..12
funct3
11..7
imm[4:1|11]
6..0
opcode

BEQ uses opcode 1100011 (0x63), funct3 000. The rs1 and rs2 fields hold the comparison operands, and the 13-bit immediate encodes the branch offset (±4 KiB).

Format: B-type
opcode: 1100011 (0x63)
funct3: 000 (0x0)

Instruction Behavior

BEQ (B-type, opcode=1100011, funct3=000) compares rs1 and rs2 and takes the branch (to PC + sign-extended 12-bit B-immediate in multiples of 2) if they are equal. The conditional branch range is ±4 KiB. If the condition is false, execution falls through.

Quick Understanding & Search Notes

BEQ is a conditional branch: when rs1 and rs2 satisfy the equal condition, the PC branches to the current PC plus the B-type offset; otherwise execution falls through.

The B-type branch offset is encoded in 2-byte units and reaches about +/-4 KiB from the current PC.
RISC-V conditional branches do not write rd and do not save a return address.

Official Spec Notes

These notes are checked against the RISC-V Unprivileged ISA manual and summarize operation semantics, immediate ranges, and edge behavior.

Common Usage Scenarios

Comparison & Detection

Understand this scenario with real code like «beq x1, x2, 0x100 # if x1 == x2, branch to pc+0x100».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is B-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

Range is ±4 KiB; use jump if beyond
B-type targets are encoded in 2-byte units; without compressed-instruction support, taken targets must be 4-byte aligned

FAQ

What is the BEQ branch range?

Conditional branches use a B-type immediate, giving an approximate reach of +/-4 KiB from the current PC.

What is the difference between signed and unsigned branches?

BLT/BGE compare as signed integers; BLTU/BGEU compare as unsigned integers. BEQ/BNE only test equality of the bit patterns.