home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n03 / 80x87.exe / POW.ASM < prev    next >
Encoding:
Assembly Source File  |  1992-01-24  |  896 b   |  34 lines

  1.  
  2. ; pow.asm: integer power function callable from Borland C++.
  3. ; Copyright (C) 1991 by Nicholas Wilt.  All rights reserved.
  4.  
  5. .MODEL  LARGE,C
  6.  
  7. .CODE
  8.  
  9. ; double intpow(double x, unsigned int y);
  10. ; Returns x^y.
  11.  
  12.     PUBLIC  intpow
  13.  
  14. intpow  PROC    X:QWORD,Y:WORD
  15.     fld1            ; Load 1 into the 80x87
  16.     mov cx,Y        ; Get y
  17.     fld X       ; Load x into the 80x87
  18.  
  19.     jcxz    Return      ; If y already zero, return
  20.  
  21. TestY:  test    cx,1        ; Is the LSB of y set?
  22.     jz  NextIteration   ; Jump if no
  23.     fmul    st(1),st    ; ret *= x
  24. NextIteration:
  25.     fmul    st,st(0)    ; Square x
  26.     shr cx,1        ; y >>= 1
  27.     jnz TestY       ; Continue if nonzero
  28. Return: fstp    st      ; Pop stack.  Return value is
  29.                 ; now in ST(0).
  30.     ret
  31. intpow  ENDP
  32.  
  33.     END
  34.