I’m using gcc 4.6.3 compiled for sam7s processor.
I need to use some inline assembly:
int res;
asm volatile (" \
MRS r0,CPSR \n \
MOV %0, r0 \n \
BIC r0,r0,%1 \n \
MSR CPSR,r0 \n \
" : "=r" (res) : "r" (0xc0) : "r0" );
return res;
Which is translated by gcc to (comments added by me):
mov r3, #192 ; load 0xc0 to r3
str r0, [sl, #56] ; preserve value of r0?
mrs r0, CPSR ; load CPSR to r0
mov r3, r0 ; save r0 to "res"; r3 overwritten!
bic r0, r0, r3 ;
msr CPSR_fc, r0 ;
The problem is that in place of “%0” (res) and “%1” (constant: 0xc0) the same register “r3” is used. For this reason %1 is overwritten before it is used, and code work incorrectly.
The question is how can I forbid gcc to use the same register for input/output operands?
Ok, finally i’ve found it here
After changing
"=r" (res)to"=&r" (res)everything works fine.