Most embedded firmware today is written in C or C++, and modern compilers produce good machine code for general logic. Assembly hasn’t disappeared, though. It’s moved into a few specific places where a compiler can’t do the job or can’t do it predictably enough. Knowing where those places are, and being able to read assembly even if you rarely write it, is a useful skill for any firmware developer.
If you’re not sure what an assembler is, start with What is an assembler programming language?
Is assembly still used in embedded systems?
Yes, but sparingly. A typical microcontroller project might have a short assembly startup file, possibly some RTOS port code, and nothing else. Whole applications written in assembly are now uncommon. You mostly find them on very small or legacy 8-bit parts, in hobby and retro projects, or where a team has a long history with a particular architecture.
Where assembly still appears
Startup and reset code
Before C code can run, something has to set up the environment it expects: a valid stack pointer, initialized .data, zeroed .bss. On many toolchains, that reset handler is provided as an assembly file, such as the startup_<device>.s files in many Cortex-M vendor packages. On Cortex-M, startup code can be written in C, because the hardware loads the stack pointer from the vector table, so you’ll see both styles. The firmware development overview walks through the reset-to-main() sequence.
RTOS context switching
Switching between tasks means saving one task’s registers and restoring another’s, including the stack pointer itself. C can’t express that directly. On Cortex-M, RTOS kernels such as FreeRTOS typically do the switch in the PendSV exception handler, written in assembly or as a “naked” function with inline assembly.
Cycle-exact timing
Some protocols or bit-banged signals need precise timing, for example driving certain addressable LEDs from a chip without a suitable peripheral. In C, the compiler decides which instructions to emit, so a change in optimization level can change the timing. In assembly, you can count cycles. That only holds on simpler cores, though. Caches, flash wait states and pipelines on faster chips make cycle counts less predictable.
Special instructions
Some instructions have no C equivalent: enabling and disabling interrupts, wait-for-interrupt, memory and instruction barriers, and saturating or DSP operations. These are usually reached through intrinsics (see below) rather than raw assembly.
Hand-optimized hot loops
DSP filters, cryptography and other tight loops are sometimes hand-written in assembly. Arm’s CMSIS-DSP and CMSIS-NN libraries, for example, use optimized code for some Cortex-M targets. This is worth doing only after profiling shows the loop really is the bottleneck, and after checking that the compiler’s output can’t be improved with better C or intrinsics.
What embedded assembly looks like
Arm Cortex-M (Thumb-2) example
A simple delay loop in GNU assembler syntax:
.syntax unified
.thumb
.global delay_loop
.type delay_loop, %function
@ void delay_loop(uint32_t count);
@ count arrives in r0 (AAPCS)
delay_loop:
1: subs r0, r0, #1 @ r0 = r0 - 1, update flags
bne 1b @ branch back while r0 != 0
bx lr @ return to caller
AVR example
Toggling a pin on an ATmega328P (PB5, the Arduino Uno LED):
sbi DDRB-0x20, 5 ; set PB5 as output (I/O address form)
loop:
sbi PINB-0x20, 5 ; writing 1 to PINx toggles the pin on many AVRs
rjmp loop
The -0x20 converts the memory-mapped address from the device header into the I/O-space address that sbi expects. Some include files define I/O-space names directly, which is exactly the kind of detail that makes assembly less portable. Check your assembler’s conventions.
Mixing C and assembly
Calling conventions (AAPCS)
To call assembly from C, or C from assembly, both sides must follow the same calling convention. On 32-bit Arm, that’s the AAPCS (Procedure Call Standard for the Arm Architecture). In brief:
- Arguments go in
r0–r3, and any further arguments go on the stack. - The return value comes back in
r0(orr0–r1for 64-bit values). r4–r11are callee-saved: if your function uses them, it must restore them before returning.- The stack must stay 8-byte aligned at public function boundaries.
AVR-GCC has its own convention with different register assignments. Always look up the ABI for your target.
Inline assembly
GCC and Clang let you embed assembly inside C:
static inline void cpu_nop(void)
{
__asm volatile ("nop");
}
Anything more complex needs the extended syntax with input, output and clobber lists, and it’s easy to get subtly wrong. If you don’t tell the compiler which registers or memory you touched, it may assume they’re unchanged.
Intrinsics as a safer alternative
For most single-instruction needs, compiler or CMSIS intrinsics are the better choice:
__disable_irq(); /* CPSID i */
__enable_irq(); /* CPSIE i */
__WFI(); /* wait for interrupt */
__DSB(); /* data synchronization barrier */
They’re readable, the compiler understands their effects, and they’re portable across toolchains that support CMSIS.
Reading compiler output: the most useful assembly skill
For many firmware developers, reading assembly is far more valuable than writing it. You’ll use it to:
- Check whether a
volatileaccess actually happens. See the volatile keyword in embedded C. - Understand a crash address when a fault handler reports the program counter.
- Confirm that an “optimized” piece of C really got faster.
- Step through code in a debugger when no source is available.
Practical ways to see compiler output:
arm-none-eabi-objdump -d firmware.elf(oravr-objdump) to disassemble a build-Sor-save-tempswith GCC to keep the generated assembly files- Compiler Explorer (godbolt.org) to compare small snippets across compilers and flags
- The disassembly view in your debugger. See debugging with JTAG, SWD and GDB
Tools: assemblers, disassemblers, debuggers
- GNU assembler (
as), which ships with GCC toolchains such asarm-none-eabi-andavr- - Vendor assemblers in commercial toolchains (IAR, Arm Compiler, Microchip’s tools)
- objdump and similar disassemblers
- Debuggers with instruction-level stepping and register views
Costs and risks
Assembly carries real costs, which is why it’s used sparingly:
- No portability. Cortex-M assembly won’t run on RISC-V or AVR. Moving to a new chip family means rewriting it.
- Harder maintenance. Fewer people on a team can safely change it, and there’s no type checking to catch mistakes.
- Easy-to-miss bugs. Forgetting to preserve a callee-saved register or misaligning the stack can cause failures far from the actual mistake.
- Often no faster. For general code, modern optimizing compilers frequently match or beat hand-written assembly. Measure before and after.
A reasonable rule: use assembly only where C (plus intrinsics) can’t do the job, keep it in small, well-commented functions with a clean C interface, and test it carefully.
Is assembly harder than C or C++?
It depends on what “harder” means. Assembly has very little syntax, and each instruction does one simple thing, so the basics are easy to learn. Writing large, correct programs in it is much harder: you manage every register and stack slot yourself, and nothing stops you from making mistakes. C++ is the opposite. It’s a huge language with many features to master, but it handles the low-level bookkeeping for you. For embedded work, most people find that learning to read assembly for their target is quite manageable alongside C.
As for C vs. C++ in embedded: both are widely used. C is the default on smaller microcontrollers and in most vendor code. C++ is common in larger projects. See Embedded C programming for microcontrollers for the C side.
How to learn embedded assembly
- Pick one architecture you actually use, such as Cortex-M (Thumb-2), AVR or RISC-V, and get its instruction set reference.
- Write small C functions and read the compiler’s output at different optimization levels.
- Single-step through startup code in a debugger and watch the registers change.
- Write one or two small assembly functions called from C, following the ABI.
- Read the context-switch code of an open-source RTOS for your core.