RISC-V manual

The instruction set cheatsheet

ortfero

The RISC-V instruction set as the aldan machine runs it: RV64 with M, F, D, Zicsr, Zifencei, Zba, Zbb, Zbs, Sdtrig and a subset of V, in machine mode. Each instruction is listed with what it does; whatever aldan leaves out is marked. Opcodes, CSR numbers and codes are hexadecimal; bit positions, counts and sizes are decimal. The devices and the memory map are in the aldan codex.

Aldan manual

Architecture

Extensions

NameWhat it addsAldan
IThe base integer setYes
MMultiply and divideYes
AAtomics: LR/SC and AMONo
FSingle-precision floatYes
DDouble-precision floatYes
C16-bit compressed encodingsNo
VVectorsSubset
ZicsrCSR accessYes
Zifenceifence.iA no-op
ZicntrCounters: time, cycle, instretPartial
ZbaAddress generationYes
ZbbBasic bit manipulationYes
ZbsSingle-bit operationsYes
SdtrigDebug triggersYes

Aldan has time and instret but not cycle. G is shorthand for IMAFD with Zicsr and Zifencei, so aldan is rv64imfdv_zicsr_zifencei_zba_zbb_zbs: G without A, plus V.

Privilege levels

LevelNameAldan
3MachineThe only one
1SupervisorAbsent: no paging, no satp
0UserAbsent: no PMP

Integer registers

32 registers of XLEN = 64 bits, x0 to x31, and the pc. x0 always reads 0 and drops writes. The ABI names and who saves them across a call:

RegisterABIUseSaved
x0zeroAlways 0—
x1raReturn addressCaller
x2spStack pointerCallee
x3gpGlobal pointer—
x4tpThread pointer—
x5-x7t0-t2TemporariesCaller
x8s0, fpSaved, frame pointerCallee
x9s1SavedCallee
x10-x11a0-a1Arguments, resultsCaller
x12-x17a2-a7ArgumentsCaller
x18-x27s2-s11SavedCallee
x28-x31t3-t6TemporariesCaller

Float registers

32 registers f0 to f31 of FLEN = 64 bits, and fcsr.

RegisterABIUseSaved
f0-f7ft0-ft7TemporariesCaller
f8-f9fs0-fs1SavedCallee
f10-f11fa0-fa1Arguments, resultsCaller
f12-f17fa2-fa7ArgumentsCaller
f18-f27fs2-fs11SavedCallee
f28-f31ft8-ft11TemporariesCaller

A single held in a 64-bit register is NaN-boxed: the upper 32 bits are all ones. A single that is not boxed reads as the canonical NaN.

Vector registers

32 registers v0 to v31 of VLEN = 256 bits on aldan, and the CSRs vl, vtype, vstart, vxrm and vxsat. v0 holds the mask of a masked instruction. All of them are caller-saved.

Memory

Instruction formats

Every instruction is one 32-bit word. The opcode is bits 6-0, rd 11-7, funct3 14-12, rs1 19-15 and rs2 24-20, in every format that has them.

       31-25     24-20   19-15  14-12    11-7     6-0
R  |   funct7  |  rs2  |  rs1  |  f3 |    rd   | opcode |
I  |      imm 11:0     |  rs1  |  f3 |    rd   | opcode |
S  |  imm 11:5 |  rs2  |  rs1  |  f3 | imm 4:0 | opcode |
B  |  12,10:5  |  rs2  |  rs1  |  f3 |  4:1,11 | opcode |
U  |            imm 31:12            |    rd   | opcode |
J  |       imm 20,10:1,11,19:12      |    rd   | opcode |
R4 |  rs3,fmt  |  rs2  |  rs1  |  rm |    rd   | opcode |
V  |   f6,vm   |  vs2  |  vs1  |  f3 |    vd   |   57   |

Major opcodes

OpcodeNameOpcodeName
03LOAD33OP
07LOAD-FP37LUI
0FMISC-MEM3BOP-32
13OP-IMM43-4FMADD, MSUB, NMSUB, NMADD
17AUIPC53OP-FP
1BOP-IMM-3257OP-V
23STORE63BRANCH
27STORE-FP67JALR
6FJAL
73SYSTEM

LOAD-FP and STORE-FP carry the vector loads and stores too, told apart by the width field.

Base integer set

