diff options
-rw-r--r-- | Optimizing-assembly-code.md | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md index 2f9ab4c..be46820 100644 --- a/Optimizing-assembly-code.md +++ b/Optimizing-assembly-code.md @@ -17,6 +17,7 @@ WikiTI's advice fully applies here: - [Registers](#registers) - [Set `a` to 0](#set-a-to-0) + - [Increment or decrement `a`](#increment-or-decrement-a) - [Invert the bits of `a`](#invert-the-bits-of-a) - [Set `a` to some constant minus `a`](#set-a-to-some-constant-minus-a) - [Set `a` to one constant or another depending on the carry flag](#set-a-to-one-constant-or-another-depending-on-the-carry-flag) @@ -80,6 +81,29 @@ Don't use the optimized versions if you need to preserve flags. As such, `ld a, ``` +### Increment or decrement `a` + +When possible, avoid doing: + +```asm + add 1 ; 2 bytes, 2 cycles; sets carry for -1 to 0 overflow +``` + +```asm + sub 1 ; 2 bytes, 2 cycles; sets carry for 0 to -1 underflow +``` + +If you don't need to set the carry flag, then do: + +```asm + inc a ; 1 byte, 1 cycle +``` + +```asm + dec a ; 1 byte, 1 cycle +``` + + ### Invert the bits of `a` Don't do: @@ -202,7 +226,7 @@ If `FOO` equals `BAR - 2`, then do: sbc -BAR ; -1 becomes BAR - 2 aka FOO, 0 becomes BAR ``` -If `FOO` is 0 and `BAR` is 0 (i.e. set `a` to 0 if carry or 1 if not carry), then do: +If `FOO` is 0 and `BAR` is 1 (i.e. set `a` to 0 if carry, or 1 if not carry), then do: ```asm ; 2 bytes, 2 cycles |