diff options
author | Eldred Habert <eldredhabert0@gmail.com> | 2019-02-18 20:48:33 +0100 |
---|---|---|
committer | Eldred Habert <eldredhabert0@gmail.com> | 2019-02-18 20:48:33 +0100 |
commit | 6636c4b8f3a867e5b3cb4e496148c82a2053605a (patch) | |
tree | a05f801a08d1160f60ffefef09e527b65ba42ae8 /Optimizing-assembly-code.md | |
parent | f2d265e7ddf020bc793f3ccd3d3f22bded4f3678 (diff) |
Add explanation about 16-bit inc/dec
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r-- | Optimizing-assembly-code.md | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 35fa835..d5f60c5 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -11,12 +11,13 @@ Most of these tricks come from either [Jeff's GB Assembly Code Tips v1.0](http:/ - [Invert the bits of `a`](#invert-the-bits-of-a) - [Multiply `hl` by 2](#multiply-hl-by-2) - [Add `a` to a 16-bit register](#add-a-to-a-16-bit-register) - - [Loading from an address to `hl`](#loading-from-an-address-to-hl) - - [Exchanging two 16-bit registers](#exchanging-two-16-bit-registers) - - [Loading a constant into `[hl]`](#loading-a-constant-into-hl) - - [Loading two constants into a register pair](#loading-two-constants-into-a-register-pair) - - [Loading a constant into `[hl]` and incrementing or decrementing `hl`](#loading-a-constant-into-hl-and-incrementing-or-decrementing-hl) - - [Incrementing or decrementing `[hl]`](#incrementing-or-decrementing-hl) + - [Increment a 16-bit register](#increment-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) + - [Load a constant into `[hl]`](#load-a-constant-into-hl) + - [Load two constants into a register pair](#load-two-constants-into-a-register-pair) + - [Load a constant into `[hl]` and increment or decrement `hl`](#load-a-constant-into-hl-and-increment-or-decrement-hl) + - [Increment or decrement `[hl]`](#increment-or-decrement-hl) - [Branching (control flow)](#branching-control-flow) - [Relative jumps](#relative-jumps) - [Compare `a` to 0](#compare-a-to-0) @@ -25,11 +26,11 @@ Most of these tricks come from either [Jeff's GB Assembly Code Tips v1.0](http:/ - [Add `a` to `hl` without using a 16-bit register](#add-a-to-hl-without-using-a-16-bit-register) - [Subroutines (functions)](#subroutines-functions) - [Tail call optimization](#tail-call-optimization) - - [Calling `hl`](#calling-hl) + - [Call `hl`](#call-hl) - [Inlining](#inlining) - [Fallthrough](#fallthrough) - [Jump and lookup tables](#jump-and-lookup-tables) - - [Chaining comparisons](#chaining-comparisons) + - [Chain comparisons](#chaining-comparisons) ## Registers @@ -170,7 +171,24 @@ Or better (doesn't require a label): ``` -### Loading from an address to `hl` +### Increment / decrement a 16-bit register + +When possible, avoid doing: + +```asm + inc hl ; 1 byte, 2 cycles +``` + +If you can ensure that the low byte won't overflow (or that it won't matter if it does), then do this: + +```asm + inc l ; 1 byte, 1 cycle +``` + +Further, if you intend your code to run on DMG (black & white GB), avoiding 16-bit inc/dec means less occasions to trigger the OAM corruption bug. + + +### Load from an address to `hl` Don't do: @@ -193,7 +211,7 @@ But do: ``` -### Exchanging two 16-bit registers +### Exchange two 16-bit registers (The example uses `hl` and `de`, but any pair of `bc`, `de`, or `hl` would also work.) @@ -220,7 +238,7 @@ If you care about size: ``` -### Loading a constant into `[hl]` +### Load a constant into `[hl]` Don't do: @@ -238,7 +256,7 @@ But do: ``` -### Loading two constants into a register pair +### Load two constants into a register pair (The example uses `bc`, but `hl` or `de` would also work.) @@ -265,7 +283,7 @@ Or better, use the `lb` macro in [macros/code.asm](../blob/master/macros/code.as ``` -### Loading a constant into `[hl]` and incrementing or decrementing `hl` +### Load a constant into `[hl]` and incrementing or decrementing `hl` Don't do: @@ -298,7 +316,7 @@ But do: ``` -### Incrementing or decrementing `[hl]` +### Increment or decrement `[hl]` Don't do: @@ -477,7 +495,7 @@ But do: ``` -### Calling `hl` +### Call `hl` ```asm ; 5 bytes, 8 cycles @@ -558,7 +576,7 @@ You can still `call Function` elsewhere, but one tail call can be optimized into ## Jump and lookup tables -### Chaining comparisons +### Chain comparisons Don't do: |