diff options
-rw-r--r-- | Optimizing-assembly-code.md | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 9ee065a..35fa835 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -1,4 +1,4 @@ -Sometimes the simplest way to write something in assembly code isn't the best. There are optimization techniques to make code smaller and/or faster. +Sometimes the simplest way to write something in assembly code isn't the best. All of your resources are limited: CPU speed, ROM size, RAM space, register use. You can rewrite code to use those resources more efficiently (sometimes by trading one for another). Most of these tricks come from either [Jeff's GB Assembly Code Tips v1.0](http://www.devrs.com/gb/files/asmtips.txt), or [WikiTI's Z80 Optimization page](http://wikiti.brandonw.net/index.php?title=Z80_Optimization). (Note that Z80 assembly is *not* the same as GBZ80; it has more registers and some different instructions.) @@ -11,16 +11,18 @@ 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 offset to `hl`](#loading-from-an-offset-to-hl) + - [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) - [Branching (control flow)](#branching-control-flow) + - [Relative jumps](#relative-jumps) - [Compare `a` to 0](#compare-a-to-0) - [Compare `a` to 1](#compare-a-to-1) - [Compare `a` to 255](#compare-a-to-255) + - [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) @@ -168,15 +170,15 @@ Or better (doesn't require a label): ``` -### Loading from an offset to `hl` +### Loading from an address to `hl` Don't do: ```asm ; 8 bytes, 10 cycles - ld a, [offset] + ld a, [Address] ld l, a - ld a, [offset+1] + ld a, [Address+1] ld h, a ``` @@ -184,7 +186,7 @@ But do: ```asm ; 6 bytes, 8 cycles - ld hl, offset + ld hl, Address ld a, [hli] ld h, [hl] ld l, a @@ -334,6 +336,23 @@ But do: ## Branching (control flow) +### Relative jumps + +Don't do: + +```asm + jp Somewhere ; 3 bytes, 4 cycles +``` + +But do: + +```asm + jr Somewhere ; 2 bytes, 3 cycles +``` + +This only applies if `Somewhere` is within ±127 bytes of the jump. + + ### Compare `a` to 0 Don't do: @@ -414,6 +433,29 @@ with: ``` +### Add `a` to `hl` without using a 16-bit register + +Don't do: + +```asm + ; 4 bytes, 5 cycles + ld d, 0 + ld e, a + add hl, de +``` + +But do: + +```asm + ; 5 bytes, 5 or 6 cycles + add l + ld l, a + jr nc, .no_carry + inc h +.no_carry +``` + + ## Subroutines (functions) |