Skip to content
systemcoding.dev
Assembly

Load and Store on ARM: LDR, STR, LDM and STM

How ARM Cortex-M assembly moves data between memory and registers: LDR and STR sizes and addressing, offsets, writeback, and the multi-register LDM and STM.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Arm, AMD and GNU logos.

ARM is a load-store architecture. Arithmetic never touches memory: to add two numbers that live in RAM, you load both into registers, add, and store the result back. Three families of instructions do all of that moving, ldr and str for single values, and ldm and stm for runs of registers. This post covers them for Cortex-M in Thumb-2 under unified syntax, from the Armv6-M and Armv7-M architecture reference manuals, with a worked copy routine whose result you can check by hand.

Single loads and stores

ldr loads a 32-bit word from memory into a register. str stores a register to memory. The memory operand is always in square brackets and names the address.

basic.Sasm
        ldr     r0, [r1]            // r0 = *(uint32_t *)r1
        str     r0, [r2]            // *(uint32_t *)r2 = r0
        ldr     r0, [r1, #4]        // r0 = *(uint32_t *)(r1 + 4)
        ldr     r0, [r1, r2]        // r0 = *(uint32_t *)(r1 + r2)
        ldr     r0, [r1, r2, lsl #2]// r0 = word number r2 of the array at r1 (Armv7-M)

The last form is the array index. Shifting r2 left by two multiplies it by four, the size of a word, so r2 can hold the element index. The 16-bit Thumb encoding allows only a plain register offset; the shifted-register form needs the 32-bit encoding and is not available on a Cortex-M0.

Sizes are chosen with a suffix: ldrb and strb for 8 bits, ldrh and strh for 16 bits. A load of a smaller value has to decide what to put in the upper bits. The plain forms ldrb and ldrh zero-extend. The signed forms ldrsb and ldrsh sign-extend, copying bit 7 or bit 15 into the top. A byte of 0xFF loads as 0x000000FF with ldrb and as 0xFFFFFFFF with ldrsb. Stores take the low bits and discard the rest.

Immediate offsets have ranges that depend on the encoding. In the 16-bit ldr with a low base register the offset is a multiple of four from 0 to 124; for ldrb it is 0 to 31; for ldrh a multiple of two from 0 to 62. The 32-bit Thumb-2 forms (ldr.w) take any offset from 0 to 4095. If your offset does not fit, the assembler either picks the wide form on a core that has it or reports an out-of-range error on a Cortex-M0.

Alignment

A Cortex-M3 or later can load a word from an address that is not a multiple of four, with ldr, ldrh and str, as long as the UNALIGN_TRP bit in the configuration register is clear. A Cortex-M0 cannot: any unaligned word or halfword access raises a HardFault. Instructions that transfer more than one register, ldm, stm, ldrd, strd, push and pop, always require word alignment regardless of the core. If a protocol gives you packed fields, the safe route is to read bytes with ldrb and assemble the value in registers.

Pre-index, post-index and writeback

Besides plain offset addressing there are two forms that update the base register at the same time. They are Thumb-2 only, not available on a Cortex-M0.

index.Sasm
        ldr     r0, [r1, #4]!       // pre-index: r1 = r1 + 4, then r0 = *r1
        ldr     r0, [r1], #4        // post-index: r0 = *r1, then r1 = r1 + 4

Post-index is the idiom for walking an array: one instruction reads an element and advances the pointer. The same addressing modes exist for str, ldrb and the other sizes. A later post in this series compares the two modes on AArch64, where they are written the same way.

Loading many registers at once

ldm and stm move a list of registers to or from consecutive words. The register list sits in braces, and registers transfer in ascending number order, lowest register at the lowest address. The most common forms are ldmia and stmia, where ia means "increment after": the base address starts at the first word and increases by four for each register.

copy4.Sasm
        .syntax unified
        .cpu    cortex-m0
        .thumb
        .text
        .global copy4
        .type   copy4, %function
        .thumb_func
// void copy4(uint32_t *dst, const uint32_t *src)   copy 4 words
copy4:
        ldmia   r1!, {r2, r3}       // r2 = src[0], r3 = src[1], r1 += 8
        stmia   r0!, {r2, r3}       // dst[0] = r2, dst[1] = r3, r0 += 8
        ldmia   r1!, {r2, r3}       // r2 = src[2], r3 = src[3], r1 += 8
        stmia   r0!, {r2, r3}       // dst[2] = r2, dst[3] = r3, r0 += 8
        bx      lr
        .size   copy4, .-copy4

The ! after the base register means writeback: the base register ends up pointing past the last transferred word. On a Cortex-M0, ldmia and stmia accept only low registers in the list and require the base register to be written back for stmia. A function can only use r0 to r3 and r12 without saving, and r12 is a high register, so a copy loop on an M0 is limited to the low scratch registers unless you push more.

Calling it from C:

main.cc
#include <stdint.h>
#include <stdio.h>

void copy4(uint32_t *dst, const uint32_t *src);

int main(void)
{
    uint32_t src[4] = {1, 2, 3, 4};
    uint32_t dst[4] = {0, 0, 0, 0};

    copy4(dst, src);
    printf("%u %u %u %u\n", (unsigned)dst[0], (unsigned)dst[1],
           (unsigned)dst[2], (unsigned)dst[3]);
    return 0;
}
Text
1 2 3 4

With a semihosting or UART-backed printf in your firmware this is what appears. Inside the function, r1 ends at src + 16 and r0 ends at dst + 16, because each of the two ldmia and two stmia instructions advanced its pointer by eight bytes. Since the arguments are caller-saved, nobody notices.

Push and pop are the stack forms

push {r4, lr} is an alias for stmdb sp!, {r4, lr}: store multiple, decrement before, with writeback to sp. pop {r4, pc} is ldmia sp!, {r4, pc}. The db variant stores the highest register at the highest address, so after a push, [sp] holds the lowest-numbered register of the list. Because the order is fixed, you can never pop into a different set of registers by accident: the list on the way out must match the list on the way in, with pc replacing lr to return.

Writing pc through pop or ldm branches to the loaded address, and its low bit must be set, as for bx. On Armv7-M, ldm with pc in the list also treats that as an interworking branch, so a bad value faults instead of silently running in the wrong state.

A mistake to avoid: base register in the list

Loading into the base register while using writeback is unpredictable. ldmia r0!, {r0, r1} has two writers for r0, the loaded value and the incremented address, and the architecture declines to say which wins. Without the !, ldmia r0, {r0, r1} is legal on Thumb: the loaded value wins. Assemblers warn on the unpredictable form. Treat the warning as an error.

When to prefer which

  • One value, with an index or a field offset: ldr or str with an immediate or scaled register offset.

  • A struct of several consecutive words, or saving registers: ldm, stm, push, pop. They need one instruction fetch and one address, though they are not faster per word on every core, so check the core's technical reference manual for cycle counts.

  • Reading a memory-mapped peripheral register: a plain ldr with the full 32 bits, never ldm, because many peripherals require single-access reads and an ldm may be split or restarted by an interrupt.

Also inEmbedded
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.