2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 07.08.1998
|
|
|
|
;
|
|
|
|
; CC65 runtime: division for unsigned ints
|
|
|
|
;
|
2018-05-24 01:55:40 +00:00
|
|
|
; Don't use tmp1 here, the signed division tunnels data with it!
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export tosudiva0, tosudivax, udiv16
|
2018-05-24 01:55:40 +00:00
|
|
|
.import popptr1
|
2013-05-09 11:56:54 +00:00
|
|
|
.importzp sreg, ptr1, ptr4
|
2009-08-19 14:51:16 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
tosudiva0:
|
2009-08-19 14:51:16 +00:00
|
|
|
ldx #$00 ; Clear high byte
|
2000-05-28 13:40:48 +00:00
|
|
|
tosudivax:
|
2013-05-09 11:56:54 +00:00
|
|
|
sta ptr4
|
|
|
|
stx ptr4+1 ; Save right operand
|
2018-05-24 01:55:40 +00:00
|
|
|
jsr popptr1 ; Get left operand
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
; Do the division
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr udiv16
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
; Result is in ptr1, remainder in sreg
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
lda ptr1
|
|
|
|
ldx ptr1+1
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-08-19 14:51:16 +00:00
|
|
|
;---------------------------------------------------------------------------
|
2018-05-24 01:55:40 +00:00
|
|
|
; 16by16 division. Divide ptr1 by ptr4. Result is in ptr1, remainder in sreg
|
2009-08-19 14:51:16 +00:00
|
|
|
; (see mult-div.s from "The Fridge").
|
2000-05-28 13:40:48 +00:00
|
|
|
; This is also the entry point for the signed division
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
udiv16: lda #0
|
2018-05-24 01:55:40 +00:00
|
|
|
sta sreg+1
|
2013-05-09 11:56:54 +00:00
|
|
|
ldy #16
|
2011-01-11 17:08:29 +00:00
|
|
|
ldx ptr4+1
|
2009-08-19 14:51:16 +00:00
|
|
|
beq udiv16by8a
|
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
L0: asl ptr1
|
2013-05-09 11:56:54 +00:00
|
|
|
rol ptr1+1
|
2018-05-24 01:55:40 +00:00
|
|
|
rol a
|
|
|
|
rol sreg+1
|
2009-08-19 14:51:16 +00:00
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
tax
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp ptr4
|
2018-05-24 01:55:40 +00:00
|
|
|
lda sreg+1
|
2013-05-09 11:56:54 +00:00
|
|
|
sbc ptr4+1
|
|
|
|
bcc L1
|
2009-08-19 14:51:16 +00:00
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
sta sreg+1
|
|
|
|
txa
|
2013-05-09 11:56:54 +00:00
|
|
|
sbc ptr4
|
2018-05-24 01:55:40 +00:00
|
|
|
tax
|
|
|
|
inc ptr1
|
2009-08-19 14:51:16 +00:00
|
|
|
|
2018-05-24 01:55:40 +00:00
|
|
|
L1: txa
|
2013-05-09 11:56:54 +00:00
|
|
|
dey
|
|
|
|
bne L0
|
2018-05-24 01:55:40 +00:00
|
|
|
sta sreg
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2009-08-19 14:51:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
; 16by8 division
|
|
|
|
|
|
|
|
udiv16by8a:
|
2018-05-24 01:55:40 +00:00
|
|
|
@L0: asl ptr1
|
|
|
|
rol ptr1+1
|
2013-05-09 11:56:54 +00:00
|
|
|
rol a
|
2009-08-19 14:51:16 +00:00
|
|
|
bcs @L1
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
cmp ptr4
|
|
|
|
bcc @L2
|
|
|
|
@L1: sbc ptr4
|
2018-05-24 01:55:40 +00:00
|
|
|
inc ptr1
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
@L2: dey
|
|
|
|
bne @L0
|
2018-05-24 01:55:40 +00:00
|
|
|
sta sreg
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|