mul16.z80 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. HL_Times_BC:
  2. ;Inputs:
  3. ; HL and BC are factors
  4. ;Outputs:
  5. ; DEHL is the 32-bit value of the product
  6. ; BC is unchanged
  7. ; A is 0
  8. ex de,hl
  9. DE_Times_BC:
  10. ;This was made by Runer112
  11. ;Tested by jacobly
  12. mul16:
  13. ;BC*DE --> DEHL
  14. ; ~544.887cc as calculated in jacobly's test
  15. ;min: 214cc (DE = 1)
  16. ;max: 667cc
  17. ;avg: 544.4507883cc however, deferring to jacobly's result as mine may have math issues ?
  18. ;177 bytes
  19. ld a,d
  20. ld d,0
  21. ld h,b
  22. ld l,c
  23. add a,a
  24. jr c,Mul_BC_DE_DEHL_Bit14
  25. add a,a
  26. jr c,Mul_BC_DE_DEHL_Bit13
  27. add a,a
  28. jr c,Mul_BC_DE_DEHL_Bit12
  29. add a,a
  30. jr c,Mul_BC_DE_DEHL_Bit11
  31. add a,a
  32. jr c,Mul_BC_DE_DEHL_Bit10
  33. add a,a
  34. jr c,Mul_BC_DE_DEHL_Bit9
  35. add a,a
  36. jr c,Mul_BC_DE_DEHL_Bit8
  37. add a,a
  38. jr c,Mul_BC_DE_DEHL_Bit7
  39. ld a,e
  40. and %11111110
  41. add a,a
  42. jr c,Mul_BC_DE_DEHL_Bit6
  43. add a,a
  44. jr c,Mul_BC_DE_DEHL_Bit5
  45. add a,a
  46. jr c,Mul_BC_DE_DEHL_Bit4
  47. add a,a
  48. jr c,Mul_BC_DE_DEHL_Bit3
  49. add a,a
  50. jr c,Mul_BC_DE_DEHL_Bit2
  51. add a,a
  52. jr c,Mul_BC_DE_DEHL_Bit1
  53. add a,a
  54. jr c,Mul_BC_DE_DEHL_Bit0
  55. rr e
  56. ret c
  57. ld h,d
  58. ld l,e
  59. ret
  60. Mul_BC_DE_DEHL_Bit14:
  61. add hl,hl
  62. adc a,a
  63. jr nc,Mul_BC_DE_DEHL_Bit13
  64. add hl,bc
  65. adc a,d
  66. Mul_BC_DE_DEHL_Bit13:
  67. add hl,hl
  68. adc a,a
  69. jr nc,Mul_BC_DE_DEHL_Bit12
  70. add hl,bc
  71. adc a,d
  72. Mul_BC_DE_DEHL_Bit12:
  73. add hl,hl
  74. adc a,a
  75. jr nc,Mul_BC_DE_DEHL_Bit11
  76. add hl,bc
  77. adc a,d
  78. Mul_BC_DE_DEHL_Bit11:
  79. add hl,hl
  80. adc a,a
  81. jr nc,Mul_BC_DE_DEHL_Bit10
  82. add hl,bc
  83. adc a,d
  84. Mul_BC_DE_DEHL_Bit10:
  85. add hl,hl
  86. adc a,a
  87. jr nc,Mul_BC_DE_DEHL_Bit9
  88. add hl,bc
  89. adc a,d
  90. Mul_BC_DE_DEHL_Bit9:
  91. add hl,hl
  92. adc a,a
  93. jr nc,Mul_BC_DE_DEHL_Bit8
  94. add hl,bc
  95. adc a,d
  96. Mul_BC_DE_DEHL_Bit8:
  97. add hl,hl
  98. adc a,a
  99. jr nc,Mul_BC_DE_DEHL_Bit7
  100. add hl,bc
  101. adc a,d
  102. Mul_BC_DE_DEHL_Bit7:
  103. ld d,a
  104. ld a,e
  105. and %11111110
  106. add hl,hl
  107. adc a,a
  108. jr nc,Mul_BC_DE_DEHL_Bit6
  109. add hl,bc
  110. adc a,0
  111. Mul_BC_DE_DEHL_Bit6:
  112. add hl,hl
  113. adc a,a
  114. jr nc,Mul_BC_DE_DEHL_Bit5
  115. add hl,bc
  116. adc a,0
  117. Mul_BC_DE_DEHL_Bit5:
  118. add hl,hl
  119. adc a,a
  120. jr nc,Mul_BC_DE_DEHL_Bit4
  121. add hl,bc
  122. adc a,0
  123. Mul_BC_DE_DEHL_Bit4:
  124. add hl,hl
  125. adc a,a
  126. jr nc,Mul_BC_DE_DEHL_Bit3
  127. add hl,bc
  128. adc a,0
  129. Mul_BC_DE_DEHL_Bit3:
  130. add hl,hl
  131. adc a,a
  132. jr nc,Mul_BC_DE_DEHL_Bit2
  133. add hl,bc
  134. adc a,0
  135. Mul_BC_DE_DEHL_Bit2:
  136. add hl,hl
  137. adc a,a
  138. jr nc,Mul_BC_DE_DEHL_Bit1
  139. add hl,bc
  140. adc a,0
  141. Mul_BC_DE_DEHL_Bit1:
  142. add hl,hl
  143. adc a,a
  144. jr nc,Mul_BC_DE_DEHL_Bit0
  145. add hl,bc
  146. adc a,0
  147. Mul_BC_DE_DEHL_Bit0:
  148. add hl,hl
  149. adc a,a
  150. jr c,Mul_BC_DE_DEHL_FunkyCarry
  151. rr e
  152. ld e,a
  153. ret nc
  154. add hl,bc
  155. ret nc
  156. inc e
  157. ret nz
  158. inc d
  159. ret
  160. Mul_BC_DE_DEHL_FunkyCarry:
  161. inc d
  162. rr e
  163. ld e,a
  164. ret nc
  165. add hl,bc
  166. ret nc
  167. inc e
  168. ret