From 108bf334e8de3853539d344e093b68798e2cf6a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Thu, 15 Sep 2022 17:41:44 +0300 Subject: [PATCH] Implement bcdDigitAdd3 in calculator.asm --- calculator.asm | 58 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/calculator.asm b/calculator.asm index 7224eb8..1073b97 100644 --- a/calculator.asm +++ b/calculator.asm @@ -438,8 +438,8 @@ fResult: ; Arithmetic ; ------------------------------------------------------------------ -; x y -- tens(x+y) ones(x+y) -bcdDigitAdd: +; x y z -- tens(x+y+z) ones(x+y+z) +bcdDigitAdd3: cleq r0, r0, stAdd cleq r0, r0, stDup @@ -447,18 +447,43 @@ bcdDigitAdd: load r1, #a cleq r0, r0, pushWord cleq r0, r0, stGtEq + breq r0, r1, bcdDigitAdd + + ; Adjust so that carry is in the high 3 bits + ; Basically we want 9+1=a₁₆ to become 20₁₆ + xor r0, r0 + load r1, #16 + cleq r0, r0, pushWord + cleq r0, r0, stAdd + +; x y -- tens(x+y) ones(x+y) +bcdDigitAdd: + cleq r0, r0, stAdd + + cleq r0, r0, stDup + ; Only look at the bottom 5 bits, since high 3 bits may already + ; contain carry + cleq r0, r0, popWord + load r2, #1f + and r1, r2 + cleq r0, r0, pushWord + xor r0, r0 + load r1, #a + cleq r0, r0, pushWord + cleq r0, r0, stGtEq breq r0, r1, bcdDigitAddNoAdjust - ; Adjust so that carry is in the high nybble - ; Basically we want 9+1=a₁₆ to become 10₁₆ + ; Adjust so that carry is in the high 3 bits + ; Basically we want 9+1=a₁₆ to become 20₁₆ xor r0, r0 - load r1, #6 + load r1, #16 cleq r0, r0, pushWord cleq r0, r0, stAdd bcdDigitAddNoAdjust: cleq r0, r0, stDup cleq r0, r0, stShr4 + cleq r0, r0, stShr1 cleq r0, r0, stSwap breq r0, r0, stLowNybble @@ -618,11 +643,7 @@ fPrint: ; added to the corresponding minuend. This by itself gives a ; result that is one too small, so we add one to it (c.f. binary ; subtraction, where we negate the subtrahend and then add one). - ; Normally we'd have to split subtraction into two rounds, one - ; to add the complemented subtrahend and one to add the 1, but - ; since our minuend is a constant, we can add 1 to it instead. - ; 501 + 999 - biased - ; = 500 + 1 + 999 - biased + ; 500 + 1 + 999 - biased ; = 500 + 1000 - biased (1000 = 0 mod 1000) ; = 500 - biased @@ -649,14 +670,11 @@ fPrint: xor r0, r0 load r1, #5 cleq r0, r0, pushWord - cleq r0, r0, bcdDigitAdd - cleq r0, r0, stSwap - cleq r0, r0, popWord xor r0, r0 load r1, fArg1+1 cleq r0, r0, pushWord cleq r0, r0, bcdDigit9sComplement - cleq r0, r0, bcdDigitAdd + cleq r0, r0, bcdDigitAdd3 cleq r0, r0, stSwap cleq r0, r0, popWord @@ -779,6 +797,18 @@ stShr4: shr r0, 4 breq r0, r0, pushWord +; a -- a>>1 +stShr1: + cleq r0, r0, popWord + shr r1, 1 + xor r2, r2 + or r2, r0 + shl r2, 4 + shl r2, 3 + or r1, r2 + shr r0, 1 + breq r0, r0, pushWord + ; a -- a&f stLowNybble: cleq r0, r0, popWord