Firmware Programming

What is an assembler programming language?

Assembler usually means assembly language, but it's also the tool that turns that language into machine code. Here's how both work, with Arm and AVR examples.

On this page
  1. The short answer
  2. Assembly language vs. assembler: the terms
  3. What assembly language looks like
  4. From source code to machine code
  5. Every processor family has its own assembly language
  6. Common assemblers
  7. Where assembly is still used
  8. Is assembly harder than C++?
  9. How to try it without writing a whole program

The short answer

“Assembler” is often used as another name for assembly language: a low-level programming language where each line usually corresponds to one instruction the processor can execute directly. Instead of writing binary numbers, you write short mnemonics such as MOV, ADD and B (branch), along with register names and memory addresses.

Strictly speaking, though, the assembler is the program that translates assembly language into machine code, the same way a compiler translates C. Both meanings are common. In some countries and older textbooks, “assembler” is the usual name for the language. Most modern documentation says “assembly language” for the language and “assembler” for the tool.

Assembly language vs. assembler: the terms

Term What it is Example
Machine code The binary instructions the CPU actually executes 0x1840
Assembly language A human-readable text form of those instructions adds r0, r0, r1
Assembler The tool that converts assembly source into machine code GNU as, NASM, MASM
Disassembler The tool that converts machine code back into assembly objdump -d, Ghidra
Linker Combines object files into a final program image GNU ld

What assembly language looks like

Arm Cortex-M (Thumb)

This function adds three numbers. Under Arm’s standard calling convention, the first arguments arrive in registers r0, r1 and r2, and the result is returned in r0:

    .syntax unified
    .thumb
    .global add_three

add_three:
    adds r0, r0, r1     @ r0 = r0 + r1
    adds r0, r0, r2     @ r0 = r0 + r2
    bx   lr             @ return to caller

C code could call this as uint32_t add_three(uint32_t a, uint32_t b, uint32_t c);.

AVR

On an 8-bit AVR such as the ATmega328P, these lines turn on the LED pin of an Arduino Uno:

    ldi  r16, (1 << 5)  ; load the value 0b00100000 into register r16
    out  DDRB, r16      ; make pin PB5 an output
    out  PORTB, r16     ; drive PB5 high (LED on)
loop:
    rjmp loop           ; stay here forever

The two languages look different because the processors are different. More on that below.

From source code to machine code

When the Arm example is assembled, each instruction becomes a 16-bit Thumb opcode:

Assembly Machine code (hex)
adds r0, r0, r1 1840
adds r0, r0, r2 1880
bx lr 4770

You can see the one-to-one relationship. The register numbers are packed into fields inside each opcode, which is why r1 and r2 produce different encodings of the same adds instruction.

What an assembler does

An assembler does more than look up opcodes. A typical assembler:

  1. Reads the source and splits it into labels, instructions, operands and directives.
  2. Builds a symbol table. Labels like loop and add_three are names for addresses. Since a label can be used before it’s defined, many assemblers make two passes: one to find every label’s address and one to generate code.
  3. Encodes each instruction into its binary form, choosing the right variant for the operands.
  4. Handles directives that reserve memory, define constants or place code in specific sections.
  5. Expands macros, if you’ve defined any.
  6. Writes an object file (such as ELF) containing the machine code plus relocations. Relocations are notes for the linker about addresses that aren’t known yet, such as a call to a function in another file.

The linker then combines object files from assembly and C, assigns final addresses using a linker script, and produces the image that goes into flash.

Directives, labels and pseudo-instructions

Not every line in an assembly file becomes an instruction:

  • Labels (loop:) mark addresses.
  • Directives instruct the assembler itself. Examples include .section .text (put the following code in the code section), .word 0x12345678 (place a 32-bit constant here), .equ LED_PIN, 5 (define a constant) and .global (make a symbol visible to the linker).
  • Pseudo-instructions look like instructions but expand into one or more real ones. In GNU Arm syntax, ldr r0, =0x40020014 loads a 32-bit constant by placing it in a nearby “literal pool” and generating a PC-relative load.
  • Comments start with @ or // in GNU Arm syntax and ; in many other assemblers.