Arithmetic and logic

InstructionWhat it does
add rd, rs1, rs2rd = rs1 + rs2
sub rd, rs1, rs2rd = rs1 - rs2
addi rd, rs1, immrd = rs1 + imm
and, or, xorBitwise, rd = rs1 op rs2
andi, ori, xoriBitwise with imm
slt rd, rs1, rs2rd = 1 if rs1 < rs2 signed, else 0
sltuThe same, unsigned
slti, sltiuThe same against imm; sltiu still sign-extends imm
lui rd, immrd = imm << 12
auipc rd, immrd = pc + (imm << 12)

Overflow is ignored: there are no flags and no carry. A carry out of an add is sltu rd, sum, rs1.

Shifts

InstructionWhat it does
sll rd, rs1, rs2Shift left by rs2 and 63
srlLogical shift right
sraArithmetic shift right
slli, srli, sraiBy a 6-bit shamt

32-bit operations

Each works on the low 32 bits and sign-extends the 32-bit result to 64.

InstructionWhat it does
addw, subw32-bit add, subtract
addiw rd, rs1, imm32-bit add of imm; addiw rd, rs, 0 is sext.w
sllw, srlw, sraw32-bit shifts by rs2 and 31
slliw, srliw, sraiwBy a 5-bit shamt

Loads and stores

InstructionWhat it does
ld rd, off(rs1)Load 64 bits from rs1 + off
lw, lh, lbLoad 32, 16, 8 bits, sign-extended
lwu, lhu, lbuThe same, zero-extended
sd rs2, off(rs1)Store 64 bits at rs1 + off
sw, sh, sbStore the low 32, 16, 8 bits

Branches and jumps

InstructionWhat it does
beq rs1, rs2, labelBranch if rs1 = rs2
bneIf rs1 is not rs2
blt, bgeIf rs1 < rs2, rs1 >= rs2, signed
bltu, bgeuThe same, unsigned
jal rd, labelrd = pc + 4, jump to label
jalr rd, off(rs1)rd = pc + 4, pc = (rs1 + off) with bit 0 cleared

jalr reads rs1 before it writes rd, so jalr ra, 0(ra) works. auipc and jalr together reach any pc within 2 GB.

System

InstructionWhat it does
ecallTrap to the environment: mcause B from machine mode
ebreakTrap as a breakpoint: mcause 3
fence pred, succOrder memory: pred before succ (i, o, r, w); a no-op on aldan
fence.iMake stored code visible to fetch; a no-op on aldan
mretReturn from a machine trap
wfiWait for an interrupt

Pseudo-instructions

The assembler's shorthands, and what they stand for.

PseudoIs
nopaddi zero, zero, 0
li rd, immlui and addi, or a longer sequence for 64 bits
la rd, symbolauipc and addi
mv rd, rsaddi rd, rs, 0
not rd, rsxori rd, rs, -1
neg rd, rssub rd, zero, rs
negw rd, rssubw rd, zero, rs
sext.w rd, rsaddiw rd, rs, 0
zext.b rd, rsandi rd, rs, 255
seqz rd, rssltiu rd, rs, 1
snez rd, rssltu rd, zero, rs
sltz, sgtzslt rd, rs, zero; slt rd, zero, rs
beqz, bnez rs, labelbeq, bne against zero
blez, bgez, bltz, bgtzThe signed branches against zero
bgt, ble, bgtu, bleublt, bge, bltu, bgeu with the operands swapped
j labeljal zero, label
jal labeljal ra, label
jr rsjalr zero, 0(rs)
jalr rsjalr ra, 0(rs)
retjalr zero, 0(ra)
call symbolauipc ra and jalr ra: any distance within 2 GB
tail symbolauipc t1 and jalr zero: a jump that does not return here

M: multiply and divide

InstructionWhat it does
mul rd, rs1, rs2Low 64 bits of rs1 * rs2
mulhHigh 64 bits, signed * signed
mulhuHigh 64 bits, unsigned
mulhsuHigh 64 bits, signed rs1 * unsigned rs2
div, divuQuotient, signed, unsigned; rounds toward zero
rem, remuRemainder, signed, unsigned; takes the sign of rs1
mulw32-bit product, sign-extended
divw, divuw, remw, remuw32-bit divide and remainder, sign-extended

