diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2019-11-13 18:36:50 -0500 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2019-11-13 18:36:50 -0500 |
commit | 436a77fbb00e674bb7a2ef2bb3c7e9642c40ea14 (patch) | |
tree | 3b30581df06c8ec059064adb05642018c705a30c /Optimizing-assembly-code.md | |
parent | 55a2c7ffd2bbb6b8ee30610cacd4faa357d223d8 (diff) |
More optimal code
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r-- | Optimizing-assembly-code.md | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 80d83a6..325798a 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -11,6 +11,7 @@ Most of these tricks come from either [Jeff's GB Assembly Code Tips v1.0](http:/ - [Set `a` to some constant minus `a`](#set-a-to-some-constant-minus-a) - [Multiply `hl` by 2](#multiply-hl-by-2) - [Add `a` to a 16-bit register](#add-a-to-a-16-bit-register) + - [Add `a` to an address](#add-a-to-an-address) - [Increment or decrement a 16-bit register](#increment-or-decrement-a-16-bit-register) - [Load from an address to `hl`](#load-from-an-address-to-hl) - [Exchange two 16-bit registers](#exchange-two-16-bit-registers) @@ -118,6 +119,7 @@ But do: (The `SpeciesItemBoost` routine in [engine/battle/effect_commands.asm](../../blob/master/engine/battle/effect_commands.asm#L2812-L2814) actually does this!) + ### Add `a` to a 16-bit register (The example uses `hl`, but `bc` or `de` would also work.) @@ -176,6 +178,31 @@ Or if you can spare another 16-bit register and want to optimize for size over s add hl, de ``` +### Add `a` to an address + +(The example uses `hl`, but `bc` or `de` would also work.) + +Don't do: + +```asm + ; 7 bytes, 8 cycles; uses another 16-bit register + ld e, a + ld d, 0 + ld hl, Address + add hl, de +``` + +But do: + +```asm + ; 7 bytes, 7 cycles + add a, LOW(Address) + ld l, a + adc a, HIGH(Address) + sub l + ld h, a +``` + ### Increment or decrement a 16-bit register |