Useless instructions

From Rosetta Code
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.



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.

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.