## Arithmetic, Logical and Branch Instructions

### Arithmetic and Logical Instructions

- Instructions operate only on registers
- If you want to add the values stored in two memory locations, you must
- Load each value in memory into a register
- Store the result in a memory

- Binary operations take two operands
- The result is stored in the destination (
**Rd**) register

Some popular arithmetic instructions:

*ADD*Rd, Rr- Rd ← Rd + Rr

*SUB*Rd, Rr- Rd ← Rd - Rr

*ADC*Rd, Rr- Rd ← Rd + Rr + C where
**C**is the contents of the carry flag

- Rd ← Rd + Rr + C where
*SBC*Rd, Rr- Rd ← Rd - Rr - C

Example of adding two 16-bit numbers:

- Suppose one number is stored in
**R1:R0**and the other in**R3:R2** - We would add these as follows:

ADD R0, R2 ADC R1, R3

- The result is stored in
**R1:R0**

## Branch Instructions

- These instructions modify the program counter.
- Jump instructions are sometimes lumped in with the branch instructions since they modify the program counter as well.
*RJMP*and all of the branch instructions use relative addressing- Relative addressing is faster than direct addressing, but it limits how far you can jump.
- The assembler will warn you if you are trying to jump too far (not very common)
*JMP*does not use relative addressing and can- If you want to add the values stored in two memory locations, you must
- Load each value in memory into a register
- Store the result in a memory

- Binary operations take two operands
- The result is stored in the destination (
**Rd**) register

Some popular arithmetic instructions:

*RJMP*k- PC ← PC + k + 1
- where -2K ⇐ k < 2K

*JMP*k- PC ← k
- where 0 ⇐ k < 4M

*BRNE*k- if(Z==0) PC ← PC + k + 1
- where -64 ⇐ k < 64

*BREQ*k- if(Z==1) PC ← PC + k + 1
- where -64 ⇐ k < 64