summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2020-11-29 16:09:52 -0500
committerRangi <remy.oukaour+rangi42@gmail.com>2020-11-29 16:13:06 -0500
commit36dfd67276d0a6519925af21734546be48edad2f (patch)
treebbcbf6d6fb5c795df378ee2c11e393d58ac7d74f
parent4c1cfe6b4802dd01acf750d184c6e96bcd3cc9a0 (diff)
ax6's trick
-rw-r--r--Optimizing-assembly-code.md38
1 files changed, 15 insertions, 23 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md
index 5d80acf..9933b84 100644
--- a/Optimizing-assembly-code.md
+++ b/Optimizing-assembly-code.md
@@ -35,7 +35,6 @@ WikiTI's advice fully applies here:
- [Add `a` to a 16-bit register](#add-a-to-a-16-bit-register)
- [Subtract an 8-bit constant from a 16-bit register](#subtract-an-8-bit-constant-from-a-16-bit-register)
- [Set a 16-bit register to `a` plus a 16-bit constant](#set-a-16-bit-register-to-a-plus-a-16-bit-constant)
- - [Set a 16-bit register to `a` plus a nonzero 8-bit constant](#set-a-16-bit-register-to-a-plus-a-nonzero-8-bit-constant)
- [Set a 16-bit register to `a` \* 16](#set-a-16-bit-register-to-a--16)
- [Increment or decrement a 16-bit register](#increment-or-decrement-a-16-bit-register)
- [Load from an address to `hl`](#load-from-an-address-to-hl)
@@ -679,21 +678,21 @@ But do:
Don't do:
```asm
- ld a, [hFoo] ; 3 bytes, 4 cycles
+ ld a, [hFoobar] ; 3 bytes, 4 cycles
```
```asm
- ld [hFoo], a ; 3 bytes, 4 cycles
+ ld [hFoobar], a ; 3 bytes, 4 cycles
```
But do:
```asm
- ldh a, [hFoo] ; 2 bytes, 3 cycles
+ ldh a, [hFoobar] ; 2 bytes, 3 cycles
```
```asm
- ldh [hFoo], a ; 2 bytes, 3 cycles
+ ldh [hFoobar], a ; 2 bytes, 3 cycles
```
@@ -784,7 +783,7 @@ Don't do:
```asm
; 8 bytes, 8 cycles
ld a, l
- sub FOO
+ sub FOOBAR
ld l, a
ld a, h
sbc 0
@@ -796,7 +795,7 @@ But do:
```asm
; 7 bytes, 7 or 8 cycles
ld a, l
- sub FOO
+ sub FOOBAR
ld l, a
jr nc, .no_carry
dec h
@@ -807,7 +806,7 @@ Or if you can spare another 16-bit register, do:
```asm
; 4 bytes, 5 cycles
- ld de, -FOO
+ ld de, -FOOBAR
add hl, de
```
@@ -822,7 +821,7 @@ Don't do:
; 7 bytes, 8 cycles; uses another 16-bit register
ld e, a
ld d, 0
- ld hl, Address
+ ld hl, FooBar
add hl, de
```
@@ -830,7 +829,7 @@ And don't do:
```asm
; 8 bytes, 8 cycles
- ld hl, Address
+ ld hl, FooBar
add l
ld l, a
adc h
@@ -842,8 +841,8 @@ And don't do:
```asm
; 8 bytes, 7 or 8 cycles
- ld h, HIGH(Address)
- add LOW(Address)
+ ld h, HIGH(FooBar)
+ add LOW(FooBar)
ld l, a
jr nc, .no_carry
inc h
@@ -854,31 +853,24 @@ But do:
```asm
; 7 bytes, 7 cycles
- add LOW(Address)
+ add LOW(FooBar)
ld l, a
- adc HIGH(Address)
+ adc HIGH(FooBar)
sub l
ld h, a
```
-
-### Set a 16-bit register to `a` plus a nonzero 8-bit constant
-
-(The example uses `hl`, but `bc` or `de` would also work.)
-
-Don't do any of [the above](#set-a-16-bit-register-to-a-plus-a-16-bit-constant) methods for a 16-bit constant. Instead, do:
+And if the constant is 8-bit and nonzero (i.e. 0 < `FooBar` < 256), then do:
```asm
; 6 bytes, 6 cycles
- sub -FOO
+ sub LOW(-FooBar)
ld l, a
sbc a
inc a
ld h, a
```
-This will work for any constant `FOO` from 1 to 255, but not 0.
-
### Set a 16-bit register to `a` \* 16