diff options
author | Rangi <remy.oukaour+rangi42@gmail.com> | 2021-04-23 09:07:20 -0400 |
---|---|---|
committer | Rangi <remy.oukaour+rangi42@gmail.com> | 2021-04-23 09:07:20 -0400 |
commit | a5da6fa906c87c1f9e6c879312d291ba7a192e03 (patch) | |
tree | 07a4d13c47e2e3cc4ef4d977072f7d80c3926b7b | |
parent | 80943da9cf684a9152ad28537b612f20037c2680 (diff) |
Optimize 'sub hl, de'
-rw-r--r-- | Optimizing-assembly-code.md | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 7aed42f..0c5e733 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -1,6 +1,6 @@ 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 [Jeff's GB Assembly Code Tips v1.0](http://www.devrs.com/gb/files/asmtips.txt), [WikiTI's Z80 Optimization page](http://wikiti.brandonw.net/index.php?title=Z80_Optimization), and [z80 Heaven's optimization tutorial](http://z80-heaven.wikidot.com/optimization). (Note that Z80 assembly is *not* the same as GBZ80; it has more registers and some different instructions.) +Most of these tricks come from [Jeff's GB Assembly Code Tips v1.0](http://www.devrs.com/gb/files/asmtips.txt), [WikiTI's Z80 Optimization page](http://wikiti.brandonw.net/index.php?title=Z80_Optimization), [z80 Heaven's optimization tutorial](http://z80-heaven.wikidot.com/optimization), and [GBDev Wiki's ASM Snippets](https://gbdev.gg8.se/wiki/articles/ASM_Snippets). (Note that Z80 assembly is *not* the same as GBZ80; it has more registers and some different instructions.) WikiTI's advice fully applies here: @@ -41,6 +41,7 @@ WikiTI's advice fully applies here: - [Add or subtract the carry flag from a 16-bit register](#add-or-subtract-the-carry-flag-from-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) + - [Subtract two 16-bit registers](#subtract-two-16-bit-registers) - [Load two constants into a register pair](#load-two-constants-into-a-register-pair) - [Load a constant into `[hl]`](#load-a-constant-into-hl) - [Increment or decrement `[hl]`](#increment-or-decrement-hl) @@ -1258,6 +1259,49 @@ If you care about size: ``` +### Subtract two 16-bit registers + +(The example uses `hl` and `de`, but any pair of `bc`, `de`, or `hl` would also work.) + +Don't do: + +```asm + ; 9 bytes, 10 cycles, modifies subtrahend de + ld a, $ff + xor d + ld d, a + ld a, $ff + xor e + ld e, a + add hl, de +``` + +And don't do: + +```asm + ; 7 bytes, 8 cycles, modifies subtrahend de + ld a, d + cpl + ld d, a + ld a, e + cpl + ld e, a + add hl, de +``` + +But do: + +```asm + ; 6 bytes, 6 cycles + ld a, l + sub e + ld l, a + ld a, h + sbc d + ld h, a +``` + + ### Load two constants into a register pair (The example uses `bc`, but `hl` or `de` would also work.) |