home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n20.zip / MPMUL1.ASM < prev    next >
Assembly Source File  |  1989-10-13  |  4KB  |  117 lines

  1.         title   MPMUL1.ASM Multiple-Precision Unsigned Multiply
  2.         page    55,132
  3.  
  4. ; MPMUL1.ASM    Multiple-Precision Unsigned Multiply
  5. ;               for Intel 8086, 8088, 80286, and
  6. ;               80386 in real mode/16-bit protected mode
  7. ;
  8. ; Copyright (C) 1989 Ziff Davis Communications
  9. ; PC Magazine * Ray Duncan
  10. ;
  11. ; Call with:    DS:SI   = address of source operand
  12. ;               ES:DI   = address of destination operand
  13. ;               CX      = operand length in bytes
  14. ;
  15. ;               Assumes direction flag is clear at entry
  16. ;               Assumes DS = ES <> SS
  17. ;               Assumes CX <= 255
  18. ;
  19. ; Returns:      ES:DI   = address of product
  20. ;
  21. ;               NOTE: Buffer for destination operand must be
  22. ;               twice as long as the actual operand, because
  23. ;               it will receive a double-precision result.
  24. ;
  25. ; Destroys:     AX (other registers preserved)
  26. ;
  27. ; Usage:        DS:SI = u[0] base address source operand
  28. ;               SS:BP = v[0] base address destination operand
  29. ;               ES:DI = w[0] base address of product
  30. ;               BX    = i    index for outer loop
  31. ;               CX    = j    index for inner loop
  32. ;               DH    = m    operand length in bytes
  33. ;               DL    = k    remainder of partial products
  34.  
  35. _TEXT    segment word public 'CODE'
  36.  
  37.          assume  cs:_TEXT
  38.  
  39.          public  mpmul1
  40. mpmul1   proc    near
  41.  
  42.          push    bx                      ; save registers
  43.          push    dx
  44.          push    bp
  45.          sub     sp,cx                   ; make buffer on stack
  46.          mov     bp,sp                   ; for destination operand
  47.          mov     dh,cl                   ; save operand length (m)
  48.  
  49.          push    cx
  50.          push    si                      ; copy destination operand
  51.          push    di                      ; to temporary storage in
  52.          push    es                      ; stack frame, because result
  53.          push    ss                      ; will be built in destination
  54.          pop     es                      ; operand's buffer
  55.          mov     si,di
  56.          mov     di,bp
  57.          rep     movsb
  58.          pop     es
  59.          pop     di
  60.          pop     si
  61.          pop     cx
  62.  
  63.          push    di                      ; initialize destination buffer
  64.          xor     ax,ax                   ; to receive result (it better be 
  65.          rep     stosw                   ; twice the size of the operands)
  66.          pop     di
  67.  
  68.          xor     bx,bx                   ; i = 0
  69.  
  70. mpmul11: xor     dl,dl                   ; k = 0
  71.          xor     cx,cx                   ; j = 0
  72.  
  73. mpmul12: xchg    bx,cx
  74.          mov     al,[si+bx]              ; get u[j]
  75.          xchg    bx,cx
  76.  
  77.          xchg    bp,di
  78.          mov     ah,ss:[di+bx]           ; get v[i]
  79.          xchg    bp,di
  80.         
  81.          mul     ah                      ; t = u[j] * v[i]
  82.          add     al,dl                   ;     + k
  83.          adc     ah,0
  84.          add     bx,cx
  85.          add     al,[bx+di]              ;     + w[i+j]
  86.          adc     ah,0
  87.          mov     [bx+di],al              ; w[i+j] = t mod b
  88.          mov     dl,ah                   ; k      = t / b
  89.          sub     bx,cx                   ; restore i
  90.  
  91.          inc     cx                      ; j++
  92.          cmp     cl,dh                   ; j = m?
  93.          jne     mpmul12                 ; no, repeat inner loop
  94.  
  95.          push    bx
  96.          add     bl,dh                   ; w[i+m] = k
  97.          adc     bh,0
  98.          mov     [di+bx],ah
  99.          pop     bx
  100.  
  101.          inc     bx                      ; i++
  102.          cmp     bl,dh                   ; i = m?
  103.          jne     mpmul11                 ; no, repeat outer loop
  104.  
  105.          add     sp,bx                   ; discard operand buffer
  106.          pop     bp                      ; restore registers
  107.          pop     dx
  108.          pop     bx
  109.          ret                             ; back to caller
  110.  
  111. mpmul1   endp
  112.  
  113. _TEXT    ends
  114.  
  115.          end
  116.  
  117.