2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 07.08.1998
|
|
|
|
;
|
|
|
|
; CC65 runtime: division for unsigned ints
|
|
|
|
;
|
|
|
|
|
|
|
|
.export tosudiva0, tosudivax, udiv16
|
2009-08-19 14:51:16 +00:00
|
|
|
.import popsreg
|
|
|
|
.importzp sreg, ptr1, ptr4
|
|
|
|
|
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:
|
|
|
|
sta ptr4
|
|
|
|
stx ptr4+1 ; Save right operand
|
|
|
|
jsr popsreg ; Get left operand
|
|
|
|
|
|
|
|
; Do the division
|
|
|
|
|
|
|
|
jsr udiv16
|
|
|
|
|
|
|
|
; Result is in sreg, remainder in ptr1
|
|
|
|
|
|
|
|
lda sreg
|
|
|
|
ldx sreg+1
|
|
|
|
rts
|
|
|
|
|
2009-08-19 14:51:16 +00:00
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
; 16by16 division. Divide sreg by ptr4. Result is in sreg, remainder in ptr1
|
|
|
|
; (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
|
|
|
|
|
|
|
|
udiv16: lda #0
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #16
|
2009-08-19 14:51:16 +00:00
|
|
|
ldx sreg+1
|
|
|
|
beq udiv16by8a
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
L0: asl sreg
|
|
|
|
rol sreg+1
|
|
|
|
rol a
|
|
|
|
rol ptr1+1
|
2009-08-19 14:51:16 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
pha
|
|
|
|
cmp ptr4
|
|
|
|
lda ptr1+1
|
|
|
|
sbc ptr4+1
|
2009-08-19 14:51:16 +00:00
|
|
|
bcc L1
|
|
|
|
|
|
|
|
sta ptr1+1
|
|
|
|
pla
|
|
|
|
sbc ptr4
|
|
|
|
pha
|
|
|
|
inc sreg
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
L1: pla
|
2009-08-19 14:51:16 +00:00
|
|
|
dey
|
|
|
|
bne L0
|
|
|
|
sta ptr1
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
|
|
|
; 16by8 division
|
|
|
|
|
|
|
|
udiv16by8:
|
|
|
|
lda #0
|
|
|
|
sta ptr1+1
|
|
|
|
ldy #16
|
|
|
|
udiv16by8a:
|
|
|
|
@L0: asl sreg
|
|
|
|
rol sreg+1
|
|
|
|
rol a
|
|
|
|
bcs @L1
|
|
|
|
|
|
|
|
cmp ptr4
|
|
|
|
bcc @L2
|
|
|
|
@L1: sbc ptr4
|
|
|
|
inc sreg
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-08-19 14:51:16 +00:00
|
|
|
@L2: dey
|
|
|
|
bne @L0
|
|
|
|
sta ptr1
|
|
|
|
rts
|
2000-05-28 13:40:48 +00:00
|
|
|
|