Every processor family has its own assembly language

Assembly language isn’t one language. Each processor architecture has its own instruction set, registers and conventions, so code written for one won’t run on another:

  • Arm: 32-bit A32 and Thumb/T32 (Cortex-M uses Thumb), and 64-bit A64
  • x86 and x86-64: desktop and server processors from Intel and AMD
  • AVR: 8-bit Microchip (formerly Atmel) microcontrollers, as used on classic Arduino boards
  • PIC: Microchip’s 8-, 16- and 32-bit families. PIC32 uses a MIPS-based core.
  • RISC-V: an open instruction set used in a growing number of microcontrollers and processors
  • 8051: an old 8-bit architecture that still appears in many low-cost chips

Even on one architecture, syntax can differ between assemblers. On x86, for example, Intel syntax (mov eax, 5) and AT&T syntax (movl $5, %eax) put the operands in opposite order.

Common assemblers

Assembler Architectures Notes
GNU Assembler (as, “gas”) Many, including Arm, x86, AVR, RISC-V Part of GNU Binutils; used behind GCC
LLVM integrated assembler Many Built into Clang
NASM x86, x86-64 Popular open-source assembler with Intel syntax
MASM x86, x86-64 Microsoft’s assembler, shipped with Visual Studio
Arm Compiler (armclang) Arm Arm’s commercial toolchain, used in Keil MDK
AVR Assembler (avrasm2) AVR Microchip’s assembler for AVR, available in its IDEs
pic-as PIC Microchip’s current PIC assembler, which replaced the older MPASM

Where assembly is still used

Compilers are very good now, so most firmware is written in C or C++. Assembly still shows up in a few places:

  • Startup code that runs before C can work, such as early vector tables and stack setup.
  • RTOS context switching, which saves and restores registers exactly.
  • Special instructions that C can’t express, such as some barrier, sleep and system-register instructions. These are often wrapped in compiler intrinsics.
  • Cycle-exact timing for bit-banged protocols or tight signal-processing loops.
  • Very small chips where every byte of flash counts.
  • Debugging. Reading the compiler’s assembly output is often how you find out why optimized code behaves unexpectedly.

Our guide to assembly language programming for embedded systems goes deeper into calling conventions, inline assembly, and mixing assembly with C.

Is assembly harder than C++?

It depends on what you mean by “hard”:

  • Individual assembly instructions are simple. Each one does one small thing, and an instruction set can be learned in days or weeks.
  • Assembly programs are hard to write and maintain. You track every register yourself, manage the stack by hand, and get no type checking. A small mistake can corrupt memory silently. Code also has to be rewritten for each architecture.
  • C++ is a big, complex language. Templates, object lifetimes, overload rules and the standard library take years to master, but the compiler catches many mistakes for you.

So assembly is a small language that’s unforgiving to use, and C++ is a large language that’s safer to use. For most embedded work, the practical path is to write in C or C++ and learn enough assembly to read compiler output and handle the occasional low-level routine.

How to try it without writing a whole program

  • Compiler Explorer (godbolt.org) shows the assembly a compiler generates for C code you type in. You can pick the compiler, architecture and optimization level.
  • Ask your own compiler. arm-none-eabi-gcc -O2 -S file.c writes file.s, the generated assembly.
  • Disassemble a build. arm-none-eabi-objdump -d firmware.elf lists every instruction in a compiled program.
  • Step through instructions. In GDB, stepi executes one machine instruction at a time and disassemble shows where you are. Our article on debugging with JTAG, SWD and GDB covers the setup.

Spending a little time reading the assembly behind your own C code is one of the quickest ways to understand what a microcontroller is actually doing. That knowledge carries over directly to embedded C programming.

The complete guide to firmware programming

Embedded Firmware Development: Core Programming Concepts
Read the guide

Keep exploring

Guides from other topics

All topics →