home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / misc / vfwdk / samples / bravado / muldiv.asm < prev    next >
Assembly Source File  |  1993-01-31  |  3KB  |  115 lines

  1.         TITLE MULDIV.ASM
  2.         page 60,132
  3.  
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5. ;
  6. ; MULDIV.ASM - Fast 32 bit multiplies
  7. ;
  8. ; (C) Copyright Microsoft Corp. 1992-1993.  All rights reserved.
  9. ;
  10. ; You have a royalty-free right to use, modify, reproduce and 
  11. ; distribute the Sample Files (and/or any modified version) in 
  12. ; any way you find useful, provided that you agree that 
  13. ; Microsoft has no warranty obligations or liability for any 
  14. ; Sample Application Files which are modified. 
  15. ;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. ?WIN    = 0
  19. ?PLM    = 1
  20. ?NODATA = 0
  21. PMODE   = 1
  22.  
  23.         .xlist
  24.         include cmacros.inc
  25.         include windows.inc
  26.         .list
  27.  
  28. ;       The following two equates are just used as shorthand
  29. ;       for the "word ptr" and "byte ptr" overrides.
  30.  
  31. wptr    equ     word ptr
  32. bptr    equ     byte ptr
  33.  
  34. ; The following structure should be used to access high and low
  35. ; words of a DWORD.  This means that "word ptr foo[2]" -> "foo.hi".
  36.  
  37. LONG    struc
  38. lo      dw      ?
  39. hi      dw      ?
  40. LONG    ends
  41.  
  42. EAXtoDXAX   macro
  43.         shld    edx,eax,16      ; move HIWORD(eax) to dx
  44.         endm
  45.  
  46. DXAXtoEAX   macro
  47.         ror     eax,16          ; xchg HIWORD(eax) and LOWORD(eax)
  48.         shrd    eax,edx,16      ; move LOWORD(edx) to HIWORD(eax)
  49.         endm
  50.  
  51. ifndef SEGNAME
  52.     SEGNAME equ <_TEXT>
  53. endif
  54.  
  55. createSeg %SEGNAME, CodeSeg, word, public, CODE
  56.  
  57. sBegin  CodeSeg
  58.         .386
  59.         assumes cs,CodeSeg
  60.     assumes ds,nothing
  61.         assumes es,nothing
  62.  
  63. ;---------------------------Public-Routine------------------------------;
  64. ; muldiv32
  65. ;
  66. ; multiples two 32 bit values and then divides the result by a third
  67. ; 32 bit value with full 64 bit presision
  68. ;
  69. ; ulResult = (ulNumber * ulNumerator) / ulDenominator
  70. ;
  71. ; Entry:
  72. ;       dwNumber = number to multiply by nNumerator
  73. ;       dwNumerator = number to multiply by nNumber
  74. ;       dwDenominator = number to divide the multiplication result by.
  75. ;   
  76. ; Returns:
  77. ;       DX:AX = result of multiplication and division.
  78. ; Error Returns:
  79. ;       none
  80. ; Registers Preserved:
  81. ;       DS,ES,SI,DI
  82. ;-----------------------------------------------------------------------;
  83.         assumes ds,nothing
  84.         assumes es,nothing
  85.  
  86. cProc   muldiv32,<NEAR,PUBLIC,NODATA,NONWIN>,<>
  87. ;       ParmD  ulNumber
  88. ;       ParmD  ulNumerator
  89. ;       ParmD  ulDenominator
  90. cBegin  nogen
  91.         .386
  92.  
  93.         pop     cx      ; get return addr
  94.         pop     ebx     ; get ulDenominator
  95.         pop     edx     ; get ulNumerator
  96.         pop     eax     ; get ulNumber
  97.  
  98. ; unsigned version 
  99. ;        mul     edx     ; edx:eax = (ulNumber * ulNumerator)
  100. ;        div     ebx     ; eax     = (ulNumber * ulNumerator) / ulDenominator
  101.  
  102. ; signed version 
  103.         imul     edx     ; edx:eax = (ulNumber * ulNumerator)
  104.         idiv     ebx     ; eax     = (ulNumber * ulNumerator) / ulDenominator
  105.         EAXtoDXAX       ; covert eax to dx:ax for 16 bit programs
  106.  
  107.         push    cx      ; retore return addr
  108.         ret
  109.  
  110. cEnd    nogen
  111.  
  112. sEnd   CodeSeg
  113.  
  114.        end
  115.