<asm>
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;-- Подпрограмы и процедуры.
;
;---------------------------------------------
;"DIV16U" - 16/16 Bit беззнаковое деление.
; -- Модифицированная Библитечная процедура.
;=============================================
; "OP2_RES0" (делимое), "OP1" (делитель).
; "OP2_RES0" (частное), "OPHI_RES1"(остаток).
; "остаток" - именно остаток от деления, например:
; 5678/1234=4.601 4=int,742=remainder (742/1234=0.601)
;---------------------------------------------
DIV16U: LDI_ OPHI_RES1L,0 ;clear remainder Low byte
sub OPHI_RES1H,OPHI_RES1L ;clear remainder High byte and carry
ldi COUNTER,17 ;init loop counter
D16U_1: rol OP2L_RES0L ;shift left dividend
rol OP2H_RES0H
dec COUNTER ;decrement counter
brne D16U_2 ;if done
ret ; return
D16U_2: rol OPHI_RES1L ;shift dividend into remainder
rol OPHI_RES1H
sub OPHI_RES1L,OP1L ;remainder = remainder - divisor
sbc OPHI_RES1H,OP1H ;
brcc D16U_3 ;if result negative
add OPHI_RES1L,OP1L ; restore remainder
adc OPHI_RES1H,OP1H
clc ; clear carry to be shifted into result
rjmp D16U_1 ;else
D16U_3: sec ; set carry to be shifted into result
rjmp D16U_1
ret
;---------------------------------------------
;"DIV32U16" - 32/16 Bit беззнаковое деление.
</asm>