summaryrefslogtreecommitdiff
path: root/Optimizing-assembly-code.md
diff options
context:
space:
mode:
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r--Optimizing-assembly-code.md36
1 files changed, 28 insertions, 8 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md
index a4a1347..9b28f12 100644
--- a/Optimizing-assembly-code.md
+++ b/Optimizing-assembly-code.md
@@ -46,6 +46,7 @@ WikiTI's advice fully applies here:
- [Compare `a` to 1](#compare-a-to-1)
- [Compare `a` to 255](#compare-a-to-255)
- [Compare `a` to 0 after masking it](#compare-a-to-0-after-masking-it)
+ - [Test whether `a` is negative (compare `a` to $80)](#test-whether-a-is-negative-compare-a-to-80)
- [Subroutines (functions)](#subroutines-functions)
- [Tail call optimization](#tail-call-optimization)
- [Call `hl`](#call-hl)
@@ -133,37 +134,37 @@ But do:
Don't do:
```asm
- rl a ; 2 bytes, 2 cycles
+ rl a ; 2 bytes, 2 cycles; updates Z and C flags
```
```asm
- rlc a ; 2 bytes, 2 cycles
+ rlc a ; 2 bytes, 2 cycles; updates Z and C flags
```
```asm
- rr a ; 2 bytes, 2 cycles
+ rr a ; 2 bytes, 2 cycles; updates Z and C flags
```
```asm
- rrc a ; 2 bytes, 2 cycles
+ rrc a ; 2 bytes, 2 cycles; updates Z and C flags
```
But do:
```asm
- rla ; 1 byte, 1 cycle
+ rla ; 1 byte, 1 cycle; updates C flag
```
```asm
- rlca ; 1 byte, 1 cycle
+ rlca ; 1 byte, 1 cycle; updates C flag
```
```asm
- rra ; 1 byte, 1 cycle
+ rra ; 1 byte, 1 cycle; updates C flag
```
```asm
- rrca ; 1 byte, 1 cycle
+ rrca ; 1 byte, 1 cycle; updates C flag
```
The exception is if you need to set the zero flag when the operation results in 0 for `a`; the two-byte operations can set `z`, the one-byte operations cannot.
@@ -912,6 +913,25 @@ But do:
```
+### Test whether `a` is negative (compare `a` to $80)
+
+If you don't need to preserve the value in `a`, then don't do:
+
+```
+ ; 4 bytes, 4/5 cycles
+ cp $80 ; high bit means negative
+ jr nc, .negative
+```
+
+Instead, do:
+
+```
+ ; 3 bytes, 3/4 cycles
+ rlca ; high bit means negative
+ jr c, .negative
+```
+
+
## Subroutines (functions)