www.pudn.com > iccavr_lib.rar > div16u.s, change:2003-11-08,size:1076b


; "div16u" - 16/16 Bit Unsigned Division 
; 
; This subroutine divides the two 16-bit numbers  
; r17:r16 (dividend) and r19:r18 (divisor).  
; The result is placed in r17:r16 and the remainder in r15:r14 
; Low registers used  :2 (r15:r14) 
; High registers used :5 (r16,r17,r18,r19,r24) 
 
  .text 
mod16u:: 
  set 
  rjmp  xdiv16u 
div16u::   
  clt 
xdiv16u:: 
  st    -y,R14 
  st    -y,R15 
  st    -y,r24 
  clr   r14       ; clear remainder 
  clr   r15 
  ldi   r24,16    ; init loop counter 
d16u_1: 
  lsl   r16       ; shift dividend/quotient left 
  rol   r17 
  rol   r14       ; shift dividend into remainder 
  rol   r15 
  cp    r14,r18   ; remainder <= divisor? 
  cpc   r15,r19 
  brcs  d16u_2    ; skip if > 
  sub   r14,r18   ; remainder -= divisor 
  sbc   r15,r19 
  inc   r16       ; quotient++, lsb = 0 
d16u_2: 
  dec   r24       ; decrement counter 
  brne  d16u_1    ; if not done 
  brtc  nocopy    ; if T is set, then copy the remainder R14/15 to R16/17 
  mov   r16,r14 
  mov   r17,r15 
nocopy: 
  ld r24,Y+ 
  ld R15,Y+ 
  ld R14,Y+ 
  ret