## Let x = y * z (multiplication)

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.

#### Demonstrating Multiply.

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 16-bit numbers into a 16-bit 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 16-bit 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.
BTFSC status,c             ; 16-bit addition: prod+mulp
INCF prodH
MOVF mulpH,w
Multiply_skip
DECFSZ index
GOTO Multiply_loop
RETLW 0h

end

```
```; 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.
inc	prodH
:skip	djnz	index,:loop
ret
```

 file: /Techref/microchip/seepicsrc/psbpix/multiplication.htm, 6KB, , updated: 2001/5/25 14:48, local time: 2018/9/18 17:15, TOP NEW HELP FIND:  54.161.71.87:LOG IN

 ©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?Please DO link to this page! Digg it! / MAKE! /  microchip seepicsrc psbpix multiplication

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

 PICList 2018 contributors: o List host: MIT, Site host massmind.org, Top posters @20180918 RussellMc, Van Horn, David, Sean Breheny, David C Brown, Bob Blick, Isaac M. Bavaresco, Neil, Denny Esterline, John Gardner, Harold Hallikainen, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.