Div32
In order to make the Positron8 compiler more compatible with code produced for the melab's PICBASIC Pro compiler, the Div32 function has been added. The original melab's compiler's multiply function only operated as a 16-bit x 16-bit multiply, thus producing a 32-bit result. However, since the compiler only supported a maximum variable size of 16 bits (Word), access to the result had to happen in 2 stages: -
Var = Var1 * Var2 returns the lower 16 bits of the multiply
while…
Var = Var1 ** Var2 returns the upper 16 bits of the multiply
There was no way to access the 32-bit result as a valid single value.
In many cases it is desirable to be able to divide the entire 32-bit result of the multiply by a 16bit number for averaging, or scaling. Div32 is actually limited to dividing a 31-bit unsigned integer (0 - 2147483647) by a 15-bit unsigned integer (0 - 32767). This ought to be sufficient in most situations.
Because the melab's compiler only allowed a maximum variable size of 16 bits (0 - 65535), Div32 relies on the fact that a multiply was performed just prior to the Div32 command, and that the internal compiler variables still contain the 32-bit result of the multiply. No other operation may occur between the multiply and the Div32 or the internal variables may be altered, thus destroying the 32-bit multiplication result.
The following example demonstrates the operation of Div32:-
Device = 18F26K40 ' Select the device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Set the Baud rate for HRsoutLn
Dim Wrd1 as Word
Dim Wrd2 as Word
Dim Wrd3 as Word
Dim wFake as Word ' Must be a Word type variable for result
Wrd2 = 300
Wrd3 = 1000
wFake = Wrd2 * Wrd3 ' Operators ** or */ could also be used instead
Wrd1= Div32 100
HRsoutLn Dec Wrd1
The above program assigns Wrd2 the value 300 and Wrd3 the value 1000. When multiplied together, the result is 300000. However, this number exceeds the 16-bit word size of a variable (65535). Therefore, the dummy variable, wFake, contains only the lower 16 bits of the result. Div32 uses the compiler's internal (System) variables as the Parameters.
Note.
This operand enables a certain compatibility with melab's compiler code, but is rather obsolete considering the 32-bit, and floating point capabilities of the Positron8 compiler.
