mli4.s 1.7 KB

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