Nothing traps. Division by zero gives all ones for the quotient and rs1 for the remainder. The one signed overflow, the most negative number divided by -1, gives rs1 and remainder 0. For a full 128-bit product, take mulh then mul with the same operands.

F and D: floating point

F works on 32-bit singles (suffix .s), D on 64-bit doubles (.d); every instruction listed with .s has a .d twin. Operands are f registers unless named rd or rs.

Arithmetic

InstructionWhat it does
flw, fld rd, off(rs)Load a float
fsw, fsd rs2, off(rs1)Store a float
fadd.s, fsub.s, fmul.s, fdiv.srd = rs1 op rs2
fsqrt.s rd, rs1Square root
fmin.s, fmax.sThe smaller, the larger; a NaN loses to a number
fmadd.s rd, rs1, rs2, rs3rd = rs1 * rs2 + rs3, rounded once
fmsub.srd = rs1 * rs2 - rs3
fnmsub.srd = -(rs1 * rs2) + rs3
fnmadd.srd = -(rs1 * rs2) - rs3

Sign, compare, classify

InstructionWhat it does
fsgnj.s rd, rs1, rs2rs1 with the sign of rs2
fsgnjn.sWith the sign of rs2 inverted
fsgnjx.sWith the two signs xored
feq.s rd, rs1, rs2rd = 1 if equal, else 0
flt.s, fle.srd = 1 if less, less or equal
fclass.s rd, rs1One bit in rd for the class of rs1, below

fmv.s, fneg.s and fabs.s are fsgnj, fsgnjn and fsgnjx with rs1 = rs2. A comparison with a NaN gives 0; flt and fle also raise NV for it, feq only for a signalling NaN.

BitClass of fclassBitClass of fclass
0-inf5Positive subnormal
1Negative normal6Positive normal
2Negative subnormal7+inf
3-08Signalling NaN
4+09Quiet NaN

Conversion and moves

InstructionWhat it does
fcvt.w.s rd, rs1Float to signed 32-bit integer
fcvt.wu.s, fcvt.l.s, fcvt.lu.sTo unsigned 32, signed 64, unsigned 64
fcvt.s.w rd, rs1Signed 32-bit integer to float
fcvt.s.wu, fcvt.s.l, fcvt.s.luFrom unsigned 32, signed 64, unsigned 64
fcvt.s.d, fcvt.d.sDouble to single, single to double
fmv.x.w rd, rs1The bits of a single to rd, sign-extended
fmv.w.x rd, rs1The low 32 bits of rs1 to a single
fmv.x.d, fmv.d.xThe 64 bits of a double, each way

A conversion to an integer saturates: out of range or NaN gives the largest value of the type (the smallest for a negative number), and raises NV. A conversion or an arithmetic instruction may take a rounding mode as its last operand: fcvt.w.d a0, fa0, rtz truncates.

Rounding and flags

rmModeMeaning
0rneTo nearest, ties to even
1rtzToward zero
2rdnDown
3rupUp
4rmmTo nearest, ties away
7dynTake frm
BitFlagMeaning
4NVInvalid operation
3DZDivide by zero
2OFOverflow
1UFUnderflow
0NXInexact

fcsr (CSR 3) holds frm in bits 7-5 and fflags in bits 4-0; frm (2) and fflags (1) reach each part alone. The flags accrue: they stay set until written 0. Nothing traps on a float exception.

Zicsr: control and status registers

InstructionWhat it does
csrrw rd, csr, rs1rd = csr, csr = rs1
csrrs rd, csr, rs1rd = csr, set the bits of rs1
csrrc rd, csr, rs1rd = csr, clear the bits of rs1
csrrwi, csrrsi, csrrciThe same with a 5-bit unsigned immediate
PseudoIs
csrr rd, csrcsrrs rd, csr, zero
csrw csr, rscsrrw zero, csr, rs
csrs, csrc csr, rscsrrs, csrrc with rd = zero
csrwi, csrsi, csrciThe immediate forms, rd = zero
rdtime rdcsrr rd, time
rdinstret rdcsrr rd, instret
frcsr, fscsrRead, swap fcsr
frrm, fsrmRead, swap frm

csrrs and csrrc with rs1 = zero only read: they write nothing. csrrw with rd = zero does not read. The cycle counter, rdcycle, is not on aldan.

