Wish NOP instruction

Troberg

Well-Known Member
Licensed User
Longtime 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:

B4X:
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:

B4X:
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...".
 

Troberg

Well-Known Member
Licensed User
Longtime 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
 

sorex

Expert
Licensed User
Longtime 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?

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).
 

Troberg

Well-Known Member
Licensed User
Longtime 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!".
 

sorex

Expert
Licensed User
Longtime 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.
 

Troberg

Well-Known Member
Licensed User
Longtime 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.
 

sorex

Expert
Licensed User
Longtime 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.
 
Top