4.7.20. __ldrex

This intrinsic inserts an instruction of the form LDREX{size} into the instruction stream generated by the compiler. It enables you to load data from memory in your C or C++ code using an LDREX instruction.


unsigned int __ldrex(volatile void *ptr)



Points to the address of the data to be loaded from memory. To specify the size of the data to be loaded, cast the parameter to an appropriate integral type.

Table 4.8. Access widths supported by the __ldrex intrinsic

InstructionSize of data loadedC cast
LDREXBunsigned byte(char *)
LDREXHunsigned halfword(short int *)
LDREXword(int *)

Return value

The __ldrex intrinsic returns the data loaded from the memory address pointed to by ptr.


The compiler does not recognize the __ldrex intrinsic when compiling for a target that does not support the LDREX instruction. The compiler generates either a warning or an error in this case.

The __ldrex intrinsic does not support access to doubleword data. The compiler generates an error if you specify an access width that is not supported.


int foo(void)
    int loc = 0xff;
    return __ldrex((volatile char *)loc);

Compiling this code with the command-line option --cpu=6k produces

||foo|| PROC
    MOV      r0,#0xff
    LDREXB   r0,[r0]
    BX       lr

See also

Copyright © 2007, 2010 ARM Limited. All rights reserved.ARM DUI 0348A