How is SUB encoded differently from ADD?
They share opcode and funct3. SUB uses funct7=0100000, while ADD uses funct7=0000000.
Subtract rs2 from rs1, place result in rd
SUB uses opcode 0110011 (0x33), funct3 000, funct7 0100000. The rs1 and rs2 fields select the two source registers, and rd selects the destination register.
SUB (R-type) subtracts rs2 from rs1 (overflow ignored), writing to rd. funct7=0100000, funct3=000. Bit30=1 in funct7 distinguishes SUB from ADD (same funct3/opcode). There is no SUBI; use ADDI with a negated immediate instead.
SUB subtracts rs2 from rs1 and writes rd. It is register subtraction; the base integer ISA has no immediate subtract, so subtracting a small constant is normally ADDI with a negative immediate.
Understand this scenario with real code like «sub x5, x6, x7 # x5 = x6 - x7».
Understand this scenario with real code like «sub x5, x6, x7 # x5 = x6 - x7».
Understand this scenario with real code like «sub x5, x6, x7 # x5 = x6 - x7».
They share opcode and funct3. SUB uses funct7=0100000, while ADD uses funct7=0000000.
Use ADDI with a negative immediate, for example addi rd, rs1, -8 to subtract 8.