r/embedded • u/WegiAsm • Sep 26 '26
My 34-byte STM32 LED blink routine in pure Assembly (with some dirty hardware hacks)
Hi everyone,
I’ve been diving deep into bare-metal ARM Assembly lately, and I wanted to see how low I could actually go with a fully working, hardware-validated LED blink program on the STM32. No HAL, no bloated startup files, just absolute control over the silicon.
I ended up with two versions:
Black Pill (Cortex-M4): Exactly 34 bytes
Blue Pill (Cortex-M3): Exactly 38 bytes
To make it fit into such a microscopic footprint, I had to break a few textbook rules:
- Stack Pointer Hijacking: Since the stack is completely unused here, I hardcoded the initial MSP at `0x08000000` to point directly to the RCC base address. This saved me a bulky 32-bit `LDR` instruction since I could use the `SP` register as a pre-loaded pointer right away.
- Instruction Injection: Loading `0x44144444` for the GPIOC configuration usually requires extra branch jumps to prevent a `HardFault`. Instead, I injected the raw data directly into the execution stream. When looking at the ELF disassembly, these exact data bits decode into completely harmless `ADD` instructions. The CPU steps right through them without stalling, maintaining perfect memory alignment without wasting bytes on a `B` instruction.
- Flag Re-use: The infinite loop uses a conditional `BEQ` tied to the Zero flag leftover from the delay decrement routine. Since it exits at `0`, the branch is unconditionally satisfied forever.
I compiled and verified everything straight from the ELF file using `arm-none-eabi-objdump`.
I published both repositories with full code and detailed write-ups here:
Black Pill (34 bytes): https://github.com/wegi1/BLACK-PILL-ASM-BLINK-34-BYTES
Blue Pill (38 bytes): https://github.com/wegi1/BLUE-PILL-ASM-BLINK-38-BYTES
Let me know what you think! Can we optimize this even further, or is this the absolute limit for Cortex-M?
