ARM Constants, Literal Pools and Address Pseudo-instructions

Course: Computer Architectures · Lecture: ARM Constant Encoding and Literal Pools

The central problem

The source notation MOV r0, #number looks as if any 32-bit number could fit into the instruction. The instruction encoding has limited room because it must also identify the operation and register. Therefore the assembler must choose among immediate encodings, complemented values, halfword construction and memory-based literal loads.

MOV and its alternatives

MOV r0, r1 copies a register, optionally applying a shift. MOV r0, #constant can encode only particular immediate patterns: an 8-bit value shifted left by an allowed amount, or certain repeated-byte forms. It cannot be used as a general memory read and it does not directly mean “load the contents at this address.”

MVN writes the one’s complement:

MVN r0, #0x14       ; 0xFFFFFFEB

MOVW writes a 16-bit low halfword:

MOVW r0, #0xA1B2    ; 0x0000A1B2

MOVT writes the high halfword:

MOVW r0, #0x47D2
MOVT r0, #0xC901    ; 0xC90147D2

This pair can construct any 32-bit constant. MOV32 is a pseudo-instruction for the pair.

Literal-pool expansion

LDR r0, =0xC90147D2

The assembler first checks whether a MOV-family encoding is suitable. If not, it stores the constant in a literal pool and emits a PC-relative load. The instruction reads the constant from nearby code memory and copies it to r0.

This explains the two different roles of LDR:

LDR r0, [r1]             ; real load from memory at the address in r1
LDR r0, =0xC90147D2      ; pseudo-instruction for obtaining a constant

The second line may become a real LDR r0,[PC,#offset] plus a pool entry. The pool is memory, not a spare register.

PC-relative offset trace

For an instruction at 0x118 and a literal at 0x144:

instruction address = 0x118
+4                    = 0x11C
word-aligned PC       = 0x11C
offset                = 0x144 - 0x11C = 0x28 = 40

The generated form is conceptually:

LDR r0, [PC, #40]

The PC adjustment and alignment are why the offset is not always simply “pool address minus the printed instruction address.”

Range and placement

A normal Thumb literal load has the source’s stated range 0-1020 bytes. LDR.W uses a Thumb-2 form with a stated range -4095 to +4095. A pool at END can therefore be too far away. LTORG inserts the pool at a chosen point:

LDR r0, =constant
LTORG

This makes the constant reachable. A later SPACE 4095 does not move the pool backward if LTORG has already placed it.

Address pseudo-instructions

LDR r0, =label loads a label address from a literal pool and can reference another area. ADR r0, label computes a nearby address from PC without a pool entry. ADR has a smaller aligned range; ADR.W expands that range and handles an unaligned target; ADRL uses two Thumb-2 instructions for a much larger offset.

Use the mechanism that matches the target:

NeedTypical form
small encodable constantMOV/MVN
arbitrary 16-bit halfwordMOVW
arbitrary 32-bit valueMOVW + MOVT, or LDR =constant
nearby same-area labelADR/ADR.W
far or cross-area labelLDR =label

Common confusions

  • MOV r0, #address is not a general address-loading solution.
  • LDR r0, =constant is assembler syntax, not necessarily one hardware instruction.
  • LTORG solves PC-relative range, not the inability of registers to hold constants.
  • ADR calculates an address; LDR [address] reads the value stored there.
  • MOVW changes the low halfword; MOVT changes the high halfword.

Worked recall

Q: The pool is at 0x144, the instruction is at 0x118. What is the source’s offset?
A: PC is 0x11C, so 0x144 - 0x11C = 0x28 = 40.

Q: Why can ordinary LDR fail after SPACE 1021 but LDR.W work?
A: The ordinary Thumb form’s stated range ends at 1020; Thumb-2 LDR.W has a larger range.

Q: What does MOVW r0,#0x47D2 followed by MOVT r0,#0xC901 produce?
A: 0xC90147D2.

Q: Which form avoids a literal pool for a nearby address?
A: ADR, if its range and alignment constraints are satisfied.