Bregalad on the Nesdev forums just informed me of a trick for optimizing if-then-else type blocks, where the "else" area consists of a 2-byte instruction.
So you normally have an if-else-endif block like this:
Code: Select all
jr nz,else ;the IF
;some code
jr endif
else:
;some code
endif:
But here's a crazy trick for when the Else code is a single 2-byte instruction:
You use the first byte of a 3 byte instruction with no side effects instead of the "jr endif" line!
So if you had code like this:
Code: Select all
cp 7
jr nz,else
ld a,3
jr endif
else:
ld a,4
endif:
You could replace it with this:
Code: Select all
cp 7
jr nz,else
ld a,3
.db $C2 ;jp nz,xxxx
else:
ld a,4
endif:
Instead of branching over the ld a,4 instruction, it now executes a jp nz,XXXX instruction where the XXXX is the two bytes of the next instruction. You already know what the flags will be here, so you can make the jump never taken. You can use this to skip the next two bytes of execution! Who needs to branch over it?
You know your hexadecimal output routine is broken when it displays the character 'G'.