Cortex-M AssemblyA two-day crash course on a simulated ARM microcontroller All courses
0 of 19 exercises
Simulator starting…

A two-day crash course

Cortex-M Assembly

What the instruments' firmware, and every compiled program, is really made of. You start knowing nothing about assembly. By Sunday evening you can read compiler output for an ARM Cortex-M, write functions that follow the calling convention, and drive GPIO pins through their registers.

Every box runs on a Cortex-M simulator built into this page, so nothing needs installing and nothing can brick a board. After each run you see the registers, the flags, and eight LEDs on GPIO port A. The simulator stops any loop that runs past 200,000 instructions, so a mistake never freezes the page.

There are 8 lessons and 19 exercises, each with a Check button that runs your code against several inputs, a Hint and the Solution. Progress is kept in this browser. The Python course first is a good idea but not required.

Saturday: the machine

09:00Registers and the first instructions
What the CPU actually does, registers, MOV, ADD, SUB, and numbers in hex.
50 min
10:00Flags, branches and loops
CMP and the NZCV flags, labels, B and conditional branches, if/else and loops.
60 min
11:00Lunch, away from the screen60 min
12:00Bits and masks
AND, ORR, EOR, BIC, shifts and TST: setting, clearing and testing bits like a driver does.
55 min
13:05Memory, arrays and pointers
LDR and STR, addresses, walking an array, words versus bytes.
65 min

Sunday: functions and hardware

09:00Functions and the stack
BL and BX LR, the calling convention, PUSH and POP, and functions that call functions.
65 min
10:15Talking to hardware: GPIO
Memory-mapped registers, ODR, BSRR and IDR, read-modify-write, and driving LEDs.
60 min
11:15Lunch, away from the screen60 min
12:15Numbers on a microcontroller
Signed and unsigned, overflow, MUL and division, and fixed-point scaling of ADC readings.
55 min
13:20Putting it together
Reading compiler output, then two capstones: a limit check over a sweep and a bias search.
70 min

How this simulator differs from a real chip

  • It runs Thumb instructions in unified syntax, the form GCC and the ARM docs use. It is lenient about instruction encodings: any constant works with mov, and both add and adds work everywhere.
  • Conditional execution of non-branch instructions (IT blocks) is left out; use a branch.
  • bl print_int, print_hex, print_char and print_str are helpers built into the page that stand in for a debug console.
  • RAM is 64 KB at 0x20000000, the stack starts at the top of it, and GPIOA is at 0x40020000. Clocks and pin modes need no setup.

Start lesson 1