8.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:

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 LDM/STM instructions. Individual members of structures can be accessed by a load/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 8.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 8.6. 

	EXPORT		asmsubroutine
	IMPORT		globvar
	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
Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D