mlu2.s 812 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. .define .mlu2
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! 16 bits unsigned multiply routine
  8. ! Expects operands on stack
  9. ! Yields result in de-registers
  10. ! This routine could also be used for signed integers, but it won't
  11. ! because there is a more clever one just for signed integers.
  12. .mlu2:
  13. pop h
  14. shld .retadr
  15. mov h,b
  16. mov l,c
  17. shld .bcreg
  18. pop b ! bc = multiplier
  19. pop d ! de = multiplicand
  20. lxi h,0 ! hl = product
  21. 1: mov a,b ! if multiplier = 0 then finished
  22. ora c
  23. jz 3f
  24. xra a ! reset carry
  25. mov a,b ! shift multiplier right
  26. rar
  27. mov b,a
  28. mov a,c
  29. rar
  30. mov c,a
  31. jnc 2f !if carry set: add multiplicand to product
  32. dad d
  33. 2: xchg ! shift multiplicand left
  34. dad h
  35. xchg
  36. jmp 1b ! keep looping
  37. 3: xchg ! de becomes product
  38. lhld .bcreg
  39. mov b,h
  40. mov c,l
  41. lhld .retadr
  42. pchl