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


	.text
;   div16s   - 16/16 Bit Signed Division

; This subroutine divides two signed 16 bit numbers 
; r17:r16 (dividend) and r19:r18 (divisor). 
; The result is placed in r17:r16.
; The remainder is the result if mod is called.

; Low registers used   :1 (r13)
; High registers used  :4 (r16,r17,r18,r19)

  .text
mod16s::
  set
  st    -y,r13
  mov   r13,r17   ; move dividend High to sign register
  rjmp xdiv16s
div16s::  
  clt
  st    -y,r13
  mov   r13,r17   ; move dividend High to sign register
  eor   r13,r19   ; xor divisor High with sign register
xdiv16s:
  sbrs  r17,7     ; if MSB in dividend set
  rjmp  d16s_1
  com   r17       ; change sign of dividend
  com   r16    
  subi  r16,0xff
  sbci  r17,0xff
d16s_1:
  sbrs  r19,7     ; if MSB in divisor set
  rjmp  d16s_2
  com   r19       ; change sign of divisor
  com   r18    
  subi  r18,0xff
  sbci  r19,0xff
d16s_2:
  rcall xdiv16u   ; do the divide
d16s_4:  
  sbrs  r13,7     ; if MSB in sign register set
  rjmp  noneg
  com   r17       ; change sign of result
  com   r16
  subi  r16,0xff
  sbci  r17,0xff
noneg:
  ld    R13,Y+
  ret