diff options
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r-- | Optimizing-assembly-code.md | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 893ade2..27c9433 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -28,7 +28,8 @@ WikiTI's advice fully applies here: - [Increment or decrement `a` when the carry flag is set](#increment-or-decrement-a-when-the-carry-flag-is-set) - [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) + - [Set `a` to some value plus or minus carry](#set-a-to-some-value-plus-or-minus-carry) + - [Add or subtract the carry flag from a register besides `a`](#add-or-subtract-the-carry-flag-from-a-register-besides-a) - [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) - [Multiply `hl` by 2](#multiply-hl-by-2) @@ -616,7 +617,7 @@ But do: ``` -### Set `a` to some value plus carry +### Set `a` to some value plus or minus carry (The example uses `b` and `c`, but any registers besides `a` would also work, including `[hl]`.) @@ -629,6 +630,13 @@ Don't do: adc 0 ``` +```asm + ; 4 bytes, 4 cycles + ld b, a + ld a, c + sbc 0 +``` + And don't do: ```asm @@ -638,6 +646,13 @@ And don't do: adc c ``` +```asm + ; 4 bytes, 4 cycles + ld b, a + ld a, 0 + sbc c +``` + But do: ```asm @@ -647,6 +662,13 @@ But do: sub b ``` +```asm + ; 3 bytes, 3 cycles + ld b, a + sbc c + add b +``` + Also, don't do: ```asm @@ -656,6 +678,13 @@ Also, don't do: adc 0 ``` +```asm + ; 5 bytes, 5 cycles + ld b, a + ld a, N + sbc 0 +``` + And don't do: ```asm @@ -665,6 +694,13 @@ And don't do: adc N ``` +```asm + ; 5 bytes, 5 cycles + ld b, a + ld a, 0 + sbc N +``` + But do: ```asm @@ -674,6 +710,52 @@ But do: sub b ``` +```asm + ; 4 bytes, 4 cycles + ld b, a + sbc N + add b +``` + +(If the original value of `a` was not backed up in `b`, this optimization would not apply.) + + +### Add or subtract the carry flag from a register besides `a` + +(The example uses `b`, but any of `c`, `d`, `e`, `h`, or `l` would also work.) + +Don't do: + +```asm + ; 4 bytes, 4 cycles + ld a, b + adc 0 + ld b, a +``` + +```asm + ; 4 bytes, 4 cycles + ld a, b + sbc 0 + ld b, a +``` + +But do: + +```asm + ; 3 bytes, 3 or 4 cycles + jr nc, .no_carry + inc b +.no_carry +``` + +```asm + ; 3 bytes, 3 or 4 cycles + jr nc, .no_carry + dec b +.no_carry +``` + ### Load from HRAM to `a` or from `a` to HRAM |