Zba: address generation

InstructionWhat it does
sh1add rd, rs1, rs2rd = (rs1 << 1) + rs2
sh2add, sh3addrd = (rs1 << 2) + rs2, (rs1 << 3) + rs2
add.uw rd, rs1, rs2rd = zext32(rs1) + rs2
sh1add.uw, sh2add.uw, sh3add.uwrd = (zext32(rs1) << n) + rs2
slli.uw rd, rs1, shamtrd = zext32(rs1) << shamt

sh3add a0, index, base is the address of base[index] for 64-bit elements. zext.w rd, rs is add.uw rd, rs, zero.

Zbb: basic bit manipulation

InstructionWhat it does
andn, orn rd, rs1, rs2rd = rs1 and, or the inverse of rs2
xnorrd = inverse of rs1 xor rs2
clz, ctz rd, rs1Count the leading, trailing zeros; 64 for 0
clzw, ctzwThe same on the low 32 bits
cpop, cpopwCount the set bits, of 64 and of 32
min, maxThe smaller, the larger, signed
minu, maxuThe same, unsigned
sext.b, sext.hSign-extend the low 8, 16 bits
zext.hZero-extend the low 16 bits
rol, ror rd, rs1, rs2Rotate left, right by rs2 and 63
roriRotate right by shamt; there is no roli
rolw, rorw, roriwRotate the low 32 bits, sign-extended
orc.b rd, rs1Each byte of rs1 becomes FF if it is not 0, else 00
rev8 rd, rs1Reverse the order of the bytes

orc.b finds a zero byte in a word at once, as a string search wants; rev8 turns a big-endian value around.

Zbs: single-bit operations

InstructionWhat it does
bset rd, rs1, rs2rs1 with bit rs2 and 63 set
bclrWith that bit cleared
binvWith that bit inverted
bext rd, rs1, rs2rd = bit rs2 and 63 of rs1
bseti, bclri, binvi, bextiThe same with a 6-bit index

Machine mode

Trap CSRs

CSRNumberWhat it holds
mstatus300MIE bit 3, MPIE 7, MPP 12-11
mie, mip304, 344Enabled, pending interrupts: MSIE/MSIP 3, MTIE/MTIP 7, MEIE/MEIP 11
mtvec305Trap base; mode in bits 1-0: 0 direct, 1 vectored
mscratch340Scratch for the handler
mepc341The pc of the trapped instruction
mcause342Bit 63 for an interrupt, the code below
mtval343The bad address or instruction

The rest of mstatus (FS, VS, the S and U bits) does nothing on aldan, nor does the rest of mie. The full list of what works is in the aldan codex.

Causes

CodeExceptionCodeInterrupt
0Instruction misaligned3Machine software
1Instruction access7Machine timer
2Illegal instructionBMachine external
3Breakpoint
4, 6Load, store misaligned
5, 7Load, store access
8, 9ecall from U, S
Becall from M
C, D, FPage faults

Aldan raises only 2, 3 and B, and the three machine interrupts.

Taking a trap

Timer and software interrupts

MTIP is high while mtime >= mtimecmp, two 64-bit registers of the clint; writing a later mtimecmp clears it. MSIP is bit 0 of the clint's msip register. The time CSR reads mtime.

Debug triggers (Sdtrig)

tselect (7A0) picks a trigger; tdata1 (7A1) sets it up, its type in bits 63-60; tdata2 (7A2) holds its address; tinfo (7A4) lists the types it can take. Aldan has 8 triggers of type 6, mcontrol6 (execute at an exact address), and 3, icount (step a count of instructions). A trigger that fires is a breakpoint: mcause 3. tcontrol (7A5) holds the bit that keeps triggers off inside a handler.

The PLIC

The platform-level interrupt controller gathers the lines of the devices and offers them to the harts as one external interrupt, MEIP.

OffsetRegister
0 + 4 * sourcePriority
1000Pending bits, 32 sources a word
2000 + 80 * contextEnable bits of the context
200000 + 1000 * contextThreshold of the context
200004 + 1000 * contextClaim on read, complete on write

All registers are 32 bits. Aldan has one context, 0 for hart 0 in machine mode, sources 1 to 5F and priorities 0 to 7.

V: vectors

The model

