Skip to content

Division

Division in RISC-V is actually… surprisingly simple!

We just use the div instruction, which performs signed integer division.

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, x2

For example, our full code may look something like:

.text
main:
li x1, -20
li x2, 4
div x3, x1, x2
stop:
li a7, 10
ecall

Run on creatorsim

The result in the registers will be:

x3 = -5

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:

.text
main:
li x1, 20
li x2, 3
divu x3, x1, x2
stop:
li a7, 10
ecall

Run on creatorsim

Result:

x3 = 6