|
1 | | -#include once <neg32.asm> |
2 | | - |
3 | | - ; --------------------------------------------------------- |
4 | | - push namespace core |
5 | | - |
6 | | -__DIVU32: ; 32 bit unsigned division |
7 | | - ; DEHL = Dividend, Stack Top = Divisor |
8 | | - ; OPERANDS P = Dividend, Q = Divisor => OPERATION => P / Q |
9 | | - ; |
10 | | - ; Changes A, BC DE HL B'C' D'E' H'L' |
11 | | - ; --------------------------------------------------------- |
12 | | - exx |
13 | | - pop hl ; return address |
14 | | - pop de ; low part |
15 | | - ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend |
16 | | - |
17 | | -__DIVU32START: ; Performs D'E'H'L' / HLDE |
18 | | - ; Now switch to DIVIDEND = B'C'BC / DIVISOR = D'E'DE (A / B) |
19 | | - push de ; push Lowpart(Q) |
20 | | - ex de, hl ; DE = HL |
21 | | - ld hl, 0 |
22 | | - exx |
23 | | - ld b, h |
24 | | - ld c, l |
25 | | - pop hl |
26 | | - push de |
27 | | - ex de, hl |
28 | | - ld hl, 0 ; H'L'HL = 0 |
29 | | - exx |
30 | | - pop bc ; Pop HightPart(B) => B = B'C'BC |
31 | | - exx |
32 | | - |
33 | | - ld a, 32 ; Loop count |
34 | | - |
35 | | -__DIV32LOOP: |
36 | | - sll c ; B'C'BC << 1 ; Output most left bit to carry |
37 | | - rl b |
38 | | - exx |
39 | | - rl c |
40 | | - rl b |
41 | | - exx |
42 | | - |
43 | | - adc hl, hl |
44 | | - exx |
45 | | - adc hl, hl |
46 | | - exx |
47 | | - |
48 | | - sbc hl,de |
49 | | - exx |
50 | | - sbc hl,de |
51 | | - exx |
52 | | - jp nc, __DIV32NOADD ; use JP inside a loop for being faster |
53 | | - |
54 | | - add hl, de |
55 | | - exx |
56 | | - adc hl, de |
57 | | - exx |
58 | | - dec bc |
59 | | - |
60 | | -__DIV32NOADD: |
61 | | - dec a |
62 | | - jp nz, __DIV32LOOP ; use JP inside a loop for being faster |
63 | | - ; At this point, quotient is stored in B'C'BC and the reminder in H'L'HL |
64 | | - |
65 | | - push hl |
66 | | - exx |
67 | | - pop de |
68 | | - ex de, hl ; D'E'H'L' = 32 bits modulus |
69 | | - push bc |
70 | | - exx |
71 | | - pop de ; DE = B'C' |
72 | | - ld h, b |
73 | | - ld l, c ; DEHL = quotient D'E'H'L' = Modulus |
74 | | - |
75 | | - ret ; DEHL = quotient, D'E'H'L' = Modulus |
76 | | - |
77 | | - |
78 | | - |
79 | | -__MODU32: ; 32 bit modulus for 32bit unsigned division |
80 | | - ; DEHL = Dividend, Stack Top = Divisor (DE, HL) |
81 | | - |
82 | | - exx |
83 | | - pop hl ; return address |
84 | | - pop de ; low part |
85 | | - ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend |
86 | | - |
87 | | - call __DIVU32START ; At return, modulus is at D'E'H'L' |
88 | | - |
89 | | -__MODU32START: |
90 | | - |
91 | | - exx |
92 | | - push de |
93 | | - push hl |
94 | | - |
95 | | - exx |
96 | | - pop hl |
97 | | - pop de |
98 | | - |
99 | | - ret |
100 | | - |
101 | | - |
102 | | -__DIVI32: ; 32 bit signed division |
103 | | - ; DEHL = Dividend, Stack Top = Divisor |
104 | | - ; A = Dividend, B = Divisor => A / B |
105 | | - exx |
106 | | - pop hl ; return address |
107 | | - pop de ; low part |
108 | | - ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend |
109 | | - |
110 | | -__DIVI32START: |
111 | | - exx |
112 | | - ld a, d ; Save sign |
113 | | - ex af, af' |
114 | | - bit 7, d ; Negative? |
115 | | - call nz, __NEG32 ; Negates DEHL |
116 | | - |
117 | | - exx ; Now works with H'L'D'E' |
118 | | - ex af, af' |
119 | | - xor h |
120 | | - ex af, af' ; Stores sign of the result for later |
121 | | - |
122 | | - bit 7, h ; Negative? |
123 | | - ex de, hl ; HLDE = DEHL |
124 | | - call nz, __NEG32 |
125 | | - ex de, hl |
126 | | - |
127 | | - call __DIVU32START |
128 | | - ex af, af' ; Recovers sign |
129 | | - and 128 ; positive? |
130 | | - ret z |
131 | | - |
132 | | - jp __NEG32 ; Negates DEHL and returns from there |
133 | | - |
134 | | - |
135 | | -__MODI32: ; 32bits signed division modulus |
136 | | - exx |
137 | | - pop hl ; return address |
138 | | - pop de ; low part |
139 | | - ex (sp), hl ; CALLEE Convention ; H'L'D'E' => Dividend |
140 | | - |
141 | | - call __DIVI32START |
142 | | - jp __MODU32START |
143 | | - |
144 | | - pop namespace |
| 1 | +' ---------------------------------------------------------------- |
| 2 | +' This file is released under the MIT License |
| 3 | +' |
| 4 | +' Copyleft (k) 2008 |
| 5 | +' by Jose Rodriguez-Rosa (a.k.a. Boriel) <https://www.boriel.com> |
| 6 | +' ---------------------------------------------------------------- |
| 7 | + |
| 8 | +#include once [arch:zx48k] <arith/div32.asm> |
0 commit comments