Bare-Metal Cortex-M in Assembly

The vector table, a reset handler and memory-mapped GPIO — what runs before (or instead of) C on a microcontroller.

On a microcontroller there is no operating system: after reset the CPU reads the vector table and jumps to the reset handler. Startup code is often written in assembly, and seeing a complete program at this level shows exactly what the Embedded C course's startup and GPIO code compile down to.

Blinking an LED in assembly

The .isr_vector section starts with the initial stack pointer and the address of reset_handler.

ldr r0, =RCC_AHB1ENR loads a 32-bit address from a literal pool; ldr/orr/str is a read-modify-write of the register.

bic then orr writes the 2-bit MODER field for pin 5.

eor toggles bit 5 of the output register; a down-counting loop provides a crude delay.

Interrupts on Cortex-M

Forgetting to enable the peripheral clock Set the peripheral's bit in RCC first

Exactly as in C: writes to a peripheral whose clock is off are ignored.

What are the first two words of a Cortex-M vector table?

The initial main stack pointer value, then the address of the reset handler.