# Arithmetic Instructions

Computers are constantly performing calculations.

Adding numbers, subtracting values, increasing counters, and comparing numerical quantities are all common operations performed by a processor.

The CPU performs these operations using arithmetic instructions.

Arithmetic instructions are a type of data manipulation instruction because they operate on and produce data values.

What Are Arithmetic Instructions?

Arithmetic instructions are instructions that perform mathematical operations on data.

Common arithmetic operations include:

Addition

Subtraction

Multiplication

Division

Increment

Decrement

These operations are generally performed by the Arithmetic and Logic Unit (ALU).

A simplified view is:

Operands ↓ ALU ↓ Arithmetic Result

1.  Addition
    

The ADD instruction performs addition.

For example:

ADD R1, R2

Conceptually:

R1 + R2 → Result

Depending on the instruction set, the result may replace one operand or be placed in another destination register.

For example:

R1 = 10 R2 = 5

After an addition:

Result = 15

Addition is one of the most fundamental operations performed by a processor.

2.  Subtraction
    

The SUB instruction performs subtraction.

For example:

SUB R1, R2

Conceptually:

R1 − R2 → Result

If:

R1 = 10 R2 = 4

the result is:

6

Subtraction is also commonly used when calculating differences, counters, addresses, and other values.

3.  Multiplication
    

The MUL instruction performs multiplication when supported by the processor's instruction set.

For example:

MUL R1, R2

Conceptually:

R1 × R2 → Result

If:

R1 = 6 R2 = 4

the result is:

24

The hardware implementation of multiplication varies between processor architectures.

Some processors have dedicated multiplication hardware, while other designs can perform multiplication through sequences of simpler operations.

4.  Division
    

The DIV instruction performs division when supported.

For example:

DIV R1, R2

Conceptually:

R1 ÷ R2 → Result

Depending on the architecture, division can produce a quotient and possibly a remainder.

For example:

20 ÷ 6

produces:

Quotient = 3 Remainder = 2

The exact way these results are stored depends on the instruction set.

5.  Increment
    

The increment operation increases a value by one.

Conceptually:

R1 + 1 → R1

For example:

R1 = 7

After incrementing:

R1 = 8

An increment instruction is commonly useful for:

Counters

Loops

Addresses

Iterating through data

A processor may provide a dedicated instruction such as:

INC R1

6.  Decrement
    

The decrement operation decreases a value by one.

Conceptually:

R1 − 1 → R1

For example:

R1 = 7

After decrementing:

R1 = 6

A processor may provide an instruction such as:

DEC R1

Decrement operations are commonly used with:

Counters

Loops

Index values

Iterative algorithms

Arithmetic Instructions and the ALU

The Arithmetic and Logic Unit (ALU) is responsible for performing many arithmetic and logical operations inside the CPU.

A simplified operation looks like:

```plaintext
    R1 ─────┐
            │
            ▼
          ┌─────┐
    R2 ──►│ ALU │
          └──┬──┘
             │
             ▼
           Result
```

The instruction tells the Control Unit which operation should be performed.

For example:

ADD → perform addition

SUB → perform subtraction

The Control Unit coordinates the operation, while the ALU performs the actual arithmetic.

Arithmetic Instructions and Registers

Registers usually provide the operands needed by the ALU.

For example:

R1 = 15 R2 = 5

An addition operation may conceptually follow:

R1 ──┐ ├──► ALU ──► Result R2 ──┘

The result can then be stored in a register.

This is why registers and the ALU work closely together.

Arithmetic Instructions and Flags

Arithmetic operations can affect the processor's status flags.

For example, an arithmetic operation may produce:

Zero

Carry

Negative result

Overflow

Consider:

5 - 5 = 0

The result is zero, so a processor may set its Zero Flag.

These flags can later be used by program control instructions to make decisions.

For example:

Compare values ↓ Set flags ↓ Conditional branch

This creates an important connection between arithmetic instructions and program control instructions.

Integer Overflow

Because registers have a fixed number of bits, they cannot represent unlimited values.

For example, an unsigned 8-bit register can represent:

0 to 255

If we try to calculate:

255 + 1

using an 8-bit unsigned representation, the mathematical result is 256, which cannot be represented in 8 bits.

The result wraps according to the arithmetic rules of the system, and a carry may be produced.

Signed arithmetic also has its own overflow conditions.

Understanding overflow becomes especially important when working with low-level programming and assembly language.

Example of a Complete Arithmetic Sequence

Suppose:

R1 = 10 R2 = 5

A simplified sequence could be:

ADD R1, R2

Result:

15

Then:

INC R1

Result:

16

Then:

SUB R1, R2

Result:

11

The processor performs each operation according to the instruction set's rules.

Arithmetic vs Data Transfer

It is important to distinguish arithmetic instructions from data transfer instructions.

Data Transfer

Moves data:

R1 → R2

Arithmetic

Processes data:

R1 + R2

A program often needs both.

For example:

LOAD R1, value ADD R1, R2 STORE R1, result

Here:

LOAD → transfers data

ADD → manipulates data

STORE → transfers data

Common Arithmetic Instructions

Instruction

Operation

ADD

Addition

SUB

Subtraction

MUL

Multiplication

DIV

Division

INC

Increment by 1

DEC

Decrement by 1

The exact instruction names, operands, and behavior depend on the processor architecture.

Why Arithmetic Instructions Matter

Almost every type of software eventually depends on arithmetic operations.

Arithmetic instructions are used in:

Calculations

Counters

Loops

Address calculations

Algorithms

Graphics

Scientific computing

Operating systems

Embedded systems

At the hardware level, these operations are reduced to operations that the processor's digital circuits can perform.

Final Thoughts

Arithmetic instructions allow the CPU to manipulate numerical data.

The most common operations are:

ADD → Addition

SUB → Subtraction

MUL → Multiplication

DIV → Division

INC → Increment

DEC → Decrement

The ALU performs the arithmetic operation, while registers provide the operands and store results.

Arithmetic operations can also affect status flags, which can later influence program control.

We've now learned how the processor moves data and how it performs arithmetic on data.

But numbers aren't the only thing a CPU needs to manipulate.

The processor also needs to work directly with individual bits.

Next: Logical Instructions.
