You copied the Doc URL to your clipboard.

__ldrex intrinsic

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. size in LDREX[size] is B for byte stores or H for halfword stores. If no size is specified, word stores are performed.


unsigned int __ldrex(volatile void *ptr)



points to the address of the data to be loaded from memory. To specify the type of the data to be loaded, cast the parameter to an appropriate pointer type.

Table 15. Access widths supported by the __ldrex intrinsic
InstructionSize of data loadedC cast
LDREXBunsigned byte(unsigned char *)
LDREXBsigned byte(signed char *)
LDREXHunsigned halfword(unsigned short *)
LDREXHsigned halfword(short *)
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