1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-07 21:10:15 +00:00
cc65/libsrc/runtime/udiv.s

84 lines
1.6 KiB
ArmAsm
Raw Normal View History

;
; 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!
.export tosudiva0, tosudivax, udiv16
2018-05-24 01:55:40 +00:00
.import popptr1
.importzp sreg, ptr1, ptr4
tosudiva0:
ldx #$00 ; Clear high byte
tosudivax:
sta ptr4
stx ptr4+1 ; Save right operand
2018-05-24 01:55:40 +00:00
jsr popptr1 ; Get left operand
; Do the division
jsr udiv16
2018-05-24 01:55:40 +00:00
; Result is in ptr1, remainder in sreg
2018-05-24 01:55:40 +00:00
lda ptr1
ldx ptr1+1
rts
;---------------------------------------------------------------------------
2018-05-24 01:55:40 +00:00
; 16by16 division. Divide ptr1 by ptr4. Result is in ptr1, remainder in sreg
; (see mult-div.s from "The Fridge").
; This is also the entry point for the signed division
udiv16: lda #0
2018-05-24 01:55:40 +00:00
sta sreg+1
ldy #16
ldx ptr4+1
beq udiv16by8a
2018-05-24 01:55:40 +00:00
L0: asl ptr1
rol ptr1+1
2018-05-24 01:55:40 +00:00
rol a
rol sreg+1
2018-05-24 01:55:40 +00:00
tax
cmp ptr4
2018-05-24 01:55:40 +00:00
lda sreg+1
sbc ptr4+1
bcc L1
2018-05-24 01:55:40 +00:00
sta sreg+1
txa
sbc ptr4
2018-05-24 01:55:40 +00:00
tax
inc ptr1
2018-05-24 01:55:40 +00:00
L1: txa
dey
bne L0
2018-05-24 01:55:40 +00:00
sta sreg
rts
;---------------------------------------------------------------------------
; 16by8 division
udiv16by8a:
2018-05-24 01:55:40 +00:00
@L0: asl ptr1
rol ptr1+1
rol a
bcs @L1
cmp ptr4
bcc @L2
@L1: sbc ptr4
2018-05-24 01:55:40 +00:00
inc ptr1
@L2: dey
bne @L0
2018-05-24 01:55:40 +00:00
sta sreg
rts