home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / DIVDH.AZM / DIVDH.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  2.0 KB  |  88 lines

  1. ;----------------------------------------------------------------
  2. ;       This is a module in the ASMLIB library            -
  3. ; The following two routines are extracted from page 144 & 145  -
  4. ; of Electronics International February 24 1982.        -
  5. ;  The first routine is a 32-by-16 bit division and the second  -
  6. ; routine is a 16-16 bit multiply. The 16-16 multiply also has  -
  7. ; a very fast 8-by-16 multiply routine. The register set-ups    -
  8. ; are as follows,                        -
  9. ;                                 -
  10. ; Division:    dividend in hl-de , divisor in bc        -
  11. ;        quotient in de , remainder in hl        -
  12. ;        overflow indicated by a carry            -
  13. ;                                -
  14. ; Multiplication:                        -
  15. ; for 16 * 16    multiplicand in bc , multiplier in de        -
  16. ;        result in de-hl                    -
  17. ;
  18. ;            Written         R.C.H.     16/8/83
  19. ;            Last Update    R.C.H.       16/8/83
  20. ;----------------------------------------------------------------
  21. ;
  22.     name    'divdh'
  23. ;
  24.     public    divdh
  25.     maclib    z80
  26. ;
  27. divdh:        ; 32 bits / 16 bits
  28.     mov    a,l        ; check for overflow
  29.     sub    c
  30.     mov    a,h
  31.     sbb    b
  32.     rnc            ; bad boy
  33. ;
  34.     mov    a,b
  35.     cma            ; do a 2's complement on divisor
  36.     mov    b,a
  37.     mov    a,c
  38.     cma
  39.     mov    c,a        ; all done
  40.     inx    b        ; why why why
  41.     call    loop        ; divide highest-order 3 bytes of dividend
  42. ; loop divides 3 byte dividend by 2 byte divisor
  43. loop:
  44.     mov    a,d        ; load third byte to be divided
  45.     mov    d,e        ; save low order dividend / hi order byte q.
  46.     mvi    e,8
  47. loop1:
  48.     dad    h        ; shift dividend left.
  49.     jrc    over        ; jump if dividend overflowed
  50.     add    a
  51.     jrnc    sub1
  52.     inx    h        ; fix up carry condition
  53. sub1:    
  54.     push    h        ; save high ord 2 of dividend
  55.     dad    b        ; subtract divisor
  56.     jrc    ok        ; jump if no borrow
  57.     pop    h        ; unsubtract if borrow
  58.     dcr    e        ; loop counter
  59.     jrnz    loop1
  60.     mov    e,a        ; put quotient into e
  61.     stc
  62.     ret
  63. ;
  64. ok:
  65.     inx    sp
  66.     inx    sp        ; clean stack up
  67.     inr    a        ; put 1 in quotient
  68.     dcr    e        ; update loop 1 counter
  69.     jrnz    loop1
  70.     mov    e,a        ; same as above
  71.     stc
  72.     ret
  73. ;
  74. over:
  75.     adc    a        ; finish shift, put 1 in quotient
  76.     jrnc    oversub
  77.     inx    h
  78. oversub:
  79.     dad    b
  80.     dcr    e
  81.     jrnz    loop1
  82.     mov    e,a
  83.     stc
  84.     ret
  85. ;
  86.     end
  87.  
  88.