diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2020-08-29 16:52:44 -0400 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2020-08-29 16:52:44 -0400 |
commit | 0547c45c434ee0b16bfe8c51a92225062ec4eb31 (patch) | |
tree | 450f0785ee8170588d9fdb0647e7ba6fe4363b8d /Optimizing-assembly-code.md | |
parent | 0d81816cab9d4210bd4f55211ed6026c3f6a786e (diff) |
hl = a * 16
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r-- | Optimizing-assembly-code.md | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 8346020..d1dd25f 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -34,6 +34,7 @@ WikiTI's advice fully applies here: - [Add `a` to a 16-bit register](#add-a-to-a-16-bit-register) - [Subtract an 8-bit constant from a 16-bit register](#subtract-an-8-bit-constant-from-a-16-bit-register) - [Add `a` to an address](#add-a-to-an-address) + - [Set a 16-bit register to `a` \* 16](#set-a-16-bit-register-to-a--16) - [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) @@ -631,6 +632,107 @@ But do: ``` +### Set a 16-bit register to `a` \* 16 + +(The example uses `hl`, but `bc` or `de` would also work.) + +You can do: + +``` +; 7 bytes, 11 cycles + ld l, a + ld h, 0 + add hl, hl + add hl, hl + add hl, hl + add hl, hl +``` + +``` +; 7 bytes, 11 cycles + ld l, a + ld h, 0 +rept 4 + add hl, hl +endr +``` + +But if `a` is definitely small enough, and its value can be changed, then do: + +``` +; 7 bytes, 10 cycles; sets a = a * 2; requires a < $80 + add a + ld l, a + ld h, 0 + add hl, hl + add hl, hl + add hl, hl +``` + +``` +; 7 bytes, 9 cycles; sets a = a * 4; requires a < $40 + add a + add a + ld l, a + ld h, 0 + add hl, hl + add hl, hl +``` + +``` +; 7 bytes, 8 cycles; sets a = a * 8; requires a < $20 + add a + add a + add a + ld l, a + ld h, 0 + add hl, hl +``` + +``` +; 7 bytes, 7 cycles; sets a = a * 16; requires a < $10 + add a + add a + add a + add a + ld l, a + ld h, 0 +``` + +``` +; 7 bytes, 7 cycles; sets a = a * 16; requires a < $10 +rept 4 + add a +endr + ld l, a + ld h, 0 +``` + +Or if the value of `a` can be changed and you want to optimize for speed over size, do: + +``` +; 8 bytes, 8 cycles; sets a = l + swap a + ld l, a + and $f + ld h, a + xor l + ld l, a +``` + +Or do: + +``` +; 8 bytes, 8 cycles; sets a = h + swap a + ld h, a + and $f0 + ld l, a + xor h + ld h, a +``` + + ### Increment or decrement a 16-bit register When possible, avoid doing: |