User guide

4.2 Accessing C global variables from assembly code
Global variables can only be accessed indirectly, through their address. To access a global variable, use the IMPORT
directive to import the global and then load the address into a register. You can access the variable with load and
store instructions, depending on its type.
For unsigned variables use:
LDRB/STRB for char
LDRH/STRH for short (Use two LDRB/STRB instructions for Architecture 3)
LDR/STR for int.
For signed variables, use the equivalent signed instruction, such as LDRSB and LDRSH.
Small structures of less than eight words can be accessed as a whole using the LDM and STM instructions. Individual
members of structures can be accessed by a load or store instruction of the appropriate type. You must know the
offset of a member from the start of the structure in order to access it.
Example 4-6 loads the address of the integer global globvar into r1, loads the value contained in that address into
r0, adds 2 to it, then stores the new value back into globvar.
Example 4-6 Address of global
AREA globals,CODE,READONLY
EXPORT asmsubroutine
IMPORT globvar
asmsubroutine
LDR r1, =globvar ; read address of globvar into
; r1 from literal pool
LDR r0, [r1]
ADD r0, r0, #2
STR r0, [r1]
MOV pc, lr
END
Mixing C, C++, and Assembly Language
Copyright ?1999 2001 ARM Limited 4-8