summaryrefslogtreecommitdiff
path: root/Optimizing-assembly-code.md
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2020-11-24 16:41:03 -0500
committerRangi <remy.oukaour+rangi42@gmail.com>2020-11-24 16:57:45 -0500
commit3fb2506a3c81321b8f49ce73f2e9a66a4ee9e22f (patch)
tree2d128ac990a47f52aecf76799529765ed8c9c98b /Optimizing-assembly-code.md
parent49d87bf757b0148e6996080f824d0ce544c70e5a (diff)
carry table
Diffstat (limited to 'Optimizing-assembly-code.md')
-rw-r--r--Optimizing-assembly-code.md144
1 files changed, 122 insertions, 22 deletions
diff --git a/Optimizing-assembly-code.md b/Optimizing-assembly-code.md
index 4b191db..dae62eb 100644
--- a/Optimizing-assembly-code.md
+++ b/Optimizing-assembly-code.md
@@ -256,41 +256,84 @@ Or do:
xor NCVAL ; CVAL ^ NCVAL becomes CVAL, 0 becomes NCVAL
```
-If `NCVAL` is 0 (i.e. set `a` to 0 if not carry), then do:
+And if certain conditions apply, then do something more efficient:
+
+<table>
+
+<tr>
+<th>If this...</th>
+<th>...then do:</th>
+</tr>
+
+<tr><td>
+
+`CVAL` == $FF (aka −1) <br>and<br> `NCVAL` == 0
+
+</td><td>
```asm
- ; 3 bytes, 3 cycles
- sbc a ; if carry, then $ff, else 0
- and CVAL ; $ff becomes CVAL, 0 stays 0
+ ; 1 byte, 1 cycle
+ sbc a ; if carry, then $ff, else 0
```
-If `CVAL` is 0 (i.e. set `a` to 0 if carry), invert carry and do the same:
+</td></tr>
+<tr><td>
+
+`CVAL` == 0 <br>and<br> `NCVAL` == $FF (aka −1)
+
+</td><td>
```asm
- ; 4 bytes, 4 cycles
- ccf ; invert carry flag
- sbc a ; if originally carry, then 0, else $ff
- and NCVAL ; 0 stays 0, $ff becomes NCVAL
+ ; 2 bytes, 2 cycles
+ ccf ; invert carry flag
+ sbc a ; if originally carry, then 0, else $ff
+```
+
+</td></tr>
+<tr><td>
+
+`CVAL` == 0 <br>and<br> `NCVAL` == 1
+
+</td><td>
+
+```asm
+ ; 2 bytes, 2 cycles
+ sbc a ; if carry, then $ff aka -1, else 0
+ inc a ; -1 becomes 0, 0 becomes 1
```
-If `CVAL` is $FF (i.e. set `a` to $FF if carry), then do:
+</td></tr>
+<tr><td>
+
+`CVAL` == $FF (aka −1)
+
+</td><td>
```asm
; 3 bytes, 3 cycles
sbc a ; if carry, then $ff, else 0
- or NCVAL ; $ff stays $ff, $00 becomes NCVAL; omit this if NCVAL is 0
+ or NCVAL ; $ff stays $ff, $00 becomes NCVAL
```
-If `NCVAL` is $FF (i.e. set `a` to $FF if not carry), invert carry and do the same:
+</td></tr>
+<tr><td>
+
+`NCVAL` == 0
+
+</td><td>
```asm
- ; 4 bytes, 4 cycles
- ccf ; invert carry flag
- sbc a ; if originally carry, then 0, else $ff
- or CVAL ; $00 becomes CVAL, $ff stays $ff; omit this if CVAL is 0
+ ; 3 bytes, 3 cycles
+ sbc a ; if carry, then $ff, else 0
+ and CVAL ; $ff becomes CVAL, 0 stays 0
```
-If `CVAL` equals `NCVAL - 1`, then do:
+</td></tr>
+<tr><td>
+
+`CVAL` == `NCVAL - 1`, <br>aka<br> `CVAL + 1` == `NCVAL`
+
+</td><td>
```asm
; 3 bytes, 3 cycles
@@ -298,7 +341,12 @@ If `CVAL` equals `NCVAL - 1`, then do:
add NCVAL ; -1 becomes NCVAL - 1 aka CVAL, 0 becomes NCVAL
```
-If `CVAL` equals `NCVAL - 2`, then do:
+</td></tr>
+<tr><td>
+
+`CVAL` == `NCVAL - 2`, <br>aka<br> `CVAL + 2` == `NCVAL`
+
+</td><td>
```asm
; 3 bytes, 3 cycles
@@ -306,14 +354,66 @@ If `CVAL` equals `NCVAL - 2`, then do:
sbc -NCVAL ; -1 becomes NCVAL - 2 aka CVAL, 0 becomes NCVAL
```
-If `CVAL` is 0 and `NCVAL` is 1 (i.e. set `a` to 0 if carry, or 1 if not carry), then do:
+</td></tr>
+<tr><td>
+
+`CVAL` == 0
+
+</td><td>
```asm
- ; 2 bytes, 2 cycles
- sbc a ; if carry, then $ff aka -1, else 0
- inc a ; -1 becomes 0, 0 becomes 1
+ ; 4 bytes, 4 cycles
+ ccf ; invert carry flag
+ sbc a ; if originally carry, then 0, else $ff
+ and NCVAL ; 0 stays 0, $ff becomes NCVAL
```
+</td></tr>
+<tr><td>
+
+`NCVAL` == $FF (aka −1)
+
+</td><td>
+
+```asm
+ ; 4 bytes, 4 cycles
+ ccf ; invert carry flag
+ sbc a ; if originally carry, then 0, else $ff
+ or CVAL ; $00 becomes CVAL, $ff stays $ff
+```
+
+</td></tr>
+<tr><td>
+
+`CVAL` == `NCVAL + 1`, <br>aka<br> `CVAL - 1` == `NCVAL`
+
+</td><td>
+
+```asm
+ ; 4 bytes, 4 cycles
+ ccf ; invert carry flag
+ sbc a ; if originally carry, then 0, else $ff aka -1
+ add CVAL ; -1 becomes CVAL - 1 aka NCVAL, 0 becomes CVAL
+```
+
+</td></tr>
+<tr><td>
+
+`CVAL` == `NCVAL + 2`, <br>aka<br> `CVAL - 2` == `NCVAL`
+
+</td><td>
+
+```asm
+ ; 4 bytes, 4 cycles
+ ccf ; invert carry flag
+ sbc a ; if carry, then 0, else $ff aka -1; doesn't change the carry flag
+ sbc -CVAL ; -1 becomes CVAL - 2 aka NCVAL, 0 becomes CVAL
+```
+
+</td></tr>
+
+</table>
+
### Increment or decrement `a` when the carry flag is set