Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.0 KiB

  1. title ulldvrm - unsigned long divide and remainder routine
  2. ;***
  3. ;ulldvrm.asm - unsigned long divide and remainder routine
  4. ;
  5. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  6. ;
  7. ;Purpose:
  8. ; defines the unsigned long divide and remainder routine
  9. ; __aulldvrm
  10. ;
  11. ;Revision History:
  12. ; 10-06-98 SMK Initial version.
  13. ;
  14. ;*******************************************************************************
  15. .xlist
  16. include cruntime.inc
  17. include mm.inc
  18. .list
  19. ;***
  20. ;ulldvrm - unsigned long divide and remainder
  21. ;
  22. ;Purpose:
  23. ; Does a unsigned long divide and remainder of the arguments. Arguments
  24. ; are not changed.
  25. ;
  26. ;Entry:
  27. ; Arguments are passed on the stack:
  28. ; 1st pushed: divisor (QWORD)
  29. ; 2nd pushed: dividend (QWORD)
  30. ;
  31. ;Exit:
  32. ; EDX:EAX contains the quotient (dividend/divisor)
  33. ; EBX:ECX contains the remainder (divided % divisor)
  34. ; NOTE: this routine removes the parameters from the stack.
  35. ;
  36. ;Uses:
  37. ; ECX
  38. ;
  39. ;Exceptions:
  40. ;
  41. ;*******************************************************************************
  42. CODESEG
  43. _aulldvrm PROC NEAR
  44. push esi
  45. ; Set up the local stack and save the index registers. When this is done
  46. ; the stack frame will look as follows (assuming that the expression a/b will
  47. ; generate a call to aulldvrm(a, b)):
  48. ;
  49. ; -----------------
  50. ; | |
  51. ; |---------------|
  52. ; | |
  53. ; |--divisor (b)--|
  54. ; | |
  55. ; |---------------|
  56. ; | |
  57. ; |--dividend (a)-|
  58. ; | |
  59. ; |---------------|
  60. ; | return addr** |
  61. ; |---------------|
  62. ; ESP---->| ESI |
  63. ; -----------------
  64. ;
  65. DVND equ [esp + 8] ; stack address of dividend (a)
  66. DVSR equ [esp + 16] ; stack address of divisor (b)
  67. ;
  68. ; Now do the divide. First look to see if the divisor is less than 4194304K.
  69. ; If so, then we can use a simple algorithm with word divides, otherwise
  70. ; things get a little more complex.
  71. ;
  72. mov eax,HIWORD(DVSR) ; check to see if divisor < 4194304K
  73. or eax,eax
  74. jnz short L1 ; nope, gotta do this the hard way
  75. mov ecx,LOWORD(DVSR) ; load divisor
  76. mov eax,HIWORD(DVND) ; load high word of dividend
  77. xor edx,edx
  78. div ecx ; get high order bits of quotient
  79. mov ebx,eax ; save high bits of quotient
  80. mov eax,LOWORD(DVND) ; edx:eax <- remainder:lo word of dividend
  81. div ecx ; get low order bits of quotient
  82. mov esi,eax ; ebx:esi <- quotient
  83. ;
  84. ; Now we need to do a multiply so that we can compute the remainder.
  85. ;
  86. mov eax,ebx ; set up high word of quotient
  87. mul dword ptr LOWORD(DVSR) ; HIWORD(QUOT) * DVSR
  88. mov ecx,eax ; save the result in ecx
  89. mov eax,esi ; set up low word of quotient
  90. mul dword ptr LOWORD(DVSR) ; LOWORD(QUOT) * DVSR
  91. add edx,ecx ; EDX:EAX = QUOT * DVSR
  92. jmp short L2 ; complete remainder calculation
  93. ;
  94. ; Here we do it the hard way. Remember, eax contains DVSRHI
  95. ;
  96. L1:
  97. mov ecx,eax ; ecx:ebx <- divisor
  98. mov ebx,LOWORD(DVSR)
  99. mov edx,HIWORD(DVND) ; edx:eax <- dividend
  100. mov eax,LOWORD(DVND)
  101. L3:
  102. shr ecx,1 ; shift divisor right one bit; hi bit <- 0
  103. rcr ebx,1
  104. shr edx,1 ; shift dividend right one bit; hi bit <- 0
  105. rcr eax,1
  106. or ecx,ecx
  107. jnz short L3 ; loop until divisor < 4194304K
  108. div ebx ; now divide, ignore remainder
  109. mov esi,eax ; save quotient
  110. ;
  111. ; We may be off by one, so to check, we will multiply the quotient
  112. ; by the divisor and check the result against the orignal dividend
  113. ; Note that we must also check for overflow, which can occur if the
  114. ; dividend is close to 2**64 and the quotient is off by 1.
  115. ;
  116. mul dword ptr HIWORD(DVSR) ; QUOT * HIWORD(DVSR)
  117. mov ecx,eax
  118. mov eax,LOWORD(DVSR)
  119. mul esi ; QUOT * LOWORD(DVSR)
  120. add edx,ecx ; EDX:EAX = QUOT * DVSR
  121. jc short L4 ; carry means Quotient is off by 1
  122. ;
  123. ; do long compare here between original dividend and the result of the
  124. ; multiply in edx:eax. If original is larger or equal, we are ok, otherwise
  125. ; subtract one (1) from the quotient.
  126. ;
  127. cmp edx,HIWORD(DVND) ; compare hi words of result and original
  128. ja short L4 ; if result > original, do subtract
  129. jb short L5 ; if result < original, we are ok
  130. cmp eax,LOWORD(DVND) ; hi words are equal, compare lo words
  131. jbe short L5 ; if less or equal we are ok, else subtract
  132. L4:
  133. dec esi ; subtract 1 from quotient
  134. sub eax,LOWORD(DVSR) ; subtract divisor from result
  135. sbb edx,HIWORD(DVSR)
  136. L5:
  137. xor ebx,ebx ; ebx:esi <- quotient
  138. L2:
  139. ;
  140. ; Calculate remainder by subtracting the result from the original dividend.
  141. ; Since the result is already in a register, we will do the subtract in the
  142. ; opposite direction and negate the result.
  143. ;
  144. sub eax,LOWORD(DVND) ; subtract dividend from result
  145. sbb edx,HIWORD(DVND)
  146. neg edx ; otherwise, negate the result
  147. neg eax
  148. sbb edx,0
  149. ;
  150. ; Now we need to get the quotient into edx:eax and the remainder into ebx:ecx.
  151. ;
  152. mov ecx,edx
  153. mov edx,ebx
  154. mov ebx,ecx
  155. mov ecx,eax
  156. mov eax,esi
  157. ;
  158. ; Just the cleanup left to do. edx:eax contains the quotient.
  159. ; Restore the saved registers and return.
  160. ;
  161. pop esi
  162. ret 16
  163. _aulldvrm ENDP
  164. end