From 3fb2506a3c81321b8f49ce73f2e9a66a4ee9e22f Mon Sep 17 00:00:00 2001 From: Rangi Date: Tue, 24 Nov 2020 16:41:03 -0500 Subject: carry table --- Optimizing-assembly-code.md | 144 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 122 insertions(+), 22 deletions(-) (limited to 'Optimizing-assembly-code.md') 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: + + + + + + + + + + + + + + + + + + + + +
If this......then do:
+ +`CVAL` == $FF (aka −1)
and
`NCVAL` == 0 + +
```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: +
+ +`CVAL` == 0
and
`NCVAL` == $FF (aka −1) + +
```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 +``` + +
+ +`CVAL` == 0
and
`NCVAL` == 1 + +
+ +```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: +
+ +`CVAL` == $FF (aka −1) + + ```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: +
+ +`NCVAL` == 0 + + ```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: +
+ +`CVAL` == `NCVAL - 1`,
aka
`CVAL + 1` == `NCVAL` + +
```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: +
+ +`CVAL` == `NCVAL - 2`,
aka
`CVAL + 2` == `NCVAL` + +
```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: +
+ +`CVAL` == 0 + + ```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 ``` +
+ +`NCVAL` == $FF (aka −1) + + + +```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 +``` + +
+ +`CVAL` == `NCVAL + 1`,
aka
`CVAL - 1` == `NCVAL` + +
+ +```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 +``` + +
+ +`CVAL` == `NCVAL + 2`,
aka
`CVAL - 2` == `NCVAL` + +
+ +```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 +``` + +
+ ### Increment or decrement `a` when the carry flag is set -- cgit v1.2.3