Avr 32/32 ;*****************************************************************;* "div32u" - 32/32 Bit Unsigned Division dd/dv = dres,drem
;*****************************************************************.def drem32u0=r12 ;L
.def drem32u1=r13
.def drem32u2=r14
.def drem32u3=r15 ;H
.def dres32u0=r16 ;L
.def dres32u1=r17
.def dres32u2=r18
.def dres32u3=r19 ;H
.def dd32u0 =r16 ;L
.def dd32u1 =r17
.def dd32u2 =r18
.def dd32u3 =r19 ;H
.def dv32u0 =r20 ;L
.def dv32u1 =r21
.def dv32u2 =r22
.def dv32u3 =r23 ;H
.def dcnt32u =r24
;***** Code
div32u: push temp
clr drem32u0 ;clear remainder Low byte
clr drem32u1
clr drem32u2
sub drem32u3,drem32u3;clear remainder High byte and carry
ldi dcnt32u,33 ;init loop counter
d32u_1: rol dd32u0 ;shift left dividend
rol dd32u1
rol dd32u2
rol dd32u3
dec dcnt32u ;decrement counter
brne d32u_2 ;if done
pop temp
ret ; return
d32u_2: rol drem32u0 ;shift dividend into remainder
rol drem32u1
rol drem32u2
rol drem32u3
sub drem32u0,dv32u0 ;remainder = remainder - divisor
sbc drem32u1,dv32u1 ;
sbc drem32u2,dv32u2 ;
sbc drem32u3,dv32u3 ;
brcc d32u_3 ;if result negative
add drem32u0,dv32u0 ; restore remainder
adc drem32u1,dv32u1
adc drem32u2,dv32u2
adc drem32u3,dv32u3
clc ; clear carry to be shifted into result
rjmp d32u_1 ;else
d32u_3: sec ; set carry to be shifted into result
rjmp d32u_1
;*****************************************************************.def drem32u0=r12 ;L
.def drem32u1=r13
.def drem32u2=r14
.def drem32u3=r15 ;H
.def dres32u0=r16 ;L
.def dres32u1=r17
.def dres32u2=r18
.def dres32u3=r19 ;H
.def dd32u0 =r16 ;L
.def dd32u1 =r17
.def dd32u2 =r18
.def dd32u3 =r19 ;H
.def dv32u0 =r20 ;L
.def dv32u1 =r21
.def dv32u2 =r22
.def dv32u3 =r23 ;H
.def dcnt32u =r24
;***** Code
div32u: push temp
clr drem32u0 ;clear remainder Low byte
clr drem32u1
clr drem32u2
sub drem32u3,drem32u3;clear remainder High byte and carry
ldi dcnt32u,33 ;init loop counter
d32u_1: rol dd32u0 ;shift left dividend
rol dd32u1
rol dd32u2
rol dd32u3
dec dcnt32u ;decrement counter
brne d32u_2 ;if done
pop temp
ret ; return
d32u_2: rol drem32u0 ;shift dividend into remainder
rol drem32u1
rol drem32u2
rol drem32u3
sub drem32u0,dv32u0 ;remainder = remainder - divisor
sbc drem32u1,dv32u1 ;
sbc drem32u2,dv32u2 ;
sbc drem32u3,dv32u3 ;
brcc d32u_3 ;if result negative
add drem32u0,dv32u0 ; restore remainder
adc drem32u1,dv32u1
adc drem32u2,dv32u2
adc drem32u3,dv32u3
clc ; clear carry to be shifted into result
rjmp d32u_1 ;else
d32u_3: sec ; set carry to be shifted into result
rjmp d32u_1