multiplies two 16-bit values into a 16-bit product, permitting overflow.
PBASIC allows you to multiply any two variables together--bits, bytes, words, or a mixture--to get a 16-bit product. If the product is larger than 16 bits, the highest-order bits are lost.
This isn't as much of a handicap as it might sound. Users of the BASIC Stamp have managed to get excellent results by carefully designing their programs to avoid overflow. The benefit of working within this limitation is that the routine required to do this sort of math is compact and reasonably fast.
Multiplication of binary numbers works just like the long-form multiplication you learned in school, only it's much easier. Instead of multiplication tables, all you need is this general rule: n x 0 = 0; and n x 1 = n. Arrange the numbers in the traditional way, and you're ready to go:
Although the product of two 16-bit numbers is a 32-bit number, note that in this case the first 16 bits of the product are all 0s and can be safely ignored.
To see Multiply in operation, either run the program with the PSIM simulator, or connect the circuit below to an erasable PIC or PIC emulator, such as the Parallax downloader. Assemble and run MULTIPLY.SRC. When you apply power to the PIC, the LEDs will light up in the binary pattern of the answer to the math problem 0Bh * 1016h, which is B0F2h. The product displays the following binary pattern on the LEDs:
1011 0000 1111 0010
Try multiplying various values to see different results.
; MULTIPLY multiplier, multiplicand ; Multiply two 16-bit numbers into a 16-bit product, permitting overflow. org 8 mulcH ds 1 ; MSB of multiplicand. mulcL ds 1 ; LSB of multiplicand. mulpH ds 1 ; MSB of multiplier. mulpL ds 1 ; LSB of multiplier. prodH ds 1 ; MSB of product. prodL ds 1 ; LSB of product. index ds 1 ; temporary counter ; Device data and reset vector device pic16c55,xt_osc,wdt_off,protect_off reset start org 0 start mov !rb,#0 ; Outputs for LEDs to mov !rc,#0 ; display 16-bit result. mov mulcH,#0 ; Problem: multiply mov mulcL,#0Bh ; 0Bh by 1016h mov mulpH,#10h mov mulpL,#16h call multiply mov rb,prodL ; Show result in binary on mov rc,prodH ; LEDs connected to ports. jmp $ ; Endless loop Multiply clr prodH ; Clear product to make clr prodL ; way for new calculation. mov index,#16 ; Number of bits to calc. :loop clc rl prodL ; Shift product left. rl prodH clc rl mulcL ; Shift multiplicand left. rl mulcH jnc :skip ; If carry, add multiplier ADD prodL,mulpL ; to the product. Else skip. snc ; 16-bit addition: prod+mulp inc prodH ADD prodH,mulpH :skip djnz index,:loop ret