====== Registers ======
Registers are a key element of AVR microcontrollers. There are various types of registers, including general-purpose, special-purpose, and status registers. General-purpose registers are used to store temporary data. Special registers control microcontroller functions, such as timers or ADC. Status registers store information about the processor state, such as carry or zero flags. Each register has a specific function and is accessible through particular assembler instructions. Registers allow quick access to data and control over the processor.
AVR CPU General Purpose Working Registers (R0-R31)
**R0-R15:**
Basic general-purpose registers are used for various arithmetic and logical operations.
**R16-R31:**
General-purpose registers that can be used with immediate
Comparison Table: AVR Registers R0–R15 vs. R16–R31
^ Feature ^ R0–R15 ^ R16–R31 ^
| Instruction compatibility | Cannot be used with immediate instructions (LDI, CPI, ORI, ANDI) | Fully compatible with immediate instructions |
| Opcode encoding | Not encodable in 4‑bit register fields | Encodable in all immediate‑type formats |
| Typical compiler usage | Less preferred; used for temporary or special roles | Preferred for arithmetic and logic operations |
| Special-purpose roles | R0: scratch
R1: constant zero | R26–R31 used as X/Y/Z pointer registers |
| Pointer support | No | Yes: X, Y, Z pointers |
| Efficiency | Less efficient for immediate operations | More efficient due to shorter opcodes |
| Multiplication involvement | R0/R1 used implicitly for results | Not used by multipliers |
| Suitability in ASM | Limited; avoid with immediates; R0/R1 have special rules | Recommended for general ALU operations |
| GCC policy | R1 must remain 0; R0 is volatile | Treated as normal general-purpose registers |