This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

assembly question - volatile keyword

Hi

I have a piece of code that looks like this:

uint32 i;
uint32 a = 0;

for (i=0;i<u;i++)
a += (3*i+7);

i have added the "volatile" keyword to both varaibles, and i noticed that "NOP" instructions were added to the code.

for example: (copied from CCs 5 disassembly window)

without volatile:

for (i=0;i<u;i++)
MOV dbl(*SP(#02h)),AC0
ADD #1,AC0
MOV AC0,dbl(*SP(#02h))
MOV dbl(*SP(#00h)),AC0
MOV dbl(*SP(#02h)),AC1
CMPU AC1 < AC0, TC1
BCC C$L7,TC1

with volatile:

for (i=0;i<u;i++)
MOV dbl(*SP(#02h)),AC0
ADD #1,AC0
MOV AC0,dbl(*SP(#02h))
NOP
NOP
MOV dbl(*SP(#00h)),AC0
MOV dbl(*SP(#02h)),AC1
CMPU AC1 < AC0, TC1
BCC C$L7,TC1


Can you explain why are the "NOP" instructions added?

Thanks 

Yaron

  • I forgot to mention,

    I am using code composer v 5.4

    compiler version 4.4.1

  • C55x has an instruction pipeline, and there is a latency between a store and a load.

    By using "volatile", you have told the compiler to behave as though any read or write to that variable might affect any other volatile object in the entire program.  Therefore, the compiler is forced to believe that the store might somehow affect the value stored in the location you are about to load, so it must insert enough NOPs to ensure the store completes before the load starts.