summaryrefslogtreecommitdiff
path: root/Optimizing-assembly-code.md
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2019-02-19 18:56:20 -0500
committerRangi <remy.oukaour+rangi42@gmail.com>2019-02-19 18:56:20 -0500
commitae496762561b9b702311549b713b5ac5f405d178 (patch)
tree8276e5096229b4754b2afe2d45edd1b947a71d23 /Optimizing-assembly-code.md
parent0c58b0eb1abf270e641371ce30df8e9f8d200170 (diff)
Formatting
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r--Optimizing-assembly-code.md60
1 files changed, 31 insertions, 29 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md
index 61f3873..da5cc4f 100644
--- a/Optimizing-assembly-code.md
+++ b/Optimizing-assembly-code.md
@@ -49,7 +49,7 @@ But do:
xor a ; 1 byte, 1 cycle, sets flags C to 0 and Z to 1
```
-or do:
+Or do:
```asm
sub a ; 1 byte, 1 cycle, sets flags C to 0 and Z to 1
@@ -59,7 +59,7 @@ Don't use the optimized versions if you need to preserve flags. As such, `ld a,
```asm
ld a, [wIsTrainerBattle]
- and a ; NZ if in trainer battle
+ and a ; NZ if [wIsTrainerBattle] is nonzero
ld a, 0
jr nz, .trainer
```
@@ -113,8 +113,7 @@ Don't do:
But do:
```asm
- ; 1 byte, 2 cycles
- add hl, hl
+ add hl, hl ; 1 byte, 2 cycles
```
(The `SpeciesItemBoost` routine in [engine/battle/effect_commands.asm](../blob/master/engine/battle/effect_commands.asm) actually does this!)
@@ -267,15 +266,13 @@ Don't do:
But do:
```asm
- ; 3 bytes, 3 cycles
- ld bc, ONE << 8 | TWO
+ ld bc, ONE << 8 | TWO ; 3 bytes, 3 cycles
```
Or better, use the `lb` macro in [macros/code.asm](../blob/master/macros/code.asm):
```asm
- ; 3 bytes, 3 cycles
- lb bc, ONE, TWO
+ lb bc, ONE, TWO ; 3 bytes, 3 cycles
```
@@ -292,8 +289,7 @@ Don't do:
But do:
```asm
- ; 2 bytes, 3 cycles
- ld [hl], CONST
+ ld [hl], CONST ; 2 bytes, 3 cycles
```
@@ -318,13 +314,11 @@ Don't do:
But do:
```asm
- ; 1 bytes, 3 cycles
- inc [hl]
+ inc [hl] ; 1 bytes, 3 cycles
```
```asm
- ; 1 bytes, 3 cycles
- dec [hl]
+ dec [hl] ; 1 bytes, 3 cycles
```
@@ -347,13 +341,11 @@ Don't do:
But do:
```asm
- ; 1 bytes, 2 cycles
- ld [hli], a
+ ld [hli], a ; 1 bytes, 2 cycles
```
```asm
- ; 1 bytes, 2 cycles
- ld [hld], a
+ ld [hld], a ; 1 bytes, 2 cycles
```
@@ -391,7 +383,7 @@ But do:
or a ; 1 byte, 1 cycle
```
-or do:
+Or do:
```asm
and a ; 1 byte, 1 cycle
@@ -411,7 +403,7 @@ If you don't care about the value in `a`:
dec a ; 1 byte, 1 cycle, decrements a
```
-Note that you can still do `inc a` afterwards, which is 1 cycle faster if the jump is taken. Compare:
+Note that you can still do `inc a` afterwards, which is one cycle faster if the jump is taken. Compare:
```asm
cp 1
@@ -438,10 +430,10 @@ with:
If you don't care about the value in `a`:
```asm
- dec a ; 1 byte, 1 cycle, decrements a
+ inc a ; 1 byte, 1 cycle, increments a
```
-Note that you can still do `inc a` afterwards, which is 1 cycle faster if the jump is taken. Compare:
+Note that you can still do `dec a` afterwards, which is one cycle faster if the jump is taken. Compare:
```asm
cp $ff
@@ -473,8 +465,7 @@ Don't do:
But do:
```asm
- ; 3 bytes, 4 cycles
- jp Function
+ jp Function ; 3 bytes, 4 cycles
```
@@ -493,9 +484,7 @@ But do:
But do:
```asm
- ; 4 bytes, 7 cycles
- call _hl_
-; return
+ call _hl_ ; 4 bytes, 7 cycles, counting the definition of _hl_
...
```
@@ -541,7 +530,19 @@ Don't do:
ret
Function:
+ (some code)
+ ret
+```
+
+And don't do:
+
+```asm
...
+ jp Function
+
+Function:
+ (some code)
+ ret
```
But do:
@@ -550,7 +551,8 @@ But do:
...
; fallthrough
Function:
- ...
+ (some code)
+ ret
```
You can still `call Function` elsewhere, but one tail call can be optimized into a fallthrough.
@@ -619,4 +621,4 @@ Or better, do:
dw .equals2
dw .equals3
...
-``` \ No newline at end of file
+```