ARM Load/Store Addressing, Shifts, and Conditions
Master the RISC load/store datapath of Cortex-A7 from first principles: separate effective-address calculation from pre/post-indexed base writeback, combine inline barrel shifts with BIC/ORR field masks, route VBAR exception vectors, and avoid signed-flag bugs on DDR3 addresses.
What you'll learn
- Compute pre-indexed and post-indexed effective addresses and writeback values.
- Derive field replacement with BIC and ORR without corrupting adjacent bits.
- Distinguish unsigned carry conditions from signed overflow conditions.
Load/Store Effective Addresses, Barrel Shifts, and ALU Condition Physics
Why does ARMv7-A forbid CISC-style instructions like orl $8, 0x0209C000 that modify memory in a single instruction? Because Cortex-A7 is a load/store RISC pipeline: arithmetic and bitwise logic (ADD, SUB, BIC, ORR, CMP) operate exclusively on internal 32-bit registers in a single ALU cycle, while dedicated LDR and STR instructions move words between registers and the bus. That clean separation unlocks two hardware superpowers—and two classic assembly traps—illustrated in Figure 2.2a:
- Effective Address (
EA) vs Base Writeback (!vs post-index): EveryLDR/STRanswers two independent questions: which memory address hits the bus (EA), and what value remains in the base register afterward? Starting fromr1 = 0x1000:
- Offset (
ldr r2, [r1, #4]):EA = 0x1004;r1stays0x1000(zero writeback). - Pre-indexed writeback (
ldr r2, [r1, #4]!): Adds#4first, readsEA = 0x1004, and writes0x1004back intor1(skipping word0x1000!). - Post-indexed writeback (
ldr r2, [r1], #4): Reads originalEA = 0x1000first, then writes0x1004intor1! Notice that inspectingr1 == 0x1004in a debugger after the instruction cannot tell you whether0x1000or0x1004was read!
- Zero-Cycle Inline Barrel Shifter +
BIC/ORR: Placed directly ahead of theALU's second input port (Operand2) is a combinational barrel shifter (LSL,LSR,ASR,ROR) that shifts a register by0..31bits in the same clock cycle asADD,ORR, orLDR! PairingBIC(Bit Clear:Rd <- Rn & ~Op2) with width-clampedANDand shiftedORR(orr r2, r2, r1, lsl #8) replaces multi-bit register fields cleanly without corrupting neighbors.
Figure 2.2a. Separating accessed Effective Address from base writeback (top) and inline barrel-shifted BIC/ORR field replacement (bottom).
mental model: Pre-indexed ([r1, #4]!) versus post-indexed ([r1], #4) addressing is like stepping forward on a tiled floor either before or after taking a core sample: your boots stop on 0x1004 either way, yet you sampled two different locations (0x1004 vs 0x1000).
Where it stops working: Unlike floor tiles, ARMv7-A memory accesses also check alignment and side effects, and comparing 32-bit pointers above 0x80000000 with signed condition codes treats valid DDR3 RAM as negative numbers.
definition (Effective Address,BIC/ORR Field Masking & Unsigned vs Signed CMP Flags): An ARMv7-A load/store computes an Effective Address (EA) separately from base writeback: pre-indexed [Rn, #off]! accesses Rn + off and updates Rn, while post-indexed [Rn], #off accesses Rn then updates Rn <- Rn + off. BIC clears mask bits (Rn & ~mask) ahead of ORR insertion. CMP Rn, Rm computes Rn + ~Rm + 1 and sets CPSR flags without storing a difference: unsigned conditions test carry C == 1 (BHS = no borrow, Rn >= Rm) and C == 0 (BLO, Rn < Rm), whereas signed two's-complement conditions test N == V (BGE) and N != V (BLT).
Trace the four independent computations—base/offset, effective address, shifted mask, and flag interpretation—in the step diagram below:
- Step 1. Step 1 (Base & Offset): Separate base pointer Rn from byte offset; VBAR exception bases also require 32-byte alignment.
- Step 2. Step 2 (Effective Address): Pre-index [Rn, #4]! touches Rn+4; post-index [Rn], #4 touches original Rn before advancing.
- Step 3. Step 3 (Operand Path): Clamp incoming field width with AND before barrel-shifting LSL into BIC + ORR.
- Step 4. Step 4 (Condition Flags): Always use unsigned BHS/BLO (C flag) for RAM pointers >= 0x80000000; reserve BGE/BLT (N vs V) for signed ints.
Figure 2.2b. Track accessed address, final base register, field width mask, and unsigned vs signed flags as four separate facts.
Exception Vector Base Routing and Unsigned vs Signed Flag Divergence
Why does signed versus unsigned branching bite i.MX6ULL firmware engineers on Day 1? Look at the right panel of Figure 2.2b! External DDR3 on i.MX6ULL starts at physical address 0x80000000 (bit 31 == 1), while internal OCRAM sits at 0x00900000 (bit 31 == 0). Inside the hardware ALU, cmp r0, r1 (0x87800000 - 0x00900000 = 0x86F00000) computes 0x87800000 + 0xFF6FFFFF + 1, producing N = 1 (bit 31 of result is 1), Z = 0, C = 1 (carry out of bit 31 -> zero unsigned borrow!), and V = 0.
- An unsigned branch
bhs(C == 1) sees2,273,312,768 >= 9,437,184-> TRUE! - A signed branch
bge(N == V) sees-2,021,654,528 < +9,437,184(1 != 0) -> FALSE! Writingbltinstead ofbloin a.bsszeroing loop insideDDR3(0x87800000) instantly treats valid RAM addresses as negative integers!
Figure 2.2b. Exception vector base routing via SCTLR.V and 32-byte-aligned VBAR (left) and why DDR3 addresses (>= 0x80000000) require unsigned BHS/BLO carry conditions rather than signed BGE/BLT (right).
Now inspect the left panel of Figure 2.2b (Exception Vector Base Routing) and replace_nibble in target.S below. First, when an IRQ (slot 6) arrives, Cortex-A7 checks SCTLR.V (bit 13): if V == 1, it fetches 0xFFFF0000 + 4 * 6 = 0xFFFF0018; if V == 0, it fetches VBAR + 4 * 6 (0x87800018), where VBAR (c12, c0, 0) must be 32-byte aligned (VBAR[4:0] == 0) because 8 vector words occupy 32 bytes (slot 5 at +0x14 is reserved). Second, in replace_nibble, ldr r2, [r0], #4 reads original [r0] and post-increments r0 by +4; therefore, str r2, [r0, #-4] must subtract 4 to write back the modified word rather than clobbering the next word in RAM, while and r1, r1, #15 clamps oversized caller inputs (0x32 -> 0x02) before lsl #8!
learner@ksa:~/gl-e01/m02-l02$ bat target.S File: target.S .syntax unified .cpu cortex-a7 .arm .text .global replace_nibble .type replace_nibble, %function replace_nibble: ldr r2, [r0], #4 bic r2, r2, #0x00000f00 and r1, r1, #15 orr r2, r2, r1, lsl #8 str r2, [r0, #-4] bx lr .size replace_nibble, .-replace_nibbleAuditing Vector Alignment, Pre/Post Indexing, and Nibble Masking in C11
sctlr_vbar_addr_audit.c verifies all four mechanisms in strict ISO C11: vector_entry_addr() rejects reserved slot 5, enforces 32-byte VBAR alignment ((vbar & 0x1fU) == 0U) whenever SCTLR.V == 0, and overrides VBAR with 0xFFFF0000U when SCTLR.V == 1; eff_addr() proves post-index (0x1000) and pre-index (0x1004) touch distinct addresses despite identical 0x1004 writeback; and replace_nibble(0xA5A5FFFFU, 0x32U) proves 0x32 & 0x0F prevents upper bits from spilling into [15:12].
learner@ksa:~/gl-e01/m02-l02$ bat -H 8 -H 9 -H 10 -H 11 -H 12 -H 13 -H 14 -H 15 -H 16 -H 20 -H 21 -H 22 -H 23 -H 27 -H 28 -H 29 -H 30 sctlr_vbar_addr_audit.c File: sctlr_vbar_addr_audit.c #include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> enum { VEC_RESET = 0U, VEC_UNDEF = 1U, VEC_SVC = 2U, VEC_PABT = 3U, VEC_DABT = 4U, VEC_IRQ = 6U, VEC_FIQ = 7U }; static bool vector_entry_addr(uint32_t sctlr, uint32_t vbar, unsigned slot, uint32_t *addr_out) { if (slot > 7U || slot == 5U) return false; bool high_vecs = ((sctlr >> 13) & 1U) != 0U; if (!high_vecs && (vbar & 0x1fU) != 0U) return false; uint32_t base = high_vecs ? 0xffff0000U : vbar; *addr_out = base + (slot << 2); return true; } static uint32_t eff_addr(uint32_t base, uint32_t off, bool pre, uint32_t *wb) { *wb = base + off; return pre ? (base + off) : base; } static uint32_t replace_nibble(uint32_t old, uint32_t field) { return (old & ~UINT32_C(0x0f00)) | ((field & 0x0fU) << 8); } int main(void) { uint32_t irq_vec = 0U, hi_irq = 0U; uint32_t sctlr_v0 = (1U << 12) | (1U << 2) | 1U; /* I=1, C=1, M=1, V=0 */ assert(vector_entry_addr(sctlr_v0, 0x87800000U, VEC_IRQ, &irq_vec)); assert(irq_vec == 0x87800018U); assert(!vector_entry_addr(sctlr_v0, 0x87800010U, VEC_IRQ, &irq_vec)); assert(vector_entry_addr(sctlr_v0 | (1U << 13), 0x87800010U, VEC_IRQ, &hi_irq)); assert(hi_irq == 0xffff0018U); uint32_t wb1 = 0U, wb2 = 0U; assert(eff_addr(0x1000U, 4U, false, &wb1) == 0x1000U); assert(eff_addr(0x1000U, 4U, true, &wb2) == 0x1004U && wb1 == wb2); uint32_t next = replace_nibble(0xa5a5ffffU, 0x32U); assert(next == 0xa5a5f2ffU && (next & ~0xf00U) == 0xa5a5f0ffU); assert(UINT32_MAX >= 1U && INT32_C(-1) < INT32_C(1)); printf("IRQ_vec=0x%08X hi_vec=0x%08X post=0x1000 pre=0x1004 nib=0x%08X\n", irq_vec, hi_irq, next); return 0; }- Enforce VBAR[4:0] == 0 (32-byte alignment) and select 0xFFFF0000 high vectors whenever SCTLR.V (bit 13) is 1.
- Distinguish pre-indexed [base, #off]! (base + off accessed) from post-indexed [base], #off (base accessed).
- Mask the incoming value to 4 bits before shifting by 8 so BIC + ORR cannot spill into bits [15:12].
Executing the Addressing and Vector Verifier
Build and run sctlr_vbar_addr_audit.c on the workstation and cross-assemble target.S for Cortex-A7. Experiment by deleting & 0x0fU inside replace_nibble() in a temporary edit to watch 0x32U << 8 corrupt nibble [15:12] and trip the neighbor-preservation assertion immediately.
learner@ksa:~/gl-e01/m02-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic sctlr_vbar_addr_audit.c -o sctlr_vbar_addr_auditlearner@ksa:~/gl-e01/m02-l02./sctlr_vbar_addr_auditIRQ_vec=0x87800018 hi_vec=0xFFFF0018 post=0x1000 pre=0x1004 nib=0xA5A5F2FFlearner@ksa:~/gl-e01/m02-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m02-l02learner@ksa:~/gl-e01/m02-l02strace -e trace=write,exit_group \./sctlr_vbar_addr_auditwrite(1, "IRQ_vec=0x87800018 hi_vec=0xFFFF"..., 75) = 75exit_group(0) = ?+++ exited with 0 +++Comparing ARMv7-A Addressing Modes and Condition Suffixes
Review the side-by-side summary table below before writing startup copy/zero loops in Module 5. Also keep peripheral semantics in mind: replace_nibble is safe on ordinary RAM or RW control latches under exclusive ownership, never on FIFO data ports or Write-One-to-Clear (W1C) status registers where reading or replaying 1 bits triggers hardware side effects.
| ARMv7-A Mechanism | Syntax Example | Effective Address / Flag Rule | Classic Bring-Up Bug When Confused |
|---|---|---|---|
| Offset addressing (no writeback) | ldr r2, [r0, #4] | EA = r0 + 4; r0 unchanged | Loop pointer never advances (infinite loop over first word) |
| Pre-indexed writeback | ldr r2, [r0, #4]! | EA = r0 + 4; r0 <- r0 + 4 | Skips word 0 ([r0 + 0]) at the start of .data copy or .bss zeroing |
| Post-indexed writeback | ldr r2, [r0], #4 | EA = r0; r0 <- r0 + 4 | Subsequent str r2, [r0] writes r0 + 4 unless [r0, #-4] compensates |
Unsigned BHS vs signed BGE | cmp r0, r1 -> bhs / bge | BHS: C == 1; BGE: N == V | Using signed BLT/BGE on DDR3 (>= 0x80000000) treats RAM pointers as negative! |
Later: Lesson m02-l03 combines load/store writeback with control flow—building 8-byte-aligned full-descending stack frames (STMFD / LDMFD) across nested assembly-to-C calls.
Common Production Pitfalls
warning: Symptom: A .bss zeroing loop in DDR3 at 0x87800000 compares cmp r0, r1 and branches with signed blt (or bge), either aborting after 0 iterations or overrunning RAM when comparing against lower regions. Root Cause: Any 32-bit address >= 0x80000000 has sign bit 31 == 1 (negative in two's-complement!), inverting signed BLT/BGE/BGT/BLE predicates. Fix: Always compare memory addresses, sizes, and cycle counters with unsigned condition codes (BLO for <, BHS for >=, BHI for >, BLS for <=).
warning: Symptom: Bare-metal startup writes 0x87800000 to CP15 VBAR (mcr p15, 0, r0, c12, c0, 0), yet the first IRQ jumps to 0xFFFF0018 instead of 0x87800018. Root Cause: Either SCTLR.V (bit 13) remained 1 from the Boot ROM environment (forcing high vectors at 0xFFFF0000), or .vectors lacked .balign 32 (VBAR[4:0] == 0). Fix: Align .vectors to 32 bytes, clear SCTLR bit 13 (bic r0, r0, #(1 << 13)), write VBAR, and execute isb.
Practice & Retrieval
Guided Practice: Scaled Barrel-Shift Indexing & CMP Flag Trace
Step 1 (Scaled Register Offset): Given r0 = 0x020C4068 (CCM_CCGR0) and r1 = 3, compute EA and final r0 for ldr r2, [r0, r1, lsl #2] vs ldr r2, [r0, r1, lsl #2]! (3 << 2 = 12 = 0x0C -> 0x020C4074 CCM_CCGR3; only ! updates r0). Step 2 (VBAR Check): State why VBAR = 0x00907020 is legal while 0x00907010 is invalid when SCTLR.V == 0 (bit 4 violates 32-byte alignment). Step 3 (Flag Trace): For CMP 0x87800000, 0x00900000, verify N=1, Z=0, C=1, V=0, BHS = true, and BGE = false.
Independent Practice: Exact AArch32 ALU Subtraction Flag Simulator (cmp_flags_audit.c)
Write a standalone ISO C11 verifier cmp_flags_audit.c implementing void arm_cmp32(uint32_t a, uint32_t b, bool *n, bool *z, bool *c, bool *v) using 64-bit unsigned carry ((uint64_t)a + (uint64_t)(~b) + 1ULL) and signed overflow (((a ^ b) & (a ^ diff)) >> 31) != 0U. Assert BEQ, BHS, BLO, BHI, BGE, and BLT across (0x87800000U, 0x00900000U), (0x7FFFFFFFU, 0x80000000U), (0U, 1U), and (UINT32_MAX, UINT32_MAX).
Key takeaways
ARMv7-AseparatesALU/barrel-shifter register operations fromLDR/STRmemory transfers, and separates accessed Effective Address (EA) from base writeback.- Pre-indexed
[Rn, #off]!accessesRn + offand writesRn + offback; post-indexed[Rn], #offaccesses originalRnfirst and writesRn + offback afterward. - Replace multi-bit subfields by clearing the field mask with
BIC, clamping the new value's bit-width withAND, and inserting via shiftedORR. - After
CMP, unsigned comparisons test carry (BHSC==1,BLOC==0—mandatory forDDR3addresses>= 0x80000000!), whereas signed comparisons testN == V(BGE) andN != V(BLT). - ARM load/store instructions compute an effective address separately from arithmetic on the transferred value.
- Pre-indexed writeback updates the base using the offset before the access; post-indexing accesses the original base then updates it.
- The conditions interpret carry and signed flags differently.