Division
Division in RISC-V is actually… surprisingly simple!
We just use the div instruction, which performs signed integer division.
Syntax
Section titled “Syntax”If we want to divide the value in register x1 by the value in register x2,
and store the result in register x3, we would write:
div x3, x1, x2For example, our full code may look something like:
.textmain: li x1, -20 li x2, 4 div x3, x1, x2
stop: li a7, 10 ecallThe result in the registers will be:
x3 = -5Unsigned division
Section titled “Unsigned division”We can do unsigned division using the divu instruction.
Everything else works exactly the same as div, except that the values in the
source registers are treated as unsigned integers.
For example:
.textmain: li x1, 20 li x2, 3 divu x3, x1, x2
stop: li a7, 10 ecallResult:
x3 = 6