Re: C No Operation doubt

Discussion in 'C Programming' started by Malcolm McLean, May 25, 2014.

  1. Use a volatile, and read and write to it. The compiler can't optimise it
    to nothing because volatile specifically says that the variable may be
    modified by something outside of normal program flow control.
    Malcolm McLean, May 25, 2014
    1. Advertisements

  2. I don't understand the question in first place. If a operation has no
    observable effect, why would I want to code it in C? If I need, for
    example, a delay, then this is an effect that the C language itself
    cannot ensure. That's the matter of the operating system offering such a
    feature - or not.

    Thomas Richter, May 25, 2014
    1. Advertisements

  3. You might have a callback function, and you want to test how the client
    code responds to different execution times for the callback, e.g. you
    might want to know how many instructions you can get away with before
    you see screen flicker. It may be that sleep() doesn't offer the required

    There are lots of possible reasons for wanting a nop.
    Malcolm McLean, May 25, 2014
  4. Malcolm McLean

    David Brown Guest

    There are lots of reasons for wanting a nop - but I find it hard to
    imagine a reason for wanting a nop written in C. It is not uncommon to
    find the occasional nop in low-level code for microcontrollers, but this
    would be in very target-specific code, and thus you can use
    target-specific solutions such as intrinsic functions or inline
    assembly. I can't see why you would want a nop in general, portable C code.

    Inline assembly, "asm ("nop")", will probably work on a range of
    compilers and targets. Assembly is normally very target-specific, but
    lots of targets have a "nop" instruction.
    David Brown, May 25, 2014
  5. Side effects(Short delays). Quite common in uCs GPIO bitbanging code.
    Also, avr-gcc has been known to eat nops in inline asm(not sure if it
    still does). Writing the timing routine in assembler (separate file, gcc
    never sees it and cannot mess it up) and linking against it (the .o
    file) may be the most reliable.
    Johann Klammer, May 26, 2014
  6. Malcolm McLean

    James Kuyper Guest

    He mentioned delays above. As he said, if you need to cause short
    delays, C is not the language to use - it gives you no control
    whatsoever over the execution time of the generated machine code.
    James Kuyper, May 27, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.