Skip to content

Commit 2960085

Browse files
committed
Make division code a little smaller.
This makes the code return different values on division by 0, but it is a little faster and smaller.
1 parent 26954ac commit 2960085

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/interp/div.asm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ x_pos: sta tmp3
8585
ldy #16
8686
lda #0
8787
sta tmp2+1
88+
; Detect we are dividing by numbers < 256 and use a faster algorithm.
89+
; The assumption is that smaller divisors are much more common than
90+
; larger ones, so this pays off.
8891
ldx tmp1+1
8992
beq udiv16x8
9093

@@ -111,11 +114,15 @@ L1: txa
111114
rts
112115

113116
udiv16x8:
114-
ldx tmp1
115-
beq L0
117+
; This is needed to return modulo > 255 on division by 0. We could also
118+
; set an error flag, but it is simpler to just return an invalid value.
119+
; ldx tmp1
120+
; beq L0
116121
L2: asl tmp3
117122
rol tmp3+1
118123
rol
124+
; When the current modulo is > 255, we always need to subtract the
125+
; divisor, and we already have C=1.
119126
bcs L3
120127

121128
cmp tmp1

0 commit comments

Comments
 (0)