dvi.s 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. .define Dvi2, Div, Duv
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! The subroutine Dvi2 performs a signed division.
  8. ! Its operands are on the stack.
  9. ! The subroutine Div performs also a signed division, ecxept that
  10. ! its operand are already in zeropage.
  11. ! The subroutine Duv performs a n unsigned division.
  12. ! For an explanation of the algoritm used see
  13. ! A. S. Tanenbaum's Structered Computer Organisation. 1976
  14. Dvi2:
  15. stx ARTH
  16. sta ARTH+1 ! store divisor
  17. jsr Pop
  18. stx ARTH+2
  19. sta ARTH+3 ! store dividend
  20. ldy #1
  21. sty UNSIGN ! used for result sign
  22. Div:
  23. ldy #0
  24. sty SIGN
  25. lda ARTH+1
  26. bpl 1f ! if divisor is negative
  27. ldx ARTH ! make it positive
  28. jsr Ngi2
  29. ldy #1
  30. sty SIGN
  31. stx ARTH
  32. sta ARTH+1
  33. 1: lda ARTH+3
  34. bpl 1f ! if dividend is negative
  35. ldx ARTH+2 ! make it positive
  36. jsr Ngi2
  37. pha
  38. lda SIGN
  39. eor #1 ! excusive or with sign of divisor
  40. sta SIGN
  41. lda #1
  42. sta NBYTES
  43. pla
  44. stx ARTH+2
  45. sta ARTH+3
  46. Duv:
  47. 1: ldy #0
  48. sty ARTH+4
  49. sty ARTH+5
  50. ldy #17
  51. 4: lda ARTH+5
  52. cmp ARTH+1
  53. bcc 1f ! no subtraction
  54. bne 2f ! divisor goes into dividend
  55. lda ARTH+4
  56. cmp ARTH
  57. bcc 1f ! no subtraction
  58. 2: sec ! divisor goes into dividend
  59. lda ARTH+4
  60. sbc ARTH
  61. sta ARTH+4
  62. lda ARTH+5
  63. sbc ARTH+1
  64. sta ARTH+5 ! subtract divisor from dividend
  65. sec
  66. rol ARTH+2 ! a subtraction so shift in a 1
  67. bne 3f
  68. 1: asl ARTH+2 ! no subtraction so shift in a 0
  69. 3: rol ARTH+3
  70. rol ARTH+4
  71. rol ARTH+5 ! shift dividend
  72. dey
  73. bne 4b
  74. ldx ARTH+2
  75. lda ARTH+3
  76. ldy UNSIGN ! is it an unsigned division
  77. beq 1f
  78. ldy SIGN ! is the result negative
  79. beq 1f
  80. jsr Ngi2
  81. 1: rts