home *** CD-ROM | disk | FTP | other *** search
/ Software Collection (I) / TOOLS.iso / b05 / 6.img / PSCRIPT / SCALE.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-03-11  |  3.5 KB  |  121 lines

  1. ;/**[f******************************************************************
  2. ; * scale.a -
  3. ; *
  4. ; * Copyright (C) 1988 Aldus Corporation.  All rights reserved.
  5. ; * Copyright (C) 1989 Microsoft Corporation.
  6. ; * Company confidential.
  7. ; *
  8. ; **f]*****************************************************************/
  9.  
  10. TITLE Scale - signed 16 bit multiply and divide routine
  11.         subttl  Copyright (C) 1986 Aldus Corporation.  All rights reserved.
  12.         page    60,132
  13. ;
  14. ; abstract
  15. ;
  16. ; This module contains a c-language callable routine for a * b / c : A 16 bit
  17. ; signed and rounded multiply and divide with a 32 bit intermediate result.
  18. ;
  19. ; short Scale(a, b, c)
  20. ; short a;
  21. ; short b;
  22. ; short c;
  23. ;
  24. ;============================================================================
  25.  
  26. .xlist
  27.         ?PLM = 1                                ;no plm, use C calling convention
  28.         ?WIN = 0                                ;no, don't follow windows calling conventions
  29.  
  30.    include cmacros.inc                          ;formerly \include\cmacros.i
  31. .list
  32.  
  33. ;============================================================================
  34.  
  35. sBegin  CODE
  36. assumes CS,CODE
  37. assumes ds,DATA
  38.  
  39. ;============================================================================
  40. LabelFP <PUBLIC, Scale>
  41. ife ?PLM
  42.         mov     bx,sp
  43.         mov     ax,ss:[bx+4]
  44.         mov     dx,ss:[bx+6]
  45.         mov     cx,ss:[bx+8]
  46.         mov     bx,dx
  47. else
  48.         pop     dx                      ; pop return
  49.         pop     es
  50.         pop     cx                      ; c
  51.         pop     bx                      ; b
  52.         pop     ax                      ; a
  53.         push    es
  54.         push    dx                      ; push return
  55. endif
  56.         push    si
  57.         mov     si,7fffh
  58.  
  59.         imul    bx                      ; get a * b
  60.  
  61.         push    cx                      ; save divisor
  62.  
  63.         or      cx,cx                   ; get absolute value of divisor
  64.         jns     ab100
  65.         neg     cx
  66.         neg     si                      ; negate clip value
  67. ab100:
  68.         push    cx                      ; push positive divisor
  69.         shr     cx,1                    ; now divide divisor by 2 for rouding
  70.                                         ; factor
  71.  
  72.         or      dx,dx
  73.         jns     ab200                   ; make rounding factor negative if
  74.         neg     cx                      ; intermediate result is negative.
  75.         neg     si                      ; negate clip value
  76. ab200:
  77.  
  78.         or      cx,cx                   ; if we're dividing by 0, return
  79.         jnz     ab240                   ; maxshort of sign ab.
  80.         pop     cx
  81.         pop     cx
  82.         jmp     short ab260
  83.  
  84. ab240:
  85.         xchg    ax,cx
  86.         mov     bx,dx
  87.         cwd
  88.         add     ax,cx
  89.         adc     dx,bx   ; add in 32 bit rounding number
  90.  
  91.         or      dx,dx
  92.         jns     ab250
  93.  
  94.         xor     bx,0ffffh               ; make 32 bit number positive
  95.         xor     cx,0ffffh
  96.         add     cx,1
  97.         adc     bx,0
  98.  
  99. ab250:
  100.         rol     cx,1
  101.         rcl     bx,1                    ; div 32 bit number by 8000h.
  102.  
  103.         pop     cx                      ; get positive divisor
  104.         cmp     cx,bx
  105.         pop     cx                      ; get real divisor
  106.         ja      ab300
  107.  
  108. ab260:
  109.         mov     ax,si
  110.         pop     si
  111.         db      0cbh    ; retf
  112.  
  113. ab300:
  114.         idiv    cx                      ; now div by our divisor
  115.         pop     si
  116. ab400:
  117.         db      0cbh                    ; retf
  118.  
  119. sEnd
  120. end
  121.