multiplies two 16bit values into a 16bit product, permitting overflow.
PBASIC allows you to multiply any two variables togetherbits, bytes, words, or a mixtureto get a 16bit product. If the product is larger than 16 bits, the highestorder 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 longform 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 16bit numbers is a 32bit 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.
; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.bubblesoftonline.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; MULTIPLY multiplier, multiplicand ; Multiply two 16bit numbers into a 16bit product, permitting overflow. ; Device data and reset vector P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start org 8 mulcH Res d'1' ; MSB of multiplicand. mulcL Res d'1' ; LSB of multiplicand. mulpH Res d'1' ; MSB of multiplier. mulpL Res d'1' ; LSB of multiplier. prodH Res d'1' ; MSB of product. prodL Res d'1' ; LSB of product. index Res d'1' ; temporary counter org 0 start MOVLW d'0' ; Outputs for LEDs to TRIS 6h MOVLW d'0' ; display 16bit result. TRIS 7h MOVLW d'0' ; Problem: multiply MOVWF mulcH MOVLW 0x0B ; 0Bh by 1016h MOVWF mulcL MOVLW 0x10 MOVWF mulpH MOVLW 0x16 MOVWF mulpL CALL multiply MOVF prodL,w ; Show result in binary on MOVWF 6h MOVF prodH,w ; LEDs connected to ports. MOVWF 7h GOTO $ ; Endless loop Multiply CLRF prodH ; Clear product to make CLRF prodL ; way for new calculation. MOVLW d'16' ; Number of bits to calc. MOVWF index Multiply_loop BCF status,c RLF prodL ; Shift product left. RLF prodH BCF status,c RLF mulcL ; Shift multiplicand left. RLF mulcH BTFSS status,c ; If carry, add multiplier GOTO Multiply_skip MOVF mulpL,w ; to the product. Else skip. ADDWF prodL BTFSC status,c ; 16bit addition: prod+mulp INCF prodH MOVF mulpH,w ADDWF prodH Multiply_skip DECFSZ index GOTO Multiply_loop RETLW 0h end
; MULTIPLY multiplier, multiplicand ; Multiply two 16bit numbers into a 16bit 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 16bit 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 ; 16bit addition: prod+mulp inc prodH ADD prodH,mulpH :skip djnz index,:loop ret
See also:
file: /Techref/microchip/seepicsrc/psbpix/multiplication.htm, 6KB, , updated: 2001/5/25 14:48, local time: 2018/10/16 06:20,

©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://techref.massmind.org/techref/microchip/seepicsrc/psbpix/multiplication.htm"> microchip seepicsrc psbpix multiplication</A> 
Did you find what you needed? 
Welcome to massmind.org! 
Welcome to techref.massmind.org! 
.