Home/Instructions/Shift Right Arithmetic
SRA

RISC-V SRA Instruction Details

Instruction ManualR-type

Shift rs1 right arithmetically by the low log2(XLEN) bits of rs2, result in rd

Instruction Syntax

sra rd, rs1, rs2
Operand Breakdown
Destination rd: register receiving the operation result.
Source rs1: register holding the first operand.
Source rs2: register holding the second operand.
RV32IArithmeticShift

Instruction Encoding

31..25
funct7
24..20
rs2
19..15
rs1
14..12
funct3
11..7
rd
6..0
opcode

SRA uses opcode 0110011 (0x33), funct3 101, funct7 0100000. The rs1 and rs2 fields select the two source registers, and rd selects the destination register.

Format: R-type
opcode: 0110011 (0x33)
funct3: 101 (0x5)
funct7: 0100000 (0x20)

Instruction Behavior

SRA shifts rs1 right arithmetically by the amount in the low log2(XLEN) bits of rs2 (rs2[4:0] on RV32, rs2[5:0] on RV64), filling upper bits with the original sign bit. Result in rd. funct7=0100000, funct3=101, bit30=1. bit30=1 distinguishes it from SRL. Used for signed division by powers of two and sign-preserving right shifts.

Quick Understanding & Search Notes

SRA performs a arithmetic right shift, fills high bits with the original sign bit, and writes rd. The shift amount comes from the low log2(XLEN) bits of rs2.

Register shifts use only the low log2(XLEN) bits of rs2 as the shift amount.
Logical right shift fills with zeros; arithmetic right shift copies the sign bit, matching different signed/unsigned use cases.

Common Usage Scenarios

Multiplication & Division

Understand this scenario with real code like «sra x5, x6, x7 # x5 = x6 >> (x7[4:0]) (arithmetic, sign fill)».

Type Conversion

Understand this scenario with real code like «sra x5, x6, x7 # x5 = x6 >> (x7[4:0]) (arithmetic, sign fill)».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is R-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

bit30 must be 1 (funct7=0100000); otherwise SRL
Arithmetic shift rounds toward -inf; not same as truncation toward zero for division

FAQ

Where does the SRA shift amount come from?

For register shifts, the amount comes from the low log2(XLEN) bits of rs2.

What is the difference between logical and arithmetic right shift?

Logical right shift fills high bits with zeros. Arithmetic right shift copies the original sign bit, which is usually used to preserve signedness.