How is SUB encoded differently from ADD?
Both use opcode=0110011 and funct3=000; SUB has funct7=0100000 and ADD has 0000000.
Subtract rs2 from rs1, place result in rd
SUB uses a 32-bit R-type encoding (opcode=0110011, funct3=000, funct7=0100000). It computes rs1-rs2 and writes the low XLEN bits to rd, without an arithmetic-overflow exception. XLEN is 32 in RV32I and 64 in RV64I. Writes to x0 are discarded. The base integer ISA has no SUBI; ADDI can subtract a constant only if its negation is encodable from -2048 through 2047.
RV32I/RV64I; XLEN-bit subtraction
sub rd, rs1, rs2Assume successful instruction fetch, no asynchronous interrupt or extra extension checks; one initial value per register.
0x40C58533Decode the current instruction fields and read sources before writeback; no result has been committed.
These are teaching steps for architectural effects, not processor pipeline stages or cycles.
Subtract rs2 from rs1, place result in rd
RV32I/RV64I, x6=20 and x7=7: sub x5, x6, x7 gives x5=13, retaining only low XLEN bits.
RV32I/RV64I, x7=7: sub x5, x0, x7 writes XLEN-bit -7 to x5, equivalent to neg x5, x7.
Both use opcode=0110011 and funct3=000; SUB has funct7=0100000 and ADD has 0000000.
Use addi rd, rs1, -8. ADDI requires a signed 12-bit immediate from -2048 through 2047; one ADDI cannot encode every constant subtraction.