SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: ARM Cortex M0 Assembly Instruction Set
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

ARM Cortex M0 Assembly Instruction Set

Graham Kruk
Last updated: October 5, 2023 9:58 am
Graham Kruk 7 Min Read
Share
SHARE

The ARM Cortex-M0 is a 32-bit RISC processor optimized for low-power embedded applications. Its assembly instruction set provides basic computational and data transfer capabilities through simple, fixed-length instructions. While more complex operations require sequences of these basic instructions, the Cortex-M0 ISA aims to maximize performance per clock cycle.

Contents
Data Processing InstructionsLoad/Store InstructionsBranch InstructionsStatus Register AccessMemory AccessInterruptsExclusive AccessOverflow/SaturationShift and RotateReverse BytesCount Leading ZerosSaturating Addition and SubtractionSummary

Data Processing Instructions

These allow basic arithmetic and logical operations on 32-bit register contents or immediate values:

  • ADD, SUB – Add or subtract two registers or a register and an immediate value
  • RSB – Reverse subtract between a register and an immediate value
  • ADC, SBC – Add/subtract with carry between registers or register and immediate
  • RSC – Reverse subtract with carry between register and immediate
  • AND, ORR, EOR – Bitwise AND, OR, XOR on registers or register and immediate
  • BIC – Bit clear register contents using AND with NOT of immediate value
  • MOV, MVN – Move or move NOT to copy register contents or invert immediate
  • CMP, CMN – Compare two registers or register vs immediate to set status flags
  • TST – Test register contents vs immediate to set status flags

These basic operations enable typical math, logical, and comparison functions required for most code. Status flags are updated automatically based on the results, allowing conditional execution of subsequent instructions.

Load/Store Instructions

Data transfer to/from memory uses these instructions:

  • LDR – Load word or half-word from memory into register
  • STR – Store register contents into memory word or half-word
  • LDM – Load multiple registers from consecutive memory words
  • STM – Store multiple registers into consecutive memory words

Memory access addresses can be simply offset from a base register value. Load and store multiples allow efficient buffered data transfer. Hardware automatically converts between register and memory data sizes as 32-bit registers can store 8-bit, 16-bit, or 32-bit data.

Branch Instructions

These instructions alter sequential program flow:

  • B – Unconditional branch to PC-relative offset
  • BL – Branch with link to save return address to LR
  • BX – Branch indirectly through register
  • BLX – Branch and link indirectly through register

Conditional variants switch execution to the target address if flags meet the specified condition code, enabling loops, if-then-else, etc. Helper instructions BKPT, SVC, and ISB provide debugging, supervisor calls, and synchronization.

Status Register Access

Special instructions read or write the CPSR status register:

  • MRS – Move PSR contents to general purpose register
  • MSR – Move general purpose register contents to PSR

This allows examination of status flags, interrupt enables, and execution mode bits as well as context switching by modifying the PSR directly. Access is limited to specific PSR fields based on privelege level.

Memory Access

The Cortex-M0 has several instructions to enable memory mapped IO:

  • LDREX/STREX – Locked read/write to synchronize access to shared resources
  • LDM/STM – Unpriveleged variants to protect regions
  • LDRT/STRT – Unpriveleged access to system control space

User code can safely access peripheral registers and shared data structures with these instructions. The processor automatically generates any stall cycles required until resources become available.

Interrupts

The NVIC unit controls interrupt handling:

  • CPSIE/CPSID – Enable or disable interrupts globally
  • SEV – Send event signal to other processors
  • WFE/WFI – Wait for event or interrupt with low power mode

This allows flexible multi-core synchronization and power saving modes. Handlers stored in the vector table will automatically save context, branch to the ISR, then restore context and resume execution after the ISR completes.

Exclusive Access

LDREX/STREX instructions allow atomic read-modify-write sequences:

  • LDREX – Load exclusive locks memory location
  • STREX – Store exclusive succeeds only if lock held
  • CLREX – Clear exclusive monitor

If the exclusive monitor detects any access to the locked location between LDREX and STREX, it will fail the store. This enables synchronization without using locks.

Overflow/Saturation

Signed and unsigned overflow conditions can be detected with these instructions:

  • SSAT – Signed saturate to N-bit value
  • USAT – Unsigned saturate to N-bit value
  • QADD/QDADD – Saturating add
  • QSUB/QDSUB – Saturating subtract

This allows optimizing dynamic range use in DSP algorithms. Saturation avoids unexpected wrap-around on overflow. The standard ADD/SUB instructions also set overflow/carry flags.

Shift and Rotate

Data manipulation made easier with these instructions:

  • LSL/LSR/ASR/ROR – Logical/arithmetic shift right or rotate by immediate
  • RRX – Rotate right extended by carry flag

Shifts allow scaling of data values, extracting bitfields, inexpensive multiplies/divides. Rotates facilitate bit reversal, CRC calculation, channel interleaving, etc. Useful for many algorithms.

Reverse Bytes

Byte order reversal instructions:

  • REV – Reverse bytes in 32-bit register
  • REV16 – Reverse bytes in 16-bit halfword
  • REVSH – Reverse bytes in bottom 16-bit halfword

Useful for converting between big/little endian data formats used in codecs, network protocols, file formats, etc. Saves many instructions vs. software Byte reversal.

Count Leading Zeros

Counts leading zero bits in registers:

  • CLZ – Count leading zeros in 32-bit value

Quickly finds first 1 bit location in data streams, useful in normalization, serialization, etc. Saves software checking each bit individually.

Saturating Addition and Subtraction

Saturated math ops prevent overflow:

  • QADD – Saturating ADD
  • QDADD – Saturating doubleword ADD
  • QSUB – Saturating SUB
  • QDSUB – Saturating doubleword SUB

Results roll over at maximum positive/minimum negative values. Useful for digital signal processing to avoid large wrap-arounds distorting outputs.

Summary

The Cortex-M0 assembly language provides a straightforward set of instructions for data movement, arithmetic/logic, branching, and status register access. Optimization for embedded control applications results in high performance despite small silicon area. Interrupt handling, exclusive access, and saturation support facilitate real-time processing and hardware control tasks.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Cortex-M0 Stack Pointer
Next Article ARM Cortex M0 Cycles Per Instruction
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

M0 SDK Load Program From External SPI Flash

The M0 SDK provides a way to load programs from…

6 Min Read

What is the cache memory in ARM processor?

Cache memory in ARM processors refers to small, fast memory…

6 Min Read

ARM Embedded Programming

ARM processors are extremely popular in embedded systems due to…

10 Min Read

Running RTX code from external RAM vs ITCM on Cortex-M1

When developing applications for Cortex-M1 based microcontrollers, one important decision…

7 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account