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.

174 lines
4.6 KiB

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