Skip to main content
Be Bitwise

Memory-Mapped I/O: LED Toggle

Write to a 'device register' address to turn an LED on and off. In embedded systems, controlling hardware is just writing to special memory addresses.

In memory-mapped I/O, hardware peripherals (LEDs, sensors, communication ports) are controlled by reading and writing to specific memory addresses. The CPU uses the same LOAD and STORE instructions it uses for RAM, but the memory controller routes accesses to certain address ranges to peripheral hardware instead. Writing 1 to an LED control register turns the LED on; writing 0 turns it off. This unified addressing is how most embedded systems and even modern PCs interact with hardware.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Set up the device register address

    Do this: Step through MOV R0, #240

    Watch for: R0 = 240 (0xF0, the LED control register address)

    Address 0xF0 is designated as the LED's control register. In a real microcontroller, this would be a specific address defined in the hardware datasheet (e.g. 0x40020000 for a GPIO port on an STM32).

  2. 2

    Turn the LED on

    Do this: Step through MOV R2, #1 / STORE [R0], R2 / LOAD R3, [R0]

    Watch for: Address 240 = 1 (LED on). R3 = 1 when the register is read back

    Writing 1 to the control register turns the LED on. In real hardware, this write would trigger an electrical signal on a GPIO pin. Many output registers can be read back, so the LOAD confirms the LED's current state.

  3. 3

    Turn the LED off

    Do this: Step through MOV R2, #0 / STORE [R0], R2 / LOAD R4, [R0]

    Watch for: Address 240 = 0 (LED off). R4 = 0

    Writing 0 turns the LED off. The peripheral responds immediately to the memory write. This is why memory-mapped I/O is so efficient — no special I/O instructions needed.

  4. 4

    Toggle the LED on again

    Do this: Step through the final MOV / STORE / LOAD / HALT

    Watch for: Address 240 = 1 again (LED on). R5 = 1

    The LED toggles back on. In embedded firmware, this pattern of writing to control registers and reading them back is the fundamental way all hardware interaction works — from blinking LEDs to sending network packets.

Objectives

  • •See address 240 (control register) receive value 1 - LED turns ON
  • •Watch address 240 change to 0 - LED turns OFF
  • •See address 240 change back to 1 - LED turns ON again
  • •Understand that in real hardware, these memory writes trigger physical actions

Hints

  • In our simulator, address 240 is just regular memory. On real hardware, it would be wired to a peripheral controller that drives physical pins.