Add digits of two arrays.
To include only the trim function in a DLL directly from this source file put the following lines in the DLL source:
The function AdditionVLU should compile in PowerBASIC For Windows (PBWin) Versions 8, 9 or 10 and PowerBASIC Console Compiler (PBCC) Versions 4, 5 or 6. The DEC$() function is new in PBWin 10 and PBCC 6, for earlier versions change to STR$() or FORMAT$(). All PBCC versions will ignore $$CRLF and wrap to next line at console width.
'File Name Addition_VLU.bas
'PBMain() for stand alone testing
#if not %def(%In_DLL)
#compile exe
#dim all
%In_DLL = 1
function pbmain () as long
local VLU1(), VLU2(), Total() as dword
local CurDig as long
local R_Str1, R_Str2 as string
dim VLU1(5)
dim VLU2(5)
'test 1 max
for CurDig = 0 to 5
VLU1(CurDig) = &hFFFF???
VLU2(CurDig) = &hFFFF???
next
AdditionVLU(VLU1(), VLU2(), Total())
R_Str1 = "max in digits to insure carry" + $crlf + _
hex$(Total(6), 4) + $spc + hex$(Total(5), 4) + $spc + _
hex$(Total(4), 4) + $spc + hex$(Total(3), 4) + $spc + _
hex$(Total(2), 4) + $spc + hex$(Total(1), 4) + $spc + _
hex$(Total(0), 4)
'test 2 min
VLU1(5) = 1
VLU2(5) = 1
for CurDig = 0 to 4
VLU1(CurDig) = 0
VLU2(CurDig) = 0
next
AdditionVLU(VLU1(), VLU2(), Total())
R_Str2 = "1 in MSDs, and 0s to insure leading 0" + $crlf + _
hex$(Total(5), 4) + $spc + hex$(Total(4), 4) + $spc + _
hex$(Total(3), 4) + $spc + hex$(Total(2), 4) + $spc + _
hex$(Total(1), 4) + $spc + hex$(Total(0), 4) + $crlf + _
dec$(ubound(Total()))
'test 3 lead 0
redim VLU2(3)
CurDig = AdditionVLU(VLU1(), VLU2(), Total()) 'reuse a LONG
? R_Str1 + $crlf + $crlf + R_Str2 + $crlf + $crlf + dec$(CurDig)
end function
#endif
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function AdditionVLU alias "AdditionVLU" (byref Addend1() as dword, _
byref Addend2() as dword, _
byref Sum() as dword) export as long
' - - - - - - - - - - - - - - - - - - - - - - - - - -
local SumUB as long
local SumUB_d as long 'Sum ubound decremented, needed twice
local Addend1UB, Addend2UB, LastAddDig as long
local CurCarry as long 'masked to upper 16 bits of Sum(CurDig)
register CurDig as long
Addend1UB = ubound(Addend1())
Addend2UB = ubound(Addend2())
SumUB_d = max&(Addend1UB, Addend2UB)
SumUB = SumUB_d + 1
' - - - - - - - - - - - - - - - - - - - - - - - - check for leading zero in -
if Addend1(Addend1UB) = 0 or Addend2(Addend2UB) = 0 then
function = -1
exit function
end if
' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - do the addition -
redim Sum(SumUB)
LastAddDig = min&(Addend1UB, Addend2UB)
for CurDig = 0 to LastAddDig
Sum(CurDig) = Addend1(CurDig) + Addend2(CurDig)
next
' - - - - - - - - - - - - - - - - - - - - - - - - - - - check/do digit carry -
for CurDig = 0 to SumUB_d
CurCarry = Sum(CurDig) and &hFFFF0000???
if CurCarry then
shift right CurCarry, 16
Sum(CurDig + 1) += CurCarry 'add the carry to next digit
Sum(CurDig) and= &h0000FFFF??? 'clear carry word
end if
next
' - - - - - - - - - - - - - - - - - - - - - - - - check/remove leading 0 out -
if Sum(SumUB) = 0 then
redim preserve Sum(SumUB_d)
end if
end function
Created on 25 December 2021; last edit 25 Dec 2021. This page Copyright © 2021 Dale Yarker, contained source code and implemented concept are Copyleft (ɔ).
To Dale's Notebook
| To Programs
|