slli

slli

RISC-V Assembly Reference

lyszt.net covers slli and other RISC-V instructions in more detail.

Encoding format

R
31–25funct7
24–20rs2
19–15rs1
14–12funct3
11–7rd
6–0opcode
I
31–20imm[11:0]
19–15rs1
14–12funct3
11–7rd
6–0opcode
S
31–25imm[11:5]
24–20rs2
19–15rs1
14–12funct3
11–7imm[4:0]
6–0opcode
B
31–25imm[12,10:5]
24–20rs2
19–15rs1
14–12funct3
11–7imm[4:1,11]
6–0opcode
U
31–12imm[31:12]
11–7rd
6–0opcode
J
31–12imm[20,10:1,11,19:12]
11–7rd
6–0opcode

Description

slli shifts bits left by a fixed amount. The operation matches sll: bits slide left, zeros fill the right side, bits that pass the left edge are lost, and each place moved doubles the value (the sll page covers the mechanics). The instruction holds the shift distance as a constant, so no register provides it. The i means immediate, which is the term for a constant written into the instruction.

The form of slli appears below, where amount is a fixed number from 0 to 31.

slli rd, rs1, amount

The next line shifts t0 left by 3 places, which multiplies it by 8.

slli t1, t0, 3

Arrays make this instruction common. A 32-bit value in an array occupies 4 bytes, so item number i lies i times 4 bytes from the start, and a left shift by 2 multiplies by 4. The usual pattern shifts the index to produce a byte offset, adds the array's starting address, then loads the value. Spotting this shift, add and load sequence helps when reading assembly written by others.

The RISC-V manual encodes shifts by a constant as a specialization of the I-type format, one of the standard instruction layouts. rs1 holds the operand to be shifted, and the lower 5 bits of the immediate field hold the shift amount. slli performs a logical left shift, and zeros enter the lower bits.

Use slli when the shift distance is known as you write the code. Use sll when the amount is computed while the program runs. The example shifts 5 left by 3, which gives 5 times 8, or 40.

Example

li t0, 5
slli t1, t0, 3   # t1 = 5 << 3 = 40  (5 * 8)