Difference between revisions of "Z80 Optimization"

From WikiTI
Jump to: navigation, search
(New page: {{stub}} == Introduction == Sometimes it is needed some extra speed in ASM or make your game smaller to fit on the calculator. == General == General algorithm improvements and correct us...)
 
Line 16: Line 16:
 
  sub a    ;disadvantages: changes flags
 
  sub a    ;disadvantages: changes flags
 
  </nowiki>
 
  </nowiki>
 +
 +
== Setting flags ==
 +
In some occassion you might want to selectively set/reset a flag.
 +
 +
Here are the most common uses :
 +
<nowiki>
 +
; set Carry flag
 +
scf
 +
 +
; reset Carry flag (alters Sign and Zero flags as defined)
 +
or a
 +
 +
; alternate reset Carry flag (alters Sign and Zero flags as defined)
 +
and a
 +
 +
; set Zero flag (resets Carry flag, alters Sign flag as defined)
 +
cp a
 +
 +
; reset Zero flag (alters a, reset Carry flag, alters Sign flag as defined)
 +
or 1
 +
 +
; set Sign flag (negative) (alters a, reset Zero and Carry flags)
 +
or $80
 +
 +
; reset Sign flag (positive) (set a to zero, set Zero flag, reset Carry flag)
 +
xor a
 +
</nowiki>
 +
 +
Other possible uses (much rarer) :
 +
<nowiki>
 +
; Set parity/overflow (even):
 +
xor a
 +
 +
Reset parity/overflow (odd):
 +
sub a
 +
 +
; set half carry (hardly ever useful but still...)
 +
and a
 +
 +
; reset half carry (hardly ever useful but still...)
 +
or a
 +
</nowiki>
 +
 +
As you can see these are extremely simple, small and fast ways to alter flags
 +
which make them interesting as output of routines to indicate error/success or
 +
other status bits that do not require a full register.
 +
 +
Were you to use this, remember that these flag (re)setting tricks frequently
 +
overlap so if you need a special combination of flags it might require slightly
 +
more elaborate tricks. As a rule of a thumb, always alter the carry last in
 +
such cases because the scf and ccf instructions do not have side effects.

Revision as of 01:14, 4 November 2009

This article is a stub. You can help WikiTI by expanding it.


Introduction

Sometimes it is needed some extra speed in ASM or make your game smaller to fit on the calculator.

General

General algorithm improvements and correct use of registers.

Small Tricks

;Instead of:
 ld a,0
;Try this:
 xor a    ;disadvantages: changes flags
;or
 sub a    ;disadvantages: changes flags
 

Setting flags

In some occassion you might want to selectively set/reset a flag.

Here are the most common uses :

; set Carry flag
scf

; reset Carry flag (alters Sign and Zero flags as defined)
or a

; alternate reset Carry flag (alters Sign and Zero flags as defined)
and a

; set Zero flag (resets Carry flag, alters Sign flag as defined)
cp a

; reset Zero flag (alters a, reset Carry flag, alters Sign flag as defined)
or 1

; set Sign flag (negative) (alters a, reset Zero and Carry flags)
or $80

; reset Sign flag (positive) (set a to zero, set Zero flag, reset Carry flag)
xor a
 

Other possible uses (much rarer) :

; Set parity/overflow (even):
xor a

Reset parity/overflow (odd):
sub a

; set half carry (hardly ever useful but still...)
and a

; reset half carry (hardly ever useful but still...)
or a
 

As you can see these are extremely simple, small and fast ways to alter flags which make them interesting as output of routines to indicate error/success or other status bits that do not require a full register.

Were you to use this, remember that these flag (re)setting tricks frequently overlap so if you need a special combination of flags it might require slightly more elaborate tricks. As a rule of a thumb, always alter the carry last in such cases because the scf and ccf instructions do not have side effects.