Skip to content

Commit 5efe902

Browse files
authored
Merge pull request #10 from c128lib/johnpalermo/issue9
Add division 16/8 and 16/16.
2 parents 1574cf2 + 65d51c4 commit 5efe902

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

lib/math-global.asm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
.macro @c128lib_inc16(destination) { inc16(destination) }
1111
.macro @c128lib_dec16(destination) { dec16(destination) }
1212
.macro @c128lib_mulAndAdd(left, right, targetAddr) { mulAndAdd(left, right, targetAddr) }
13+
.macro @c128lib_div16By16(dividend, divisor, remainder) { div16By16(dividend, divisor, remainder) }
14+
.macro @c128lib_div16By8(dividend, divisor, remainder) { div16By8(dividend, divisor, remainder) }

lib/math.asm

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,77 @@
191191
dex
192192
bne !-
193193
}
194+
195+
/*
196+
Divides the two-byte number dividend by the two-byte number divisor, leaving the quotient in
197+
dividend and the remainder in remainder. Addressing mode of 16-bit numbers uses little endian.
198+
dividend - 2 bytes (quotient also 2 bytes)
199+
divisor - 2 bytes
200+
remainder - 2 bytes (must be as wide as divisor)
201+
202+
*/
203+
.macro div16By16(dividend, divisor, remainder) {
204+
lda #0
205+
sta remainder // Initialize remainder to 0.
206+
sta remainder+1
207+
ldx #16 // There are 16 bits in the dividend
208+
209+
loop1:
210+
/* Shift the hi bit of dividend into remainder */
211+
asl dividend
212+
rol dividend+1
213+
rol remainder
214+
rol remainder+1
215+
216+
/* Trial subtraction */
217+
lda remainder
218+
sec
219+
sbc divisor
220+
tay
221+
lda remainder+1
222+
sbc divisor+1
223+
224+
/* Check subtraction */
225+
bcc loop2 // Did subtraction succeed?
226+
sta remainder+1 // If yes, save it, else loop2
227+
sty remainder
228+
inc dividend // and record a 1 in the quotient
229+
230+
loop2:
231+
dex
232+
bne loop1
233+
234+
}
235+
236+
/*
237+
Divides the two-byte number dividend by the one-byte number divisor, leaving the quotient in
238+
dividend and the remainder in remainder. Addressing mode of 16-bit numbers uses little endian.
239+
dividend - 2 bytes (quotient also 2 bytes)
240+
divisor - 1 byte
241+
remainder - 1 byte (must be as wide as divisor)
242+
*/
243+
.macro div16By8(dividend, divisor, remainder) {
244+
lda #0
245+
sta remainder // Initialize remainder to 0.
246+
ldx #16 // There are 16 bits in the dividend
247+
loop1:
248+
/* Shift the hi bit of dividend into remainder */
249+
asl dividend
250+
rol dividend+1
251+
rol remainder
252+
253+
/* Trial subtraction */
254+
lda remainder
255+
sec
256+
sbc divisor
257+
258+
/* Check subtraction */
259+
bcc loop2 // Did subtraction succeed?
260+
sta remainder // If yes, save it, else loop2
261+
inc dividend // and record a 1 in the quotient
262+
263+
loop2:
264+
dex
265+
bne loop1
266+
267+
}

0 commit comments

Comments
 (0)