Wish NOP instruction

Discussion in 'Bugs & wishlist' started by Troberg, Jun 4, 2015.

  1. Troberg

    Troberg Well-Known Member Licensed User

    An instruction that exists in REXX and most (all?) assembler variants is NOP. This is short for No Operation, and it does nothing.

    So, what is the use?

    Well, it's a way for me as developer to explicitly tell that I don't want to do anything, as opposed to "Oops, he forgot something".

    Example:

    Code:
    Try
      
    'Code which may fail, but I know why it fails and it can safely be ignored
    Catch
      NOP
    End Try
    In the above example, I could have used a comment to say "Not a problem, ignore it", but that would have given compiler warnings about an empty catch.

    Another example:

    Code:
    Select weekday
      
    Case "Monday"
        DoMeetings
      
    Case "Tuesday"
        DoWork
      
    Case "Wednesday"
        DoMeetings
      
    Case "Thursday"
        DoDrinkCoffey
      
    Case "Friday"
        DoWatchClock
      
    Case "Saturday"
        NOP
      
    Case "Sunday"
        NOP
    End Select
    In this example, I explicitly show that nothing is to be done on weekends, so that the next guy who comes along don't look at the code and thinks "Hmm, he's missed a couple of days, I'll better fix it...".
     
  2. NJDude

    NJDude Expert Licensed User

    You could do this:
    Code:
    Try
      
    'Code which may fail, but I know why it fails and it can safely be ignored
    Catch
      
    Log("Nothing to say here, it just failed")
    End Try
     
    thedesolatesoul and moster67 like this.
  3. Troberg

    Troberg Well-Known Member Licensed User

    I could, but that would compile to something. NOP doesn't generate any compiled code at all.
     
  4. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Then you can do:
    Code:
    Try
      
    'Code which may fail, but I know why it fails and it can safely be ignored
    Catch
      
    'Do nothing on purpose
    End Try
     
  5. Troberg

    Troberg Well-Known Member Licensed User

    I could, but that will give a compiler warning.

    And, just to preempt the next obvious solution:

    No, creating this sub will not work, as it will also generate some code:

    Sub NOP()
    End Sub
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You can add an ignore comment:
    Code:
    End Try 'ignore (I know what I'm doing)
     
  7. Troberg

    Troberg Well-Known Member Licensed User

    Will ignore suppress the warning?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

  9. sorex

    sorex Expert Licensed User

    actually that's wrong.

    nop's are actually a beloved command on some systems.

    let's say the Commodore 64.

    1.
    most of the fancy VIC stuff (rasterbars, vsp, multiplexer etc) rely on strict timing.

    NOPs are wasting 2 cycles while it only requires 1 byte of memory.

    so when you have 1 cycle too much or less you could see it as a tiny line the size of 1 or more characters or have other weird results.
    so it's finding the right match of NOPs and other 3 or more cycle commands to get the timing right.

    2.
    assembler allows us to do self modifying code. you can abuse a nop to overwrite/disable existing commands or the other way around
    and replace the NOPs with other code altho there are shorter alternatives like replacing a JSR with an LDA (1 replacement instead of 3).
     
    NJDude and thedesolatesoul like this.
  10. Troberg

    Troberg Well-Known Member Licensed User

    Yes, in assembler, it had those uses.

    Today, however, only #2 applies, as you can't rely on timing with modern CPU's, which has instruction cache, execution queues, predictive branching and stuff like that, and also lives in an environment where they can't simply read memory, as some other component (other CPU/CPU core, GPU, PCI bus controller et cetera) also access the memory bus.

    Even #1 does only apply partially, as most (PC) CPU's have prevention against modifying running code. It's still useful for "commenting out" blocks of code, when you don't want to move things around in memory (sometimes an issue on microcontrollers or when you have programs residing on several ROMs/EPROMs, and only want to change one).

    However, in high level languages, it's more of a formalized comment stating "I'm intentionally not doing anything here!".
     
  11. sorex

    sorex Expert Licensed User

    didn't know they added that to non ASM languages aswell.

    I would expect just a comment line instead or a multiline IF/THEN that doesn't expect a command after the THEN.
     
  12. Troberg

    Troberg Well-Known Member Licensed User

    I know that at least Rexx has it, and I I've seen it in a couple more. Python has it, but calls it Pass, Ada has it, but calls it Null.

    A sligthly related variant exists in Perl, which has ..., which is used to indicate that code is to be added later, sort of a Todo.
     
  13. sorex

    sorex Expert Licensed User

    shouldn't be a problem to add that to the keyword list and mark it as none operative command I guess.

    most ideal would be adding it to the bookmark list that your eye catches it.
     
  14. thedesolatesoul

    thedesolatesoul Expert Licensed User

    In Python it is a little different as the code won't even compile with an empty construct for e.g. a class def. Therefore pass is REQUIRED.
    Edit: My bad, Python doesn't compile, it just runs.
     
    Last edited: Jun 5, 2015
    NJDude likes this.
Loading...