;;****************************************************************************
TITLE         PROC MODULE  MUL16(UNSIGNED)
              PAGE           57,80
;;----------------------------------------------------------------------------
;; AUTHOR: M.S. ALAM
;; CHECKED: Dec 4, 1996
;;  This program multiplies two 16-bit numbers.  The numbers to be multiplied
;;are stored in the data segment at MPLIER and MCAND.  Space is reserved at 
;;PRODL and PRODH for the product.
;;  The program multiplies unsigned numbers using the right shift algorithm.
;;The largest two numbers that can be multiplied are: FFFFh and FFFFh or in
;;decimal 65535 and 65535.  For the purpose of testing the program, we suggest
;;you test the program for the following examples:
;;FFFFh x 10h = FFFF0h, FFFFh x 100h = FFFF00h, FFFFh x 1000h = FFFF000h 
;;FFFFh x  Fh = EFFF1h, FFFFh x  FFh = FEFF01h, FFFFh x  FFFh = FFEF001h
;;FFFFh x  FFFFh = FFFE0001
;;INPUT:   DX  <-  Multiplicand (Input1)              
;;         BX  <-  Multiplier   (Input2)
;;         CX  <-  Bit Counter = 10h = 16
;;OUTPUT:  AX  <-  High word of product
;;         BX  <-  Low Word of product
;;****************************************************************************
                 
              IF1
              INCLUDE  \PHY353\MACLIB\STRING.MAC
              ENDIF

;;****************************************************************************
DSEG          SEGMENT        PARA PUBLIC 'DATA'  ; Start of data segment

              EXTRN INPUT1:WORD, INPUT2:WORD, PRODL:WORD, PRODH:WORD
       
DSEG          ENDS                               ; End of data segment
               
;*****************************************************************************

CSEG          SEGMENT        PARA PUBLIC 'CODE'  ; Start of Code Segment
              ASSUME         CS:CSEG, DS:DSEG                      
              PUBLIC         MUL16

MUL16         PROC           NEAR
              INIT_DATA_SEG  DSEG 
              ;; Programming task starts here
              PUSH           AX                 ;; Save temporarily on stack
              PUSH           BX
              PUSH           CX
              PUSH           DX
              ;; Initializing steps
              SUB            AX,AX               ;;Store 0 in AX and clear CF
              MOV            BX,INPUT1           ;;Store multiplier in BX 
              MOV            DX,INPUT2           ;;Store multiplicand in DX
              MOV            CX,16               ;;Store # of bits in CX
              RCR            BX,1                ;;Rotate BX one bit right
NEXT:
              JNC            SHIFT               ;;If CF=1, skip the adding
              ADD            AX,DX               ;;Add multipicand to AX
SHIFT:                                           ;;Perform right shift of prod 
              RCR            AX,1                ;;Right shift of AX thru CF
              RCR            BX,1                ;;Right shift CF into BX
              DEC            CX                  ;;Decrease bit count by 1
              JCXZ           DONE                ;;Done if bit count is 0
              JMP            NEXT                ;;Otherwise loop for next bit
DONE:                                            ;;Multiplication is finished
              MOV            PRODL,BX            ;;Store low word at PRODL
              MOV            PRODH,AX            ;;Store high word at PRODH

              ;; Tidying up at the end
              POP            DX
              POP            CX
              POP            BX
              POP            AX

              RETN
MUL16         ENDP
CSEG          ENDS
              END      MUL16     

