5.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 do the import and then load the address into a register. You can access the global variable with load and store instructions, depending on its type.

For unsigned variables, for example, use:

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 5.1 loads the address of the integer global variable 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 5.1. Accessing global variables

    PRESERVE8

    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]
    BX   lr
    END

For full details on the instructions available in ARM or Thumb code, see RealView Compilation Tools v3.0 Assembler Guide.

Copyright © 2002-2006 ARM Limited. All rights reserved.ARM DUI 0203G
Non-Confidential