mli.s 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. .define Mli2, Mlinp, Mul
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! The subroutine Mli2 multiplies two signed integers. The integers
  8. ! are popped from the stack.
  9. ! The subroutine Mlinp expects the two integer to be in zeropage.
  10. ! While the subroutine Mul an unsigned multiply subroutine is.
  11. ! For the algoritme see A. S. Tanenbaum
  12. ! Structured Computer Organisation. 1976.
  13. Mli2:
  14. stx ARTH
  15. sta ARTH+1
  16. jsr Pop
  17. stx ARTH+2
  18. sta ARTH+3
  19. Mlinp: ldy #1
  20. sty UNSIGN ! it's signed
  21. lda ARTH+1
  22. bpl 3f ! multiplier negative so:
  23. ldx ARTH
  24. jsr Ngi2 ! negate multiplier
  25. stx ARTH
  26. sta ARTH+1
  27. ldx ARTH+2
  28. lda ARTH+3
  29. jsr Ngi2 ! negate multiplicand
  30. stx ARTH+2
  31. sta ARTH+3
  32. Mul:
  33. 3: lda #0
  34. sta ARTH+4
  35. sta ARTH+5
  36. sta ARTH+6
  37. sta ARTH+7 ! clear accumulator
  38. ldy #16
  39. 1: lda #0x01
  40. bit ARTH
  41. beq 2f ! multiplying by zero: no addition
  42. clc
  43. lda ARTH+6
  44. adc ARTH+2
  45. sta ARTH+6
  46. lda ARTH+7
  47. adc ARTH+3
  48. sta ARTH+7
  49. 2: lsr ARTH+1
  50. ror ARTH ! shift multiplier
  51. lsr ARTH+7
  52. ror ARTH+6
  53. ror ARTH+5
  54. ror ARTH+4 ! shift accumulator
  55. lda UNSIGN
  56. beq 3f ! unsigned multiply: so no shift in of signbit
  57. lda ARTH+3
  58. bpl 3f
  59. lda #0x40
  60. bit ARTH+7
  61. beq 3f
  62. lda ARTH+7
  63. ora #0x80
  64. sta ARTH+7
  65. 3: dey
  66. bne 1b
  67. ldx ARTH+4
  68. lda ARTH+5
  69. rts