In this chapter we will see some instructions that will allow us to compute things.
Since a computer is nothing but a glorified calculator (or a calculator it is nothing but a tiny computer), it has to be possible to perform basic arithmetic. For now we will restrict to integer operations. In later chapters we will see how we can manipulate other kinds of numbers.
Addition and subtraction
We can perform addition and subtraction using
sub instructions. These instructions are pretty flexible in that they allow using many forms.
In the forms above
Rx means either
Wx (but without mixing them in the same instruction),
extop are the shift and extension operands described in chapter 3. In this case,
shiftop does not include
ROR. All the forms shown for
add can be used for
sub as well.
Multiplication and division
Compared to addition and subtraction, multiplication and division are harder operations. And there are a few different instructions for this purpose.
Due to the nature of multiplication, multiplying two values of 32/64 bits may end requiring 64/128 bits to be able to fully encode the mathematical result. If we know this will not happen (i.e. the result of the value can be encoded in 32/64 bits) or we do not care we can use the
mul instruction. If there are excess bits, they will be dropped.
If we do care about the excess bits, then we have a bunch of instructions we can use. For 32-bit multiplications,
smull can be used. The latter is required when multiplying numbers in two's complement so the sign bit is correctly handled.
For the less common case where we multiply two 64-bit registers and still we care about the upper 64-bits of the 128-bit result, then we can use
smulh. For this case we will need two 64-bit registers (named
Xupper in the example below).
Division is a bit simpler as only two instructions are necessary:
sdiv. Again, the latter is for integer numbers encoded in two's complement.
These two instructions will compute the quotient of the division rounded towards zero.
Bitwise instructions directly operate in the bits of the registers, without assuming any encoding in them.
mvn performs a bitwise not on its operand.
Similar forms as the shown above exist for
eor. For bitwise instructions
shiftop does include
There are combined versions of
bic (bit clear),
orn (or not) and
eon (exclusive or not) respectively. In this case, the second operand is first applied a NOT operation. They have slightly more limited forms.
There are more instructions with narrower use cases, so we will omit them for now.
This is all for today.