Where does the SRL shift amount come from?
For register shifts, the amount comes from the low log2(XLEN) bits of rs2.
Shift rs1 right logically by the low log2(XLEN) bits of rs2, result in rd
SRL uses opcode 0110011 (0x33), funct3 101, funct7 0000000. The rs1 and rs2 fields select the two source registers, and rd selects the destination register.
SRL shifts rs1 right logically 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 zeros. Result in rd. funct7=0000000, funct3=101, bit30=0. Distinguished from SRA by bit30: SRL has bit30=0 (no sign extension), SRA has bit30=1 (sign extension).
SRL performs a logical right shift, fills high bits with zeros, and writes rd. The shift amount comes from the low log2(XLEN) bits of rs2.
Understand this scenario with real code like «srl x5, x6, x7 # x5 = x6 >> (x7[4:0]) (logical, zero fill)».
Understand this scenario with real code like «srl x5, x6, x7 # x5 = x6 >> (x7[4:0]) (logical, zero fill)».
Understand this scenario with real code like «srl x5, x6, x7 # x5 = x6 >> (x7[4:0]) (logical, zero fill)».
For register shifts, the amount comes from the low log2(XLEN) bits of rs2.
Logical right shift fills high bits with zeros. Arithmetic right shift copies the original sign bit, which is usually used to preserve signedness.