Useless instructions: Difference between revisions

From Rosetta Code
Content added Content deleted
m (don't include NOPs)
(Added Wren)
Line 23: Line 23:
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
<code>xor ax,ax</code> (or any other data register) takes fewer bytes to encode than <code>mov ax,0</code> and achieves the same result. The only difference is that <code>mov ax,0</code> doesn't set the flags, which can be used to the programmer's advantage when sequencing operations.
<code>xor ax,ax</code> (or any other data register) takes fewer bytes to encode than <code>mov ax,0</code> and achieves the same result. The only difference is that <code>mov ax,0</code> doesn't set the flags, which can be used to the programmer's advantage when sequencing operations.

=={{header|Wren}}==
There are no language features or standard library methods in Wren which are completely redundant either by design or due to subsequent developments. However, it's not difficult to think of instances where some of them can used in a redundant way as in the following script.

For good measure I've also included something which looks useless but isn't in fact.
<lang ecmascript>var uselessFunc = Fn.new { |uselessParam| // required but never used
if (true) {
// do something
} else {
System.print("Never called")
}

for (e in []) {
System.print("Never called")
}

while (false) {
System.print("Never called")
}

System.write("") // no visible effect

return // redundant as function would return 'naturally' anyway
}

class NotCompletelyUseless {
/*
On the face of it this class is useless because:
(1) it has no methods
(2) although it inherits from Object, static methods are not inherited

However, it's not in fact completely useless because:
(3) a Class object is still created and accessible
(4) it can be used as an abstract base class for other classes
*/
}

uselessFunc.call(0)
System.print(NotCompletelyUseless) // prints the string representation of the Class object</lang>

{{out}}
<pre>
NotCompletelyUseless
</pre>


=={{header|x86 Assembly}}==
=={{header|x86 Assembly}}==

Revision as of 08:35, 16 October 2021

Useless instructions is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Showcase an instruction or function built into the language, that is made redundant by another instruction also built into the language. It can be limited to a specific way that function is used, e.g. printing a null string, but try not to make it that obvious.

NOP and any instructions or functions that are intended to do nothing do not count.



6502 Assembly

CMP #0 is nearly completely useless, as most 6502 instructions do this implicitly. It can be handy when you need to check equality to zero after a different register was altered, and in that scenario using PHP/PLP to save and restore the condition codes would be impractical, but these situations are few and far between.

<lang 6502asm>myLoop:

do stuff here

dex

cpx #0 ;this isn't needed since the zero flag will be set if x=0

bne myLoop ;if x != 0, jump back to myLoop</lang>

68000 Assembly

CLR.L Dn sets an entire 32-bit register to 0x00000000. However, MOVEQ #0,Dn does the same thing but faster. The only advantage CLR Dn has is that it can directly clear memory locations.

8086 Assembly

xor ax,ax (or any other data register) takes fewer bytes to encode than mov ax,0 and achieves the same result. The only difference is that mov ax,0 doesn't set the flags, which can be used to the programmer's advantage when sequencing operations.

Wren

There are no language features or standard library methods in Wren which are completely redundant either by design or due to subsequent developments. However, it's not difficult to think of instances where some of them can used in a redundant way as in the following script.

For good measure I've also included something which looks useless but isn't in fact. <lang ecmascript>var uselessFunc = Fn.new { |uselessParam| // required but never used

   if (true) {
       // do something
   } else {
       System.print("Never called")
   }
   for (e in []) {
       System.print("Never called")
   }
   while (false) {
       System.print("Never called")
   }
   System.write("")  // no visible effect
   return // redundant as function would return 'naturally' anyway

}

class NotCompletelyUseless {

   /*
      On the face of it this class is useless because:
      (1) it has no methods
      (2) although it inherits from Object, static methods are not inherited
      However, it's not in fact completely useless because:
      (3) a Class object is still created and accessible
      (4) it can be used as an abstract base class for other classes
   */

}

uselessFunc.call(0) System.print(NotCompletelyUseless) // prints the string representation of the Class object</lang>

Output:
NotCompletelyUseless

x86 Assembly

Originally, LOOP label was a one-line version of DEC ECX JNZ label, but even Intel now recommends to use the latter over the former, due to LOOP taking longer to execute than DEC ECX JNZ on the 486 and beyond. Most compilers don't use LOOP anymore either. An explanation can be found here.

Z80 Assembly

xor a is shorter than ld a,0. The latter doesn't clear the zero or carry flags, which is often useful. But if the flags aren't needed for an upcoming branch, call, or return, it's preferred to use xor a.