GCC Inline Assembler "memory" Clobber don't prevent from re-arrange the code in ARM .

Liu Hao lh_mouse@126.com
Fri Nov 3 07:46:00 GMT 2017

On 2017/11/3 11:11, stephen lu wrote:
> I read article about GCC Inline Assembler
> (http://www.ethernut.de/en/documents/arm-inline-asm.html).
> (... abridged ...)
> But I disassemble code by objdump -d .  "memory" Clobber  don't works,
> the code is to do execute both inline assembler instructions, and then
> do the multiplication.
> mrs     ip, CPSR
> orr     ip, ip, #192    ; 0xc0
> msr     CPSR_c, ip
> mrs     ip, CPSR
> bic     ip, ip, #192    ; 0xc0
> msr     CPSR_c, ip
> mul     r0, r1, r0
> mov     pc, lr
> Can anyone help me?
> (... abridged ...)

Does your variable `c` have file scope?

If it has block scope and is not passed to the asm statement as an 
output or input-output parameter directly or indirectly, the compiler 
will assume that it is not altered by the asm statement hence can be 

The problem can be resolved by adding dummy constraints that will 
prohibit rearrangement of these statements:


int foo(int c, int b){
     // Note the two `"+r"(c)` in-out constraints.
     asm volatile("mrs r12, cpsr\n\t"
         "orr r12, r12, #0xC0\n\t"
         "msr cpsr_c, r12\n\t" :"+r"(c):: "r12", "cc");
     c *= b;
     asm volatile("mrs r12, cpsr\n"
         "bic r12, r12, #0xC0\n"
         "msr cpsr_c, r12" :"+r"(c):: "r12", "cc");
     return c;

Best regards,

More information about the Gcc-help mailing list