Home/Instructions/Remainder
REM

RISC-V REM Instruction Details

Instruction ManualR-type

Signed remainder of rs1 / rs2, stored in rd

Instruction Syntax

rem rd, rs1, rs2
Operand Breakdown
rd: receives the result; source/destination overlap uses old source values, and rd=x0 discards writeback.
rs1 is the dividend and rs2 the divisor; compute the remainder using signed XLEN-bit values.
Requires RV32/RV64 M; a zero divisor returns the dividend without an integer divide-by-zero arithmetic exception.
MArithmeticDivision

Instruction Behavior

REM provides the XLEN-bit signed remainder of rs1 divided by rs2. A nonzero remainder has the dividend sign; a zero divisor returns the dividend; only most-negative divided by -1 overflows and returns zero. Except for overflow, dividend = divisor x quotient + remainder.

REM Decode And Execute Animation

Starts with machine-code field decoding, then shows operand reads, instruction-specific execution, and ISA-visible state update.

rd
rs1
rs2
rem
,
,
Execution Context

This example uses fixed initial register values and PC=0x2000; instruction fetch succeeds, with no asynchronous interrupt or extra extension checks. Exception branches stop ordinary commit; post-trap PC and handler state are EEI-dependent.

t1(x6)
0x00000014
t2(x7)
0x00000007

This selector chooses specification-defined division-result branches. Division presets set the displayed t1/t2 initial values; editing a source register restores normal mode. It is not a trap or performance model.

31..25
24..20
19..15
14..12
11..7
6..0
0000001
funct7
00111
rs2
00110
rs1
110
funct3
00101
rd
0110011
opcode
Execution Data Path
instruction
0x027362B3
opcode
0110011 -> OP
funct3
110 -> REM
funct7
0000001 -> M-extension divide/remainder selector
rd / dividend / divisor
t0(x5) / t1(x6)=20 / t2(x7)=7
signed 32-bit remainder
Not computed
x5
t0(x5) = 0x00003001; pc = 0x00002000
Current Step

Concept Step: receive the 32-bit instruction encoding

The machine code is split by the current instruction format; the animation starts from encoding/decode.

encoding: 0x027362B3
syntax : rem t0(x5), t1(x6), t2(x7)
Pending result: Not computed
Current architectural state
t0(x5) = 0x00003001; pc = 0x00002000

This animation shows ISA-visible decode and state changes, not any specific CPU pipeline, cache, prediction, or timing implementation.

Quick Understanding & Search Notes

REM is the M-extension signed XLEN-bit remainder operation: rs1 is the dividend and rs2 is the divisor; the normal remainder is written to rd, and a nonzero remainder has the dividend sign.

The fixed R-type encoding is opcode=0110011, funct3=110, and funct7=0000001; REM belongs to the M extension.
A zero divisor returns the dividend as the remainder; signed overflow occurs only for most-negative divided by -1, where the remainder is zero.

Common Usage Scenarios

Integer remainder

RV32/RV64 plus M, x6=20 and x7=7: rem x5, x6, x7 gives x5=6; the quotient rounds toward zero and 20=7×2+6.

Pre-Use Checklist

Syntax Check
  • rd, rs1 and rs2 select x0–x31 or ABI aliases, with no immediate or rs3.
Semantic Check
  • REM is the M-extension signed XLEN-bit remainder operation: rs1 is the dividend and rs2 is the divisor; the normal remainder is written to rd, and a nonzero remainder has the dividend sign.
  • Destination/source overlap uses old source values; x0 is always zero and writes to it are discarded.

Pitfalls / Common Confusions

A zero divisor raises no integer divide-by-zero arithmetic exception; the remainder equals the dividend.
A nonzero remainder has the dividend sign; most-negative divided by -1 returns a zero remainder.

FAQ

Does REM raise an arithmetic exception on division by zero?

No. The M extension specifies that REM writes the dividend to rd as the remainder when the divisor is zero.