Here’s an explanation of the RISC-V commands you've mentioned, grouped by type, with an
example for each.
Arithmetic Instructions
1. ADD: Adds two registers and stores the result in a register.
o Syntax: ADD rd, rs1, rs2
Example:
o ADD x5, x1, x2 # x5 = x1 + x2
2. ADDI: Adds a register and an immediate value.
o Syntax: ADDI rd, rs1, imm
Example:
o ADDI x5, x1, 10 # x5 = x1 + 10
3. SUB: Subtracts the second register from the first.
o Syntax: SUB rd, rs1, rs2
Example:
o SUB x5, x1, x2 # x5 = x1 - x2
Logical Instructions
1. AND: Performs a bitwise AND operation between two registers.
o Syntax: AND rd, rs1, rs2
Example:
o AND x5, x1, x2 # x5 = x1 & x2
2. ANDI: Performs a bitwise AND operation between a register and an immediate.
o Syntax: ANDI rd, rs1, imm
Example:
o ANDI x5, x1, 0x0F # x5 = x1 & 0x0F
3. OR: Performs a bitwise OR operation between two registers.
o Syntax: OR rd, rs1, rs2
Example:
o OR x5, x1, x2 # x5 = x1 | x2
4. ORI: Performs a bitwise OR operation between a register and an immediate.
o Syntax: ORI rd, rs1, imm
Example:
o ORI x5, x1, 0xF0 # x5 = x1 | 0xF0
5. XOR: Performs a bitwise XOR operation between two registers.
o Syntax: XOR rd, rs1, rs2
Example:
o XOR x5, x1, x2 # x5 = x1 ^ x2
6. XORI: Performs a bitwise XOR operation between a register and an immediate.
o Syntax: XORI rd, rs1, imm
Example:
o XORI x5, x1, 0x0F # x5 = x1 ^ 0x0F
Set Instructions
1. SLT (Set Less Than): Sets rd to 1 if rs1 < rs2 (signed comparison).
o Syntax: SLT rd, rs1, rs2
Example:
o SLT x5, x1, x2 # x5 = 1 if x1 < x2; otherwise, x5 = 0
2. SLTI (Set Less Than Immediate): Sets rd to 1 if rs1 < imm (signed comparison).
o Syntax: SLTI rd, rs1, imm
Example:
o SLTI x5, x1, 10 # x5 = 1 if x1 < 10; otherwise, x5 = 0
3. SLTU (Set Less Than Unsigned): Similar to SLT but for unsigned values.
o Syntax: SLTU rd, rs1, rs2
Example:
o SLTU x5, x1, x2 # x5 = 1 if x1 < x2 (unsigned); otherwise, x5
= 0
4. SLTIU (Set Less Than Immediate Unsigned): Similar to SLTI but for unsigned
values.
o Syntax: SLTIU rd, rs1, imm
Example:
o SLTIU x5, x1, 10 # x5 = 1 if x1 < 10 (unsigned); otherwise, x5
= 0
Shift Instructions
1. SRA (Shift Right Arithmetic): Shifts bits of rs1 right by rs2 places, preserving the
sign.
o Syntax: SRA rd, rs1, rs2
Example:
o SRA x5, x1, x2 # x5 = x1 >> x2 (arithmetic shift)
2. SRAI (Shift Right Arithmetic Immediate): Shifts bits of rs1 right by an immediate
value, preserving the sign.
o Syntax: SRAI rd, rs1, imm
Example:
o SRAI x5, x1, 2 # x5 = x1 >> 2 (arithmetic shift)
3. SRL (Shift Right Logical): Shifts bits of rs1 right by rs2 places, filling with zeros.
o Syntax: SRL rd, rs1, rs2
Example:
o SRL x5, x1, x2 # x5 = x1 >> x2 (logical shift)
4. SRLI (Shift Right Logical Immediate): Shifts bits of rs1 right by an immediate
value, filling with zeros.
o Syntax: SRLI rd, rs1, imm
Example:
o SRLI x5, x1, 2 # x5 = x1 >> 2 (logical shift)
5. SLL (Shift Left Logical): Shifts bits of rs1 left by rs2 places.
o Syntax: SLL rd, rs1, rs2
Example:
o SLL x5, x1, x2 # x5 = x1 << x2
Memory Instructions
1. LW (Load Word): Loads a 32-bit word from memory.
o Syntax: LW rd, offset(rs1)
Example:
o LW x5, 4(x1) # Load word from memory address x1 + 4 into x5
2. SW (Store Word): Stores a 32-bit word in memory.
o Syntax: SW rs2, offset(rs1)
Example:
o SW x5, 8(x1) # Store word from x5 into memory at address x1 +
8
3. LB (Load Byte): Loads a single byte from memory, sign-extended.
o Syntax: LB rd, offset(rs1)
Example:
o LB x5, 1(x1) # Load byte from memory address x1 + 1 into x5
4. SB (Store Byte): Stores a single byte in memory.
o Syntax: SB rs2, offset(rs1)
Example:
o SB x5, 1(x1) # Store byte from x5 into memory at address x1 +
1
PC Instructions
1. LUI (Load Upper Immediate): Loads the upper 20 bits of a register.
o Syntax: LUI rd, imm
Example:
o LUI x5, 0x12345 # x5 = 0x12345000
2. AUIPC (Add Upper Immediate to PC): Adds an immediate value to the upper 20
bits of the PC.
o Syntax: AUIPC rd, imm
Example:
o AUIPC x5, 0x10 # x5 = PC + 0x10000
Jump Instructions
1. JAL (Jump and Link): Jumps to a target address and saves the return address in rd.
o Syntax: JAL rd, offset
Example:
o JAL x5, 16 # Jump to PC + 16 and store return address in x5
2. JALR (Jump and Link Register): Jumps to an address in a register and saves the
return address.
o Syntax: JALR rd, offset(rs1)
Example:
o JALR x5, 0(x1) # Jump to address in x1 and store return
address in x5
Branch Instructions
1. BEQ (Branch if Equal): Branches if rs1 == rs2.
o Syntax: BEQ rs1, rs2, offset
Example:
o BEQ x1, x2, 8 # Branch to PC + 8 if x1 == x2
2. BNE (Branch if Not Equal): Branches if rs1 != rs2.
o Syntax: BNE rs1, rs2, offset
Example:
o BNE x1, x2, 8 # Branch to PC + 8 if x1 != x2
3. BLT (Branch if Less Than): Branches if rs1 < rs2 (signed).
o Syntax: BLT rs1, rs2, offset
Example:
o BLT x1, x2, 8 # Branch to PC + 8 if x1 < x2
4. BGE (Branch if Greater or Equal): Branches if rs1 >= rs2 (signed).
o Syntax: BGE rs1, rs2, offset
Example:
o BGE x1, x2, 8 # Branch to PC + 8 if x1 >= x2
5. BLTU (Branch if Less Than Unsigned): Similar to BLT but for unsigned values.
o Syntax: BLTU rs1, rs2, offset
Example:
o BLTU x1, x2, 8 # Branch to PC + 8 if x1 < x2 (unsigned)
6. BGEU (Branch if Greater or Equal Unsigned): Similar to BGE but for unsigned
values.
o Syntax: BGEU rs1, rs2, offset
Example:
o BGEU x1, x2, 8 # Branch to PC + 8 if x1 >= x2 (unsigned)