div8.asm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ; --------------------------------
  2. __DIVU8: ; 8 bit unsigned integer division
  3. ; Divides (Top of stack, High Byte) / A
  4. ;- pop hl ; --------------------------------
  5. ;- ex (sp), hl ; CALLEE
  6. __DIVU8_FAST: ; Does A / H
  7. lda z80_h ;- ld l,h
  8. sta z80_l
  9. lda z80_a ;- ld h,a ; At this point do H / L
  10. sta z80_h
  11. lda %8 ;- ld b,8
  12. sta z80_b
  13. eor z80_a ;- xor a ; A = 0, Carry Flag = 0
  14. __DIV8LOOP:
  15. asl z80_h ;- sla h
  16. ;- rla
  17. cmp z80_l ;- cp l
  18. jcc __DIV8NOSUB ;- jr c,__DIV8NOSUB
  19. sec ;- sub l
  20. sbc z80_l
  21. inc z80_h ;- inc h
  22. __DIV8NOSUB:
  23. dec z80_b ;- djnz __DIV8LOOP
  24. jne __DIV8LOOP
  25. lda z80_a ;- ld l,a ; save remainder
  26. sta z80_l
  27. lda z80_h ;- ld a,h ;
  28. sta z80_a
  29. rts ;- ret ; a = Quotient,
  30. ; --------------------------------
  31. __DIVI8: ; 8 bit signed integer division Divides (Top of stack) / A
  32. ;- pop hl
  33. ; --------------------------------
  34. tsx ;- ex (sp),hl
  35. lda $0103,x
  36. ldy z80_h
  37. sta z80_h
  38. tya
  39. sta $0103,x
  40. lda $0104,x
  41. ldy z80_l
  42. sta z80_l
  43. tya
  44. sta $104,x
  45. __DIVI8_FAST:
  46. lda z80_a ;- ld e,a ; store operands for later
  47. sta z80_e
  48. lda z80_h ;- ld c,h
  49. sta z80_c
  50. ora z80_a ;- or a ; negative?
  51. jpl __DIV8A ;- jp p, __DIV8A
  52. eor #$ff ;- neg ; Make it positive
  53. clc
  54. adc #$01
  55. __DIV8A:
  56. ldx z80_ap ;- ex af,af'
  57. sta z80_ap
  58. txa
  59. lda z80_h ;- ld a,h
  60. sta z80_a
  61. ora z80_a ;- or a
  62. jpl __DIV8B ;- jp p, __DIV8B
  63. eor #$ff ;- neg
  64. clc
  65. adc #$01
  66. lda z80_a ;- ld h,a ; make it positive
  67. sta z80_h
  68. __DIV8B:
  69. ldx z80_ap ;- ex af,af'
  70. sta z80_ap
  71. txa
  72. jsr __DIVU8_FAST ;- call __DIVU8_FAST
  73. lda z80_c ;- ld a,c
  74. sta z80_a
  75. eor z80_l ;- xor l ; bit 7 of A = 1 if result is negative
  76. lda z80_h ;- ld a,h ; Quotient
  77. sta z80_a
  78. bmi *+3 ;- ret p ; return if positive
  79. rts
  80. eor #$ff ;- neg
  81. clc
  82. adc #$01
  83. rts ;- ret
  84. __MODU8: ; 8 bit module. REturns A mod (Top of stack) (unsigned operands)
  85. pla ;- pop hl
  86. sta z80_h
  87. pla
  88. sta z80_l
  89. tsx ;- ex (sp),hl ; CALLEE
  90. lda $0103,x
  91. ldy z80_h
  92. sta z80_h
  93. tya
  94. sta $0103,x
  95. lda $0104,x
  96. ldy z80_l
  97. sta z80_l
  98. tya
  99. sta $104,x
  100. __MODU8_FAST: ; __FASTCALL__ entry
  101. jsr __DIVU8_FAST ;- call __DIVU8_FAST
  102. lda z80_l ;- ld a,l ; Remainder
  103. sta z80_a
  104. rts ;- ret ; a = Modulus
  105. __MODI8: ; 8 bit module. REturns A mod (Top of stack) (For singed operands)
  106. pla ;- pop hl
  107. sta z80_h
  108. pla
  109. sta z80_l
  110. tsx ;- ex (sp),hl ; CALLEE
  111. lda $0103,x
  112. ldy z80_h
  113. sta z80_h
  114. tya
  115. sta $0103,x
  116. lda $0104,x
  117. ldy z80_l
  118. sta z80_l
  119. tya
  120. sta $104,x
  121. __MODI8_FAST: ; __FASTCALL__ entry
  122. jsr __DIVI8_FAST ;- call __DIVI8_FAST
  123. lda z80_l ;- ld a,l ; remainder
  124. sta z80_a
  125. rts ;- ret ; a = Modulus