A screen capture of the demo results.

Note - The last two lines of LONG and QUAD shows the difference between
Euclidean and truncate with remainder results.
The truncate division functions should compile and run as-is in any version of PowerBASIC from PBWin 8 and PBCC 4 to PBWin 10 and PBCC 6, maybe earier. Compiling with the PBMAIN function, the demonstration, requires PBWin 10 Will compile in either PBWin 10 or PBCC 6. To retain divide by zero detection replace "FUNCTION = %err_divisionbyzero" with "ERROR %err_divisionbyzero".
#compile exe
#dim all
#if %def(%pb_cc32) 'if PBCC
#console off 'don't create a console window
#endif
'============================== Division Of LONGs ==============================
function TruncateDivide (byval Dividend as long, _
byval Divisor as long, _
byref Quotient as long, _
byref Remainder as long) as long
! mov ebx, Divisor '----
! cmp ebx, 0
! jne DoDivide
! mov function, %err_divisionbyzero
! jmp Done
DoDivide:
! mov esi, Quotient
! mov edi, Remainder
! mov eax, Dividend 'load the dividend
! cdq 'sign-extend EAX into EDX:EAX
! idiv ebx 'EDX:EAX / ECX
! mov [esi], eax 'Quotient set return value
! mov [edi], edx 'Remainder set return value
Done:
end function
'=============================== Division Of QUADs =============================
'Easy enough to do yourself, but this makes set complete.
'Also holds place in DLL for assembly version with no change of parameters.
function TruncateDivideQuad (byval Dividend as quad, _
byval Divisor as quad, _
byref Quotient as quad, _
byref Remainder as quad) as long
'--------------------------
local FmtLg, FmtQd as string
'
if Divisor = 0 then
function = %err_divisionbyzero
exit function
end if
'
Quotient = Dividend \ Divisor
Remainder = Dividend mod Divisor
end function
'/\/\/\/\/\/\/\/\/\/\/\/\ Demostrate Truncate Division /\/\/\/\/\/\/\/\/\/\/\/\
function pbmain () as long
local hTWin as dword
local Quotient, Remainder, ErrNum as long
local QuotientQ, RemainderQ as quad
local FmtLg, FmtQd as string
'
txt.window ("Truncate Division Demonstration", 150, 100, 18, 64) to hTWin
'
FmtLg = " #;-#; 0"
FmtQd = " ###########;-###########; 0"
'================================== Long =====================================
txt.print "type LONG"
ErrNum = TruncateDivide( 9, 0, Quotient, Remainder)
txt.print " 9 / 0 = " + format$(Quotient, FmtLg) + " R" + _
format$(Remainder, FmtLg) + " error code = " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivide(9, 4, Quotient, Remainder)
txt.print " 9 / 4 = " + format$(Quotient, FmtLg) + " R" + _
format$(Remainder, FmtLg) + " error code = " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivide(9, -4, Quotient, Remainder)
txt.print " 9 / -4 = " + format$(Quotient, FmtLg) + " R" + _
format$(Remainder, FmtLg) + " error code = " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivide(-9, 4, Quotient, Remainder)
txt.print "-9 / 4 = " + format$(Quotient, FmtLg) + " R" + _
format$(Remainder, FmtLg) + " error code = " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivide(-9, -4, Quotient, Remainder)
txt.print "-9 / -4 = " + format$(Quotient, FmtLg) + " R" + _
format$(Remainder, FmtLg) + " error code = " + dec$(ErrNum, 2)
'
'================================== Quad =====================================
txt.print
txt.print "type QUAD"
ErrNum = TruncateDivideQuad(90000000000, 0, QuotientQ, RemainderQ)
txt.print " 90000000000 / 0 = " + _
format$(QuotientQ, FmtQd) + " R" + format$(RemainderQ, FmtQd) + _
" error code " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivideQuad(90000000000, 40000000000, QuotientQ, RemainderQ)
txt.print " 90000000000 / 40000000000 = " + _
format$(QuotientQ, FmtQd) + " R" + format$(RemainderQ, FmtQd) + _
" error code " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivideQuad(90000000000, -40000000000, QuotientQ, RemainderQ)
txt.print " 90000000000 / -40000000000 = " + _
format$(QuotientQ, FmtQd) + " R" + format$(RemainderQ, FmtQd) + _
" error code " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivideQuad(-90000000000, 40000000000, QuotientQ, RemainderQ)
txt.print "-90000000000 / 40000000000 = " + _
format$(QuotientQ, FmtQd) + " R" + format$(RemainderQ, FmtQd) + _
" error code " + dec$(ErrNum, 2)
'
ErrNum = TruncateDivideQuad(-90000000000, -40000000000, QuotientQ, RemainderQ)
txt.print "-90000000000 / -40000000000 = " + _
format$(QuotientQ, FmtQd) + " R" + format$(RemainderQ, FmtQd) + _
" error code " + dec$(ErrNum, 2)
'
txt.print
txt.print
txt.color = &h0000C000
txt.print "Any key to close."
txt.waitkey$
end function
Source and compiled code partial copyleft (ↄ), the limitation is you
may not claim creation and attempt to copyright it.
This page is copyright © 2026, all rights reserved Dale Yarker.
Created on 30 August 2026.
To Dale's Notebook
| To Programs
|