SEWm1m2m4m8Elements of VLEN 256
e83264128256
e16163264128
e328163264
e64481632

Configuration

InstructionWhat it does
vsetvli rd, rs1, e32, m4, ta, mavtype = e32, m4, ta, ma; vl = min(rs1, VLMAX); rd = vl
vsetivli rd, uimm, vtypeThe same with a 5-bit count
vsetvl rd, rs1, rs2The same with vtype in rs2

With rs1 = zero and rd not zero, vl = VLMAX. With both zero, vl stays and only vtype changes.

vtype bitsFieldValues
2-0vlmul0 m1, 1 m2, 2 m4, 3 m8; 5 mf8, 6 mf4, 7 mf2
5-3vsew0 e8, 1 e16, 2 e32, 3 e64
6vtaTail agnostic
7vmaMask agnostic
63villThe vtype asked for is not supported

A vtype aldan does not support (fractional LMUL, or SEW above 64) sets vill and vl = 0, and then every vector instruction but vset is illegal.

CSRNumberWhat it holds
vstart008The first element to work on
vxsat009Set by a saturating instruction that clamped
vxrm00AFixed-point rounding: 0 rnu (to nearest, up), 1 rne, 2 rdn (truncate), 3 rod (to odd)
vcsr00Fvxrm in bits 2-1, vxsat in bit 0
vlC20The element count, read only
vtypeC21The type, read only
vlenbC22VLEN in bytes, read only

A loop

The count left goes into vsetvli at each pass, and the pass takes as many elements as fit; there is no remainder loop. This adds 1 to a0 words from a1 into a2:

loop:
    vsetvli t0, a0, e32, m8, ta, ma
    vle32.v v8, (a1)
    vadd.vi v8, v8, 1
    vse32.v v8, (a2)
    sub     a0, a0, t0
    sh2add  a1, t0, a1
    sh2add  a2, t0, a2
    bnez    a0, loop

Operands

An instruction is named for its operands: .vv two vectors, .vx a vector and an integer register, .vi a vector and a 5-bit immediate, .vf a vector and a float register, .vs a vector and element 0 of another, .mm two masks, .vvm and the rest a mask in v0 as well. The second source comes first: vsub.vv vd, vs2, vs1 is vd = vs2 - vs1. The immediate is signed, but unsigned for shifts and slides.

Loads and stores

InstructionWhat it does
vle8.v, vle16.v, vle32.v, vle64.v vd, (rs1)Unit stride: vl elements from rs1 on
vse8.v ... vse64.v vs3, (rs1)Unit-stride store
vlse8.v ... vlse64.v vd, (rs1), rs2Strided: element i from rs1 + i * rs2
vsse8.v ... vsse64.vStrided store
vluxei8.v ... vluxei64.v vd, (rs1), vs2Indexed: element i from rs1 + vs2[i]; vloxei in order
vsuxei8.v ..., vsoxei8.v ...Indexed stores
vl1r.v vd, (rs1)One whole register, whatever vl
vs1r.v vs3, (rs1)Store one whole register

On aldan the element width of a load or store must equal SEW, and an index is SEW bits too. There are no mask loads (vlm.v), no fault-only-first, no segments and no vl2r.v to vl8r.v.

Integer arithmetic

InstructionWhat it does
vadd.vv, .vx, .vivd = vs2 + vs1
vsub.vv, .vxvd = vs2 - vs1
vrsub.vx, .vivd = x - vs2; vneg.v is vrsub.vx vd, vs, zero
vand, vor, vxor .vv, .vx, .viBitwise; vnot.v is vxor.vi vd, vs, -1
vsll, vsrl, vsra .vv, .vx, .viShifts by the low log2(SEW) bits
vmin, vminu, vmax, vmaxu .vv, .vxSmaller, larger, signed and unsigned
vmul .vv, .vxLow half of the product
vmulh, vmulhu, vmulhsu .vv, .vxHigh half: signed, unsigned, signed * unsigned
vmacc.vv, .vxvd = vd + vs1 * vs2
vnmsac.vv, .vxvd = vd - vs1 * vs2
vwmul, vwmulu, vwmulsu .vv, .vxWidening: a 2 * SEW product into a group of 2 * LMUL
vwadd, vwaddu .vv, .vxWidening sum; vwadd.vx vd, vs, zero sign-extends
vmerge.vvm, .vxm, .vimvd = vs1, x or imm where v0 is set, else vs2
vmv.v.v, .v.x, .v.iCopy or splat into vd

