## Unsigned Integer Division Routines

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Omegamatrix
Posts: 35
Joined: Tue Jun 10, 2014 8:15 pm

### Re: Unsigned Integer Division Routines

chitselb are you using 16 bit? I do have a 16 bit divide by 10 routine that you could tweak to get a div 40 and mod 40. That being said if you can just use multiply 40 then you probably way better off.

Code: Select all

``````;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;    UNSIGNED DIVIDE BY 10 (16 BIT)
;    By Omegamatrix
;    126 cycles (max), 79 bytes
;
;    End with 16 bit result in highTen, lowTen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

TensRemaining:
.byte 0,25,51,76,102,128,153,179,204,230
ModRemaing:
.byte 0,6,2,8,4,0,6,2,8,4

.startDivideBy10:
ldy    #-2                   ;2  @2   skips a branch the first time through
lda    counterHigh           ;3  @5
.do8bitDiv:
sta    temp                  ;3  @8
lsr                          ;2  @10
ror                          ;2  @17
lsr                          ;2  @19
lsr                          ;2  @21
ror                          ;2  @26
ror                          ;2  @31
lsr                          ;2  @33
and    #\$7C                  ;2  @35   AND'ing here...
sta    temp                  ;3  @38   and saving result as highTen (times 4)
lsr                          ;2  @40
lsr                          ;2  @42
iny                          ;2  @44
bpl    .finishLowTen         ;2³ @46/47...120

sta    highTen               ;3  @49
adc    temp                  ;3  @52   highTen (times 5)
asl                          ;2  @54   highTen (times 10)
sbc    counterHigh           ;3  @57
eor    #\$FF                  ;2  @59
tay                          ;2  @61   mod 10 result!

lda    TensRemaining,Y       ;4  @65   Fill the low byte with the tens it should
sta    lowTen                ;3  @68   have at this point from the high byte divide.
lda    counterLow            ;3  @71