# 2. ATmega328P Hardware & Memory Architecture

### A. Memory Map

The ATmega328P memory map provides information on how the Microcontroller Unit (MCU) uses memory. Here is the address division:

| Category | Address | Size | Description |
| :--- | :--- | :--- | :--- |
| **General Purpose Registers** | `0x0000` - `0x001F` | 32 x 8 bit | Registers R0 - R31 |
| **I/O Registers** | `0x0020` - `0x005F` | 64 x 8 bit | Accessible via `IN`/`OUT` instructions |
| **Extended I/O Registers** | `0x0060` - `0x00FF` | 160 x 8 bit | Additional I/O registers |
| **Internal SRAM** | `0x0100` - `0x08FF` | 2048 x 8 bit | Internal data memory |

### B. General Purpose Working Registers (GPR)

The AVR architecture has **32 general-purpose registers** labeled **R0** through **R31**. These registers function as temporary storage for data during processing and are directly connected to the ALU (Arithmetic Logic Unit).

#### Register Division:

| Group | Registers | Characteristics |
| :--- | :--- | :--- |
| **Lower Registers** | R0 - R15 | Limited functionality. **Cannot** store immediate values directly (cannot use `LDI` instruction). |
| **Upper Registers** | R16 - R31 | More flexible. **Can** work with immediate data, allowing direct storage of bytes or words. |

#### Pointer Registers:

The last six registers (R26 through R31) can be combined into 16-bit pointers for indirect memory addressing:

| Pointer Name | Low Register | High Register | Function |
| :--- | :--- | :--- | :--- |
| **X Register** | R26 (XL) | R27 (XH) | Pointer for memory access |
| **Y Register** | R28 (YL) | R29 (YH) | Pointer for memory access |
| **Z Register** | R30 (ZL) | R31 (ZH) | Pointer for memory & flash access |