Compare

Each sets bit i of the mask vd to the result for element i.

InstructionWhat it does
vmseq, vmsne .vv, .vx, .viEqual, not equal
vmslt, vmsltu .vv, .vxLess, signed and unsigned
vmsle, vmsleu .vv, .vx, .viLess or equal
vmsgt, vmsgtu .vx, .viGreater; for .vv swap the operands of vmslt

Fixed point

InstructionWhat it does
vsadd, vsaddu .vv, .vx, .viAdd, clamped to the range
vssub, vssubu .vv, .vxSubtract, clamped
vaadd.vv, .vx(vs2 + vs1) / 2, rounded by vxrm, without overflow
vsmul.vv, .vxvs2 * vs1 >> (SEW - 1), rounded and clamped: a Q15 or Q31 product
vssra.vv, .vx, .viArithmetic shift right, rounded by vxrm

A clamp sets vxsat.

Slides and moves

InstructionWhat it does
vslideup.vx, .vi vd, vs2, nvd[i + n] = vs2[i]; vd[0] to vd[n - 1] keep their value
vslidedown.vx, .vivd[i] = vs2[i + n], 0 past VLMAX
vslide1up.vx vd, vs2, rs1vd[0] = rs1, vd[i + 1] = vs2[i]
vslide1down.vxvd[i] = vs2[i + 1], vd[vl - 1] = rs1
vmv.x.s rd, vs2rd = element 0, sign-extended
vmv.s.x vd, rs1Element 0 = rs1

Reductions and masks

InstructionWhat it does
vredsum.vs vd, vs2, vs1vd[0] = vs1[0] + the sum of vs2
vredmax, vredmaxu, vredmin, vredminuThe same with max or min, from vs1[0]
vmand, vmnand, vmandn, vmor, vmnor, vmorn, vmxor, vmxnor .mmMask logic, bit by bit: vs2 op vs1. vmnand, vmnor, vmxnor invert the result; vmandn, vmorn invert vs1
vcpop.m rd, vs2The count of set bits below vl
vfirst.m rd, vs2The index of the first set bit, or -1

vmmv.m is vmand.mm vd, vs, vs; vmnot.m is vmnand.mm vd, vs, vs; vmclr.m is vmxor.mm vd, vd, vd; vmset.m is vmxnor.mm vd, vd, vd.

Float

SEW 32 or 64 only, rounded by frm.

InstructionWhat it does
vfadd, vfsub, vfmul, vfdiv .vv, .vfvd = vs2 op vs1, or op rs1
vfmin, vfmax .vv, .vfSmaller, larger
vfsgnj, vfsgnjn .vv, .vfSign injection; vfneg.v is vfsgnjn.vv vd, vs, vs
vfmacc.vv, .vfvd = vd + vs1 * vs2
vfsqrt.v vd, vs2Square root
vfcvt.x.f.v, vfcvt.xu.f.vFloat to integer, by frm
vfcvt.rtz.x.f.v, vfcvt.rtz.xu.f.vFloat to integer, truncated
vfcvt.f.x.v, vfcvt.f.xu.vInteger to float
vfmv.v.f vd, rs1Splat a float
vfmv.f.s rd, vs2rd = element 0
vfmv.s.f vd, rs1Element 0 = rs1
vfredusum.vsvd[0] = vs1[0] + the sum of vs2

Missing on aldan

These decode as illegal instructions: vdiv, vdivu, vrem and vremu; vrgather, vcompress, vid.v and viota.m; vzext and vsext; every narrowing instruction (vnsrl, vnclip, vfncvt) and the widening ones but the vwmul and vwadd families; vmv1r.v to vmv8r.v; vredand, vredor, vredxor, vmsbf, vmsif and vmsof; vfrsub, vfrdiv, vfsgnjx, the float compares, vfclass, vfredosum, vfredmax, vfredmin, vfmsac and the other fused forms but vfmacc; vaaddu, vasub and vasubu; vmadd, vnmsub and the carry instructions vadc, vsbc, vmadc and vmsbc. Instead of vmv1r.v use vl1r.v and vs1r.v through memory, or vmv.v.v under an LMUL that covers the group.