2 loading addresses with ldr rd, = label – ARM VERSION 1.2 User Manual

Page 47

Advertising
background image

Writing ARM and Thumb Assembly Language

ARM DUI 0068B

Copyright © 2000, 2001 ARM Limited. All rights reserved.

2-35

2.7.2

Loading addresses with LDR Rd, = label

The

LDR Rd,=

pseudo-instruction can load any 32-bit constant into a register. See

Loading with LDR Rd, =const on page 2-27. It also accepts program-relative
expressions such as labels, and labels with offsets.

The assembler converts an

LDR r0,=label

pseudo-instruction by:

Placing the address of

label

in a literal pool (a portion of memory embedded in

the code to hold constant values).

Generating a program-relative

LDR

instruction that reads the address from the

literal pool, for example:

LDR rn [pc, #offset to literal pool]
; load register n with one word
; from the address [pc + offset]

You must ensure that there is a literal pool within range. Refer to Placing literal
pools
on page 2-28 for m
ore information.

Unlike the

ADR

and

ADRL

pseudo-instructions, you can use

LDR

with labels that are outside

the current section. If the label is outside the current section, the assembler places a
relocation directive in the object code when the source file is assembled. The relocation
directive instructs the linker to resolve the address at link time. The address remains
valid wherever the linker places the section containing the

LDR

and the literal pool.

Example 2-9 shows how this works. It is supplied as

ldrlabel.s

in the

examples\asm

subdirectory of the ADS. Refer to Code examples on page 2-2 for instructions on how
to assemble, link, and execute the example.

The instructions listed in the comments are the ARM instructions that are generated by
the assembler.

Example 2-9

AREA LDRlabel, CODE,READONLY
ENTRY ; Mark first instruction to execute
start
BL func1 ; Branch to first subroutine
BL func2 ; Branch to second subroutine
stop MOV r0, #0x18 ; angel_SWIreason_ReportException
LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit
SWI 0x123456 ; ARM semihosting SWI
func1
LDR r0, =start ; => LDR R0,[PC, #offset into

Advertising