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.

asm("nop") instruction

Other Parts Discussed in Thread: CC2430

I am a bit familiar with assembly language and i know that the 'nop' instruction is basically used to skip an instruction cycle. What I don't know however is what the 'asm("nop")' instuction does.  I tried to compile some code which included this instruction (using Keil IDE) but the compiler returns an error which i can't make sense of. The error message reads:  " 'asm' requires ANSI-style prototype".  

'asm' is the name of the function being called. It is invoked with parameter "nop" which, I think, should be a string according to the rules of C.

I thank you in advance.

  • Hi Tebogo.

    The asm("nop") is the way to inline a "nop" assembly instruction into C code in IAR C/C++ Compiler for 8051. You can inline assembly code (although with reduced flexibility) in your C by using that compiler specific macro intrinsic function asm().

    The equivalent way to do a inline nop instruction for Keil PK51/C51 Compiler would be to use the _nop_() intrinsic.

    Also, when porting code from IAR to Keil C compiler, watch out that the Keil compiler is big-endian, while the IAR compiler is little-endian -- in case you stumble upon some non-endian-proof code.

  • Thanks, this solved all my problems. Using _NOP_() solves everything. The code (which is example code for the CC2430 downloaded from a TI page) also supports multiple compilers (Keil, IAR e.t.c) so I think I should be fine from hereon.

    Thank you

  • Hi esy,

    What exactly is the purpose of performing nop's in your C code? I understand that you're basically just eating clock cycles, but for what purpose?

    Thanks so much.

  • To add to Esy's comments, the asm intrinsic operator is not part of ANSI C.  In fact, the C standard never includes this as a keyword in its documentation.  It does describe an asm styled operator which can be supplied by an implementation but it does not define it.  Hence, why _asm_ is used by keil.  Interestingly enough, the C++ standard does describe the asm operator as a keyword and also defines it's prototype.  Because of this, many C compilers use the same prototype for coding in C as it works well when combining the two compilers into one.  However, the Keil compiler is a C only compiler.  It cannot compile even the most basic C++ constructs and thus it has no dependence on the C++ asm keyword.

    One additional note.  Most assemblers assume any line of code with non-white space characters in the first column are going to be labels.  Therefore in most asm statements which are not assembler directives of some sort, there is always at least one space in front of the assembler instruction as in " nop" because "nop" will produce syntax errors in the assembler as it can become a duplicated label.

    Jim Noxon

  • One may wish to delay processing by a known amount of cycles as it can be required for certain interactions with the hardware.  An example might be to write a bit-banged serial interface where higher data rates preclude the option of using an interrupt driven context as the ISR overhead is longer than the required timing.  Another option I use quite frequently is to provide a statement which does nothing but cannot be optimized away so I always have a place for a break point in a known position in code.  Yet another can be to simply align certain code instructions to specific addresses.  There are many more reasons but this should give you an idea.

    Jim Noxon