Adding registers
Just as we can add the value stored in a register to an immediate value (a constant) and then save the result in another register, we can also add the values of two registers together - then save that result in a third register.
Adding two registers
Section titled “Adding two registers”The instruction for adding the values of two registers together is add (not
addi, which we used for adding an immediate value).
The syntax for add is:
add <destination register>, <source register 1>, <source register 2>This means “add the value in <source register 1> to the value in <source register 2>, and store the result in <destination register>”.
Example
Section titled “Example”If we wanted to:
- Load the value
22into registerx1 - Load the value
8into registerx2 - Add the values in
x1andx2together, and store the result inx3
We would write:
.textmain: li x1, 22 # x1 = 22 li x2, 8 # x2 = 8 add x3, x1, x2 # x3 = x1 + x2
stop: li a7, 10 # 10 = exit syscall ecall # Make syscallNow, when we run this code, the value in register x3 will be 30, or 1E in
hex!
The += operation
Section titled “The += operation”In many programming languages, there is a shorthand way to add a value to a
variable and store the result back in the same variable. This is often done
using the += operator.
For example, in C:
int x1 = 10;int x2 = 5;x1 += x2; // equivalent to x1 = x1 + x2;We can effectively do the same in RISC-V assembly using the add instruction by
using the same register for both the destination and one of the source
registers.
For this C code, we can instead write:
li x1, 10 # x1 = 10 li x2, 5 # x2 = 5 add x1, x1, x2 # x1 = x1 + x2This will add the value in x2 to the value in x1, and store the result back
in x1. After , x1 will contain the value 15.