Cortex-M
Inside a Micro-controller

A chip to be considered a cpu, should have at least a 1 control unit (cu) , 1 arithmetic logic unit (alu) and some registers,
- CU: is the brain of the cpu it controls every aspect of the cpu
- ALU: the component that is responsible for doing bit wise arithmetic in binary
The alu has register bank (aka a bunch of registers), the cortex-M has 16 registers and 5 special registers.
The 16 register are divided into 13 (r0 to r12) general purpose registers, a stack pointer (sp) which can be ‘the main sp’ or ‘program sp’ depending on the mode of execution. The stack pointer register is a specialized CPU register used to keep track of the top of the stack

💡 By default the any firmware you write starts executing in Handler mode (the privileged mode) that uses the msp.
The bit 1 of the Control register indicates if the msp or psp is in use.
Link Register (LR) contains the return address after a function is done
Program counter (PC), also known as the instruction pointer or instruction address register, is a special register within a computer's central processing unit (CPU) that stores the memory address of the next instruction to be executed.

The stack

Thumb Instruction Set
Thumb is a compressed instruction set architecture used in ARM processors that provides:
- 16-bit instruction encoding (compared to ARM's 32-bit instructions)
- Higher code density (typically 30-40% smaller code size)
- Reduced memory bandwidth requirements
Key Characteristics:
- Originally had reduced functionality compared to full ARM instruction set
- Many Thumb instructions map to equivalent ARM instructions
- Processors switch between Thumb and ARM states when needed
- Provides better performance in systems with 16-bit memory
Evolution:
- Original Thumb: Basic 16-bit instruction set
- Thumb-2: Extended in ARMv7 with mixed 16/32-bit instructions
- Thumb only: In Cortex-M processors (like STM32), which only implement Thumb instructions
Benefits:
- Smaller program size
- Better performance on 16-bit memory systems
- Reduced power consumption
- Maintained in modern ARM architectures for backward compatibility

- Thread Mode: The processor operates in this mode while running regular application code, such as the main program and user tasks. It can run in either privileged or unprivileged state, depending on system configuration.
- Handler Mode: The processor switches to this mode when servicing exceptions, interrupts, or system calls (like SVC). In Handler Mode, the CPU always runs in privileged state, allowing access to all system resources and registers.