Mike - K8LH
Well-Known Member
As he is working in assembler I thought it best to avoid both multiply and divide. The above uses just shifts and addition.
But using an 8x16 (sign extended) multiply routine might not make the program much more complicated. The algorithm for three decimal digits is pretty simple, and you can always ignore any unwanted digits;
Code:
;
; BC2DF - binary °C*16 to decimal °F*1000 (3 decimal places)
;
; °F*1000 = 225 * TempH:TempL / 2 + 32000
;
; Bcd2:0 packed bcd output -067000 to +257000 (°F*1000)
;
; TempH:L Neg Output Display
; ==========================================
; 125.0000°C h'07D0' 0 257000 257.000°F
; 100.0000°C h'0640' 0 212000 212.000°F
; 77.0000°C h'04D0' 0 170600 170.600°F
; 25.0000°C h'0190' 0 077000 77.000°F
; 22.0000°C h'0160' 0 071600 71.600°F
; 0.5000°C h'0008' 0 032900 32.900°F
; 0.0625°C h'0001' 0 032112 32.112°F
; 0.0000°C h'0000' 0 032000 32.000°F
; - 0.0625°C h'FFFF' 0 031887 31.887°F
; - 0.1250°C h'FFFE' 0 031775 31.775°F
; - 5.0000°C h'FFB0' 0 023000 23.000°F
; -10.0000°C h'FF60' 0 014000 14.000°F
; -17.0625°C h'FEEF' 0 001287 1.287°F
; -17.6875°C h'FEE5' 0 000162 0.162°F
; -17.7500°C h'FEE4' 0 000050 0.050°F
; -17.8125°C h'FEE3' 1 000062 - 0.062°F
; -25.0000°C h'FE70' 1 013000 -13.000°F
; -55.0000°C h'FC90' 1 067000 -67.000°F
;
;
radix dec
BC2DF
movlw 225 ; multiplier = 225 |B0
call Mult8x16 ; multiplicand = TempH:TempL |B0
rlf Prod2,W ; divide by 2 (preserve sign) |B0
rrf Prod2,F ; |B0
rrf Prod1,F ; |B0
rrf Prod0,F ; |B0
movlw High(32000) ; add 32000 (0x7D00) to product |B0
addwf Prod1,F ; |B0
skpnc ; |B0
incf Prod2,F ; |B0
call AbsFunc ; make positive, setup NegFlag |B0
goto Bin2bcd ; output is 6 digit packed BCD |B0
;
Code:
;
; Sign Extended 8 x 16 Multiply
;
; Mult * Mul2H:L -> sign extended Prod2:0 (24 bit)
; Multiplier 'Mult' destroyed, Multiplicand 'Mul2H:L' unchanged
;
; 21 words, 98 to 138 cycles
;
Mult8x16
movwf Mult ; save multiplier |B0
clrf Prod2 ; product MSB |B0
clrf Prod1 ; |B0
clrf Prod0 ; |B0
bsf Prod0,7 ; set counter = 8 |B0
mloop rrf Mult,F ; multiplier >>= 1 |B0
bnc mnext ; |B0
movf Mul2L,W ; multiplicand lo (equ TempL) |B0
addwf Prod1,F ; |B0
movf Mul2H,W ; multiplicand hi (equ TempH) |B0
skpnc ; |B0
incf Mul2H,W ; |B0
addwf Prod2,F ; |B0
mnext rlf Prod2,W ; preserve the sign bit (b23) |B0
rrf Prod2,F ; |B0
rrf Prod1,F ; |B0
rrf Prod0,F ; |B0
bnc mloop ; |B0
return ; |B0
;
Last edited: