mli4.s 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. .define .mli4
  2. ! 32-bit multiply routine for z80
  3. ! parameters:
  4. ! on stack
  5. ! register utilization:
  6. ! ix: least significant 2 bytes of result
  7. ! hl: most significant 2 bytes of result
  8. ! bc: least significant 2 bytes of multiplicand
  9. ! de: most significant 2 bytes of multiplicand
  10. ! iy: 2 bytes of multiplier (first most significant,
  11. ! later least significant)
  12. ! a: bit count
  13. .mli4:
  14. !initialization
  15. pop hl ! return address
  16. pop de
  17. ld (.mplier+2),de! least significant bytes of
  18. ! multiplier
  19. pop de
  20. ld (.mplier),de ! most sign. bytes
  21. pop de ! least significant bytes of
  22. ! multiplicand
  23. pop bc ! most sign. bytes
  24. push hl ! return address
  25. push iy ! LB
  26. ld ix,0
  27. xor a
  28. ld h,a ! clear result
  29. ld l,a
  30. ld (.flag),a ! indicate that this is
  31. ! first pass of main loop
  32. ld iy,(.mplier)
  33. ! main loop, done twice, once for each part (2 bytes)
  34. ! of multiplier
  35. 1:
  36. ld a,16
  37. ! sub-loop, done 16 times
  38. 2:
  39. add iy,iy ! shift left multiplier
  40. jr nc,3f ! skip if most sign. bit is 0
  41. add ix,de ! 32-bit add
  42. adc hl,bc
  43. 3:
  44. dec a
  45. jr z,4f ! done with this part of multiplier
  46. add ix,ix ! 32-bit shift left
  47. adc hl,hl
  48. jr 2b
  49. 4:
  50. ! see if we have just processed the first part
  51. ! of the multiplier (flag = 0) or the second
  52. ! part (flag = 1)
  53. ld a,(.flag)
  54. or a
  55. jr nz,5f
  56. inc a ! a := 1
  57. ld (.flag),a ! set flag
  58. ld iy,(.mplier+2)! least significant 2 bytes now in iy
  59. add ix,ix ! 32-bit shift left
  60. adc hl,hl
  61. jr 1b
  62. 5:
  63. ! clean up
  64. pop iy ! restore LB
  65. ex (sp),hl ! put most sign. 2 bytes of result
  66. ! on stack; put return address in hl
  67. push ix ! least sign. 2 bytes of result
  68. jp (hl) ! return
  69. .data
  70. .flag: .byte 0
  71. .mplier: .space 4