summaryrefslogtreecommitdiff
path: root/Optimizing-assembly-code.md
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2020-06-22 09:25:03 -0400
committerRangi <remy.oukaour+rangi42@gmail.com>2020-06-22 09:25:03 -0400
commit44c162625876ecfe2bca02a1e5944719469a37f0 (patch)
tree0dafa6f3b011967ad4b14f1de6b11633243bfbc4 /Optimizing-assembly-code.md
parentf6519b58b337c9ddbff7acb6b16b072d8228495d (diff)
a / 16
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r--Optimizing-assembly-code.md32
1 files changed, 32 insertions, 0 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md
index 7d7afd9..3dbc306 100644
--- a/Optimizing-assembly-code.md
+++ b/Optimizing-assembly-code.md
@@ -23,6 +23,7 @@ WikiTI's advice fully applies here:
- [Set `a` to some constant minus `a`](#set-a-to-some-constant-minus-a)
- [Set `a` to one constant or another depending on the carry flag](#set-a-to-one-constant-or-another-depending-on-the-carry-flag)
- [Divide `a` by 8 (shift `a` right 3 bits)](#divide-a-by-8-shift-a-right-3-bits)
+ - [Divide `a` by 16 (shift `a` right 4 bits)](#divide-a-by-16-shift-a-right-4-bits)
- [Set `a` to some value plus carry](#set-a-to-some-value-plus-carry)
- [Load from HRAM to `a` or from `a` to HRAM](#load-from-hram-to-a-or-from-a-to-hram)
- [16-bit registers](#16-bit-registers)
@@ -331,6 +332,37 @@ But do:
```
+### Divide `a` by 16 (shift `a` right 4 bits)
+
+Don't do:
+
+```asm
+ ; 6 bytes, 9 cycles
+ ; (15 bytes, at least 21 cycles, counting the definition of SimpleDivide)
+ ld c, 16 ; divisor
+ call SimpleDivide
+ ld a, b ; quotient
+```
+
+And don't do:
+
+```asm
+ ; 8 bytes, 8 cycles
+ srl a
+ srl a
+ srl a
+ srl a
+```
+
+But do:
+
+```asm
+ ; 4 bytes, 4 cycles
+ swap a
+ and $f
+```
+
+
### Set `a` to some value plus carry
(The example uses `b` and `c`, but any registers besides `a` would also work, including `[hl]`.)