diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2020-03-28 14:22:55 -0400 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2020-03-28 14:22:55 -0400 |
commit | 4b6f890e45b709c7c4c4bb911c0f16489f26ef3a (patch) | |
tree | 559e27e4360d4c8c61b1347184405dfd0aa01f20 /Optimizing-assembly-code.md | |
parent | 8fbb766e1c90ebe6a10e529746aa32d54aba4d58 (diff) |
opt
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r-- | Optimizing-assembly-code.md | 125 |
1 files changed, 123 insertions, 2 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index be46820..a608c15 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -15,14 +15,17 @@ WikiTI's advice fully applies here: ## Contents -- [Registers](#registers) +- [8-bit registers](#8-bit-registers) - [Set `a` to 0](#set-a-to-0) - [Increment or decrement `a`](#increment-or-decrement-a) - [Invert the bits of `a`](#invert-the-bits-of-a) + - [Rotate the bits of `a`](#rotate-the-bits-of-a) - [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) - [Shift `a` right by 3 bits](#shift-a-right-by-3-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) - [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) @@ -38,17 +41,19 @@ WikiTI's advice fully applies here: - [Compare `a` to 0](#compare-a-to-0) - [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) - [Subroutines (functions)](#subroutines-functions) - [Tail call optimization](#tail-call-optimization) - [Call `hl`](#call-hl) - [Inlining](#inlining) - [Fallthrough](#fallthrough) + - [Conditional fallthrough](#conditional-fallthrough) - [Call `rst $38` depending on a flag](#call-rst-38-depending-on-a-flag) - [Jump and lookup tables](#jump-and-lookup-tables) - [Chain comparisons](#chain-comparisons) -## Registers +## 8-bit registers ### Set `a` to 0 @@ -119,6 +124,47 @@ But do: ``` +### Rotate the bits of `a` + +Don't do: + +```asm + rl a ; 2 bytes, 2 cycles +``` + +```asm + rlc a ; 2 bytes, 2 cycles +``` + +```asm + rr a ; 2 bytes, 2 cycles +``` + +```asm + rrc a ; 2 bytes, 2 cycles +``` + +But do: + +```asm + rla ; 1 byte, 1 cycle +``` + +```asm + rlca ; 1 byte, 1 cycle +``` + +```asm + rra ; 1 byte, 1 cycle +``` + +```asm + rrca ; 1 byte, 1 cycle +``` + +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. + + ### Set `a` to some constant minus `a` Don't do: @@ -317,6 +363,32 @@ But do: ``` +### Load from HRAM to `a` or from `a` to HRAM + +Don't do: + +```asm + ld a, [hFoo] ; 3 bytes, 4 cycles +``` + +```asm + ld [hFoo], a ; 3 bytes, 4 cycles +``` + +But do: + +```asm + ldh a, [hFoo] ; 2 bytes, 3 cycles +``` + +```asm + ldh [hFoo], a ; 2 bytes, 3 cycles +``` + + +## 16-bit registers + + ### Multiply `hl` by 2 Don't do: @@ -711,6 +783,23 @@ with: ``` +### Compare `a` to 0 after masking it + +Don't do: + +```asm + ; 3 bytes, 3 cycles; sets zero flag if a == 0 + and MASK + and a +``` + +But do: + +```asm + and MASK ; 2 bytes, 2 cycles; sets zero flag if a == 0 +``` + + ## Subroutines (functions) @@ -826,6 +915,38 @@ Function: You can still `call Function` elsewhere, but one tail call can be optimized into a fallthrough. +## Conditional fallthrough + +(The example uses `z`, but `nz`, `c`, or `nc` would also work.) + +Don't do: + +```asm + (some code) + jr z, .foo + jr .bar + +.foo + (foo code) + +.bar + (bar code) +``` + +But do: + +```asm + (some code) + jr nz, .bar + ; fallthrough +.foo + (foo code) + +.bar + (bar code) +``` + + ### Call `rst $38` depending on a flag (The example uses `z`, but `nz`, `c`, or `nc` would also work.) |