0x0000-7FFFCPU0x0000regs0x0020I/O0x0060ext0x0100SRAMb70b60b50b40b30b20b10b00b70b60b50b40b30b20b10b001// Uno: LED pin 13 = PB52auto ddrb = (volatile uint8_t*)0x24;3auto portb = (volatile uint8_t*)0x25;4*ddrb |= 1 << 5; // output5*portb |= 1 << 5; // HIGH010x08000000flash0x20000000SRAM0x40020C00GPIOD0x2570x20PB5
Back: flash, the CPU and the data map's four address regionsMiddle: the port B registers DDRB and PORTB, wired to the LED on pin 13Tokens: the program, a variable, the address 0x25 and the mask 0x20An Arduino Uno R3 drawn as its memory maps: 32 KB of flash for the program, and a data map whose addresses lead to CPU registers, I/O registers and 2 KB of SRAM. Writing bit 5 of DDRB (0x24) and PORTB (0x25) through a pointer makes pin 13 an output and lights its LED: what pinMode() and digitalWrite() do underneath. The STM32F4 keeps flash, SRAM and peripherals in one map instead.

The map

  1. A processor finds everything by number. Each byte it can reach has an address, like a house number on a street. A memory map is the chart of which addresses lead where: to the program, to variables, or to hardware.

Pin 13 by hand

Libraries and other chips

What happens

;; step 1/10 · addresses
address     a number that picks one byte
memory map  which addresses lead where
ATmega328P data map, 0x0000 to 0x08FF:
  0x0000 to 0x001F  32 working registers
  0x0020 to 0x005F  64 I/O registers
  0x0060 to 0x00FF  160 extended I/O registers
  0x0100 to 0x08FF  2,048 bytes of SRAM

The card shows the body of setup(). The real names DDRB and PORTB from <avr/io.h> expand to exactly this kind of volatile pointer access: avr-libc adds 0x20 to the I/O address, so PORTB at I/O 0x05 is data address 0x25. The AVR keeps program and data in separate maps, so address 0x0100 means one thing in flash and another in SRAM. Reading a constant table from flash needs special instructions (LPM, wrapped by pgm_read_byte), which is why Arduino marks its pin tables PROGMEM.