Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. ;bc = m
  2. ;de = n
  3. ;out: dehl = m*n
  4. intmul16:
  5.   xor a
  6.   ld h,a
  7.   ld l,a
  8.  
  9.   bit 7,d
  10.   jr z,muldpos
  11.   sbc hl,bc
  12. muldpos:
  13.  
  14.   or b
  15.   jp p,mulbpos
  16.   sbc hl,de
  17. mulbpos:
  18.  
  19.   ld a,16
  20. mulloop:
  21.   add hl,hl
  22.   rl e
  23.   rl d
  24.   jr nc,mul0bit
  25.   add hl,bc
  26.   jr nc,mul0bit
  27.   inc de
  28. mul0bit:
  29.   dec a
  30.   jr nz,mulloop
  31.   ret  
  32.  
  33. ;===============
  34. ;The following routine divides ac by de and places the quotient in ac and the remainder in hl
  35.  
  36. div_ac_de:
  37.    ld   hl, 0
  38.    ld   b, 16
  39.  
  40. ._loop:
  41.    sll  c
  42.    rla
  43.    adc  hl, hl
  44.    sbc  hl, de
  45.    jr   nc, $+4
  46.    add  hl, de
  47.    dec  c
  48.    
  49.    djnz ._loop
  50.     ret
  51.  
  52.  
  53. ;=====================
  54. ;The following routine multiplies h by e and places the result in hl
  55.  
  56. mult_h_e
  57.  
  58.    ld   d, 0    ; Combining the overhead and
  59.    sla  h       ; optimised first iteration
  60.    sbc  a, a
  61.    and  e
  62.    ld   l, a
  63.    
  64.    ld   b, 7
  65. .loop:
  66.    add  hl, hl          
  67.    jr   nc, $+3
  68.    add  hl, de
  69.    
  70.    djnz .loop
  71.    
  72.    ret
  73.  
  74.  
  75. ;============
  76. ;The following routine divides hl by c and places the quotient in hl and the remainder in a
  77.  
  78. div_hl_c:
  79.    xor  a
  80.    ld   b, 16
  81.  
  82. .loop:
  83.    add  hl, hl
  84.    rla
  85.    jr   c, $+5
  86.    cp   c
  87.    jr   c, $+4
  88.  
  89.    sub  c
  90.    inc  l
  91.    
  92.    djnz .loop
  93.    
  94.    ret
  95.  
  96.  
  97.  
  98. DEHL_Div_C:
  99. ;Inputs:
  100. ;     DEHL is a 32 bit value where DE is the upper 16 bits
  101. ;     C is the value to divide DEHL by
  102. ;Outputs:
  103. ;    A is the remainder
  104. ;    B is 0
  105. ;    C is not changed
  106. ;    DEHL is the result of the division
  107. ;
  108.   ;   ld b,32
  109.  
  110.      xor a
  111.      dup 32  
  112.        add hl,hl
  113.        rl e : rl d
  114.        rla
  115.        cp c
  116.        jr c,$+4
  117.          inc l
  118.          sub c
  119.   ;     djnz $-11
  120.      edup  
  121.      ret
  122.  
  123.  
  124.  
  125. BCDE_Times_A:
  126. ;Inputs: BC:DE,A
  127. ;Outputs: A:HL:IX is the 40-bit product, BC,DE unaffected
  128. ;503cc~831cc
  129. ;667cc average
  130. ;29 bytes
  131.     ld ix,0
  132.     ld hl,0
  133.     call .a1
  134. .a1:
  135.     call .a2
  136. .a2:
  137.     call .a3
  138. .a3:
  139.     add ix,ix : adc hl,hl : rla : ret nc
  140.     add ix,de : adc hl,bc : adc a,0
  141.     ret
  142.  
  143. mult_de_a:
  144. ;Input: A = Multiplier, DE = Multiplicand, HL = 0, C = 0
  145. ;Output: A:HL = Product
  146.  
  147.         add     a,a             ; optimised 1st iteration
  148.         jr      nc,$+4
  149.         ld      h,d
  150.         ld      l,e
  151.        
  152.         dup 7
  153.         add     hl,hl           ; unroll 7 times
  154.         rla                     ; ...
  155.         jr      nc,$+4          ; ...
  156.         add     hl,de           ; ...
  157.         adc     a,c             ; ...
  158.         edup
  159.         ret
  160.  
  161.