Compare commits

...

3 Commits

Author SHA1 Message Date
Juhani Krekelä 108bf334e8 Implement bcdDigitAdd3 in calculator.asm 2022-09-15 17:41:44 +03:00
Juhani Krekelä 95b7c676b2 Take advantage of tail call optimization where possible in calculator.asm 2022-09-15 17:30:41 +03:00
Juhani Krekelä 41bdec1b12 Unify indentation of labels in calculator.asm 2022-09-15 17:28:30 +03:00
1 changed files with 56 additions and 34 deletions

View File

@ -136,15 +136,15 @@ debug:
breq r0, r0, debugLoop breq r0, r0, debugLoop
debugEnd: debugEnd:
cleq r0, r0, newline cleq r0, r0, newline
load r0, debugr0 load r0, debugr0
load r1, debugr1 load r1, debugr1
load r2, debugr2 load r2, debugr2
load r3, debugr3 load r3, debugr3
ret ret
debugPtr: addr debugPtr debugPtr: addr debugPtr
debugr0: data 0 debugr0: data 0
@ -334,9 +334,7 @@ fUnpack:
cleq r0, r0, stLowNybble cleq r0, r0, stLowNybble
cleq r0, r0, stOver cleq r0, r0, stOver
cleq r0, r0, stStoreByte cleq r0, r0, stStoreByte
cleq r0, r0, stDec breq r0, r0, stDec
ret
; ptr -- f ; ptr -- f
fPack: fPack:
@ -388,9 +386,7 @@ fPack:
cleq r0, r0, stLoadByte cleq r0, r0, stLoadByte
cleq r0, r0, stOr cleq r0, r0, stOr
cleq r0, r0, stSwap cleq r0, r0, stSwap
cleq r0, r0, stInc breq r0, r0, stInc
ret
; ------------------------------------------------------------------ ; ------------------------------------------------------------------
; Unpacked floating point variables ; Unpacked floating point variables
@ -442,8 +438,8 @@ fResult:
; Arithmetic ; Arithmetic
; ------------------------------------------------------------------ ; ------------------------------------------------------------------
; x y -- tens(x+y) ones(x+y) ; x y z -- tens(x+y+z) ones(x+y+z)
bcdDigitAdd: bcdDigitAdd3:
cleq r0, r0, stAdd cleq r0, r0, stAdd
cleq r0, r0, stDup cleq r0, r0, stDup
@ -451,18 +447,43 @@ bcdDigitAdd:
load r1, #a load r1, #a
cleq r0, r0, pushWord cleq r0, r0, pushWord
cleq r0, r0, stGtEq 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 breq r0, r1, bcdDigitAddNoAdjust
; Adjust so that carry is in the high nybble ; Adjust so that carry is in the high 3 bits
; Basically we want 9+1=a₁₆ to become 10₁₆ ; Basically we want 9+1=a₁₆ to become 20₁₆
xor r0, r0 xor r0, r0
load r1, #6 load r1, #16
cleq r0, r0, pushWord cleq r0, r0, pushWord
cleq r0, r0, stAdd cleq r0, r0, stAdd
bcdDigitAddNoAdjust: bcdDigitAddNoAdjust:
cleq r0, r0, stDup cleq r0, r0, stDup
cleq r0, r0, stShr4 cleq r0, r0, stShr4
cleq r0, r0, stShr1
cleq r0, r0, stSwap cleq r0, r0, stSwap
breq r0, r0, stLowNybble breq r0, r0, stLowNybble
@ -602,9 +623,7 @@ fPrint:
load r0, fArg1+2 load r0, fArg1+2
cleq r0, r0, writehexNybble cleq r0, r0, writehexNybble
load r0, fArg1+3 load r0, fArg1+3
cleq r0, r0, writehexNybble breq r0, r0, writehexNybble
ret
fPrintNegativeExponent: fPrintNegativeExponent:
cleq r0, r0, popWord ; Remove result we don't need cleq r0, r0, popWord ; Remove result we don't need
@ -624,11 +643,7 @@ fPrint:
; added to the corresponding minuend. This by itself gives a ; 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 ; 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). ; subtraction, where we negate the subtrahend and then add one).
; Normally we'd have to split subtraction into two rounds, one ; 500 + 1 + 999 - biased
; 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 + 1000 - biased (1000 = 0 mod 1000) ; = 500 + 1000 - biased (1000 = 0 mod 1000)
; = 500 - biased ; = 500 - biased
@ -655,14 +670,11 @@ fPrint:
xor r0, r0 xor r0, r0
load r1, #5 load r1, #5
cleq r0, r0, pushWord cleq r0, r0, pushWord
cleq r0, r0, bcdDigitAdd
cleq r0, r0, stSwap
cleq r0, r0, popWord
xor r0, r0 xor r0, r0
load r1, fArg1+1 load r1, fArg1+1
cleq r0, r0, pushWord cleq r0, r0, pushWord
cleq r0, r0, bcdDigit9sComplement cleq r0, r0, bcdDigit9sComplement
cleq r0, r0, bcdDigitAdd cleq r0, r0, bcdDigitAdd3
cleq r0, r0, stSwap cleq r0, r0, stSwap
cleq r0, r0, popWord cleq r0, r0, popWord
@ -678,9 +690,7 @@ fPrint:
cleq r0, r0, popWord cleq r0, r0, popWord
xor r0, r0 xor r0, r0
or r0, r1 or r0, r1
cleq r0, r0, writehexNybble breq r0, r0, writehexNybble
ret
fPrintOverflow: fPrintOverflow:
load r0, #4f ; O load r0, #4f ; O
@ -787,6 +797,18 @@ stShr4:
shr r0, 4 shr r0, 4
breq r0, r0, pushWord 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 ; a -- a&f
stLowNybble: stLowNybble:
cleq r0, r0, popWord cleq r0, r0, popWord
@ -942,7 +964,7 @@ stZeroMem:
cleq r0, r0, stSwap cleq r0, r0, stSwap
breq r0, r0, stZeroMem breq r0, r0, stZeroMem
stZeroMemDone: stZeroMemDone:
ret ret
; ================================================================== ; ==================================================================