News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

converting javascript to asm

Started by Siekmanski, September 17, 2014, 11:22:26 PM

Previous topic - Next topic

Siekmanski

Today i was looking at some javascript source code.
How do i calculate this ?

Math.pow(3.0, 0.4)

I know that "Math.pow(3.0, 4.0)" == 3.0 * 3.0 * 3.0 * 3.0 = 81.0
Creative coders use backward thinking techniques as a strategy.

hutch--

See if there is a VCRT function that will do powers.

jj2007

#2
include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init

  Print Str$("Math.pow(3.0, 0.4) = %f", ExpXY(3, 0.4))

  invoke crt_pow, FP8(3.0), FP8(0.4)
  Inkey Str$("CRT Math.pow(3.0, 0.4) = %If\n", ST(0))

  Exit
end start


Output:
MB  Math.pow(3.0, 0.4) = 1.55184558407721623
CRT Math.pow(3.0, 0.4) = 1.55184557391535971

TouEnMasm

msvcrt,
pow, powf, powl
Headers downloadable in this site.
Fa is a musical note to play with CL

TouEnMasm

Fa is a musical note to play with CL

dedndave

you could use 80-bit (64-bit) precision with the FPU via logarithms
i don't know if there's an SSE equiv

XY = alog(Y*log(X))

F2XM1     2 to the X power Minus 1
FSCALE    SCALE ST(0) by ST(1)
FYL2X     Y*Log2X
FYL2XP1   Y*Log2(X+1)


exponentiation can also be done with a Taylor Series

jj2007

Quote from: dedndave on September 18, 2014, 03:23:07 AM
you could use 80-bit (64-bit) precision with the FPU via logarithms
include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  PrintLine "Exact (Wolfram Alpha)   = 1.55184557391535967427"      ; Wolfram Alpha
  Print Str$("MB10 Math.pow(3.0, 0.4) = %Jf\n", ExpXY(3, FP10(0.4)))
  Print Str$("MB8  Math.pow(3.0, 0.4) = %Jf\n", ExpXY(3, FP8(0.4)))
  invoke crt_pow, FP8(3.0), FP8(0.4)      ; CRT uses double aka REAL8
  Print Str$("CRT8 Math.pow(3.0, 0.4) = %Jf\n", ST(0))
  Print Str$("MB4  Math.pow(3.0, 0.4) = %Jf\n", ExpXY(3, 0.4))      ; default is REAL4
  Exit
end start

Exact (Wolfram Alpha)   = 1.55184557391535967427
MB10 Math.pow(3.0, 0.4) = 1.551845573915359674
MB8  Math.pow(3.0, 0.4) = 1.551845573915359712
CRT8 Math.pow(3.0, 0.4) = 1.551845573915359712
MB4  Math.pow(3.0, 0.4) = 1.551845584077216225

Siekmanski

Thanks guys,

I had an older msvcrt.inc file without the pow function ( replaced it with the latest one )
Thanks dave, for the logarithms example
Creative coders use backward thinking techniques as a strategy.

FORTRANS

Hi,

   Not sure if this is useful for your purposes.  But it should show
about the level of effort that might be required.  This is not in its
original format as it was modified to work in a (somewhat) newer
assembler.

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;         Figure 7.24 Calculate 10**ST,
; "Assembly Language Programming for the IBM Personal Computer",
;  David J. Bradley, 1984, page 218.
;----------------------------------------------;
; This routine takes the top element of        ;
; the 8087 stack, and raises ten to that power ;
; Input -- ST0 is X                            ;
; Output -- ST0 is 10**X                       ;
; This routine uses two stack positions plus   ;
;   the parameter, a total of three.           ;
;-----------------------------------------------
TEN_TO_X        PROC    NEAR
                                ;-----ST0-------;-----ST1-------;---ST2-------
                                ;     X         ;      ?        ;    ?
        FLDL2T                  ;  LOG2(10)     ;      X        ;    ?
        FMUL                    ;X*LOG2(10) = E ;      ?        ;    ?
        FNSTCW  [OLD_CW]        ;---------------;---------------;-------------
        FWAIT                   ; Get the current status word
        MOV     AX,[OLD_CW]     ; Save it
        AND     AX, NOT 0C00H   ; Set rounding control to
        OR      AX, 0400H       ;  round towards -infinity
        MOV     [NEW_CW],AX
        FLDCW   [NEW_CW]        ;---------------;---------------;-------------
        FLD1                    ;       1       ;       E       ;    ?
        FCHS                    ;      -1       ;       E       ;    ?
        FLD     ST(1)           ;       E       ;      -1       ;    E
        FRNDINT                 ; INT(E) = I    ;      -1       ;    E
        FLDCW   [OLD_CW]
        FXCH    ST(2)           ;       E       ;      -1       ;    I
        FSUB    ST,ST(2)        ; E - I = F     ;      -1       ;    I
        FSCALE                  ; F*2**-1 = F/2 ;      -1       ;    I
        F2XM1                   ; (2**F/2)-1    ;      -1       ;    I
        FSUBR                   ;  2**F/2       ;       I       ;    ?
        FMUL    ST,ST           ;  2**F         ;       I       ;    ?
        FSCALE                  ; (2**F)*(2**I) ;       I       ;    ?
        FXCH    ST(1)           ;       I       ;     2**(I+F)  ;    ?
        FCOMP                   ;   2**(I+F)    ;       ?       ;    ?
        RET                     ;  10**X        ;       ?       ;    ?
TEN_TO_X        ENDP
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


HTH,

Steve N.

Siekmanski

Thanks Steve,

And thank you Dave, you put me in the right direction.  :t

float1 real4 0.0

    fld FLT4(0.4)
    fld FLT4(3.0)
    fyl2x
    fld1
    fld st(1)
    fprem
    f2xm1
    faddp
    fscale
    fxch
    fstp st(0)
    fstp float1 ; result: 1.551845584077


Marinus
Creative coders use backward thinking techniques as a strategy.

dedndave

for all the stuff you've shown us, we are still in your debt, Marinus   :biggrin:

dedndave

back in the 80's, i played with the Taylor Series method (16-bit, of course)
as i recall, i ended up with a 5-term polynomial that was fairly accurate for a single precision result

Siekmanski

Yeah the 80's, no internet and trying to get things done from reading (school)books and visiting the book library.
Still have my old trigonometry books from highschool and those helped me a lot thru the years.
Those were the days because everything was new and exciting..... 
Creative coders use backward thinking techniques as a strategy.

dedndave

not just that - my brain isn't as sharp - lol
30 years ago, i was young and i still thought math was fun
now, it's a bit of a chore

Siekmanski

I know what you mean, i'm also not that sharp anymore.
My short time memory ( hope this is english  :biggrin:) is very bad after my motorcycle accident 2 years ago where i knocked my head to a lantern post.
So programming is a good exercise for me, although i'm slow these days.
Creative coders use backward thinking techniques as a strategy.