Menu

GSL in assembler

Help
jj2007
2012-02-11
2012-07-26
  • jj2007

    jj2007 - 2012-02-11

    Hello,

    I am mostly programming in Masm on Windows, and stumbled over gsl a while ago.
    Fantastic work! So I decided to make it more accessible in assembler, and
    wrote some macros.

    Here is a demo how to use the macro with Microsoft Masm or
    JWasm, taken 1:1 from the gsl example
    program
    :

    include \masm32\MasmBasic\MasmBasic.inc ;
    download

    ; libgsl.dll download (gnuwin32.sourceforge.net) :
    open Binaries zip, and extract

    ; libgsl.dll and libgslcblas.dll to \masm32\MasmBasic\GnuScLib\DLL

    ; define the gsl function(s) you need using the syntax of the GNU Scientific
    Library Reference

    gsl double gsl_sf_bessel_J0 (double x)
    .data
    x REAL8 5.0 ; in C, this would be double x = 5.0;

    Init*
    *gsl_INIT

    gsl_sf_bessel_J0(x)

    PrintLine "GNU expected:", Tb$, "-0.1775967713143382920"

    PrintLine "On FPU: ", Tb$, "-0.1775967713143383198"

    Inkey Str$("MB Str$() \t%If", ST(0))

    gsl_EXIT
    Exit****
    end start

     
  • Allan

    Allan - 2012-02-17

    I don't understand why GSL users would want to write code in MASM or Nasm
    assembler language except maybe for industrial applications. I would say that
    for most "open source" developers Nasm is preferred over MASM; for example
    OpenSSL no longer supports MSVC/C++ building with MASM with the latest version
    1.0.0g.

     
  • jj2007

    jj2007 - 2012-02-21

    See it the other way round: Why should a Masm coder want to use the GSL
    library? Simply because occasionally you may need some math functions.
    Besides, while the code below is assembler (i.e. ML.EXE recognises it as valid
    code and assembles it...), it undeniably bears some resemblence to, well,
    BASIC ;-)

    include \masm32\MasmBasic\MasmBasic.inc ;
    download

    ; libgsl.dll download (gnuwin32.sourceforge.net) :
    open Binaries zip, and extract the two files

    ; libgsl.dll and libgslcblas.dll to \masm32\MasmBasic\GnuScLib\DLL

    ; define your required gsl function(s) using the syntax of the GNU Scientific
    Library Reference

    gsl double gsl_stats_mean (const double data, size_t stride, size_t n) ;
    stride in doubles

    gsl double gsl_stats_variance (const double data, size_t stride, size_t n)

    gsl double gsl_stats_sd (const double data, size_t stride, size_t n)

    gsl double gsl_stats_skew (const double data, size_t stride, size_t n)

    gsl double gsl_sf_bessel_J0 (double x) ; double x means a pointer to a REAL8
    is required

    ** Init
    gsl_INIT

    ** ; Kill "MyDoubles.dat" ; activate if you want to create a new file with every run
    .if !Exist("MyDoubles.dat") ; generate a new data file in
    \Masm32\MasmBasic\GnuScLib\DLL

    NumDoubles=1000000 ; one Million items - should take a while...

    Rand() ; optional: set a seed using rdtsc

    Dim MyDoubles(NumDoubles-1) As REAL8

    For_ ebx=0 To NumDoubles-1

    Rand(0, 200, MyDoubles(ebx)) ; create a double between 0 (inclusive) and 200
    (exclusive)

    Next

    ; for numeric arrays, Recall and Store need the #x syntax, i.e. Store
    "myfile.dat", MyDoubles() is not possible

    Open "O", #1, "MyDoubles.dat" ; write the NumDoubles items to file

    Store #1, MyDoubles()

    Close #1

    Erase MyDoubles() ; this array no longer needed

    PrintLine "New dataset created in ", CurDir$(0), CrLf$

    .endif

    Open "I", #1, "MyDoubles.dat"

    mov ebx, Lof(#1) ; we need the size in a permanent register

    sar ebx, 3 ; divide size by 8

    PrintLine "Analysing ", CurDir$(), Str$("MyDoubles.dat with %i entries", ebx)

    Dim MyDoubles2(ebx) As REAL8 ; read an array of doubles from file

    Recall #1, MyDoubles2()

    Close #1

    lea ecx, MyDoubles2(0) ; get pointer to start of data

    gsl_stats_mean(ecx, 1, ebx) ; stride 1 double, NumDoubles items

    Print Str$("\nstats_mean= \t %5f", ST(0)) ; show as float, 5 digits precision

    fstp st ; clean up the FPU - gsl uses many, sometimes all 8 FPU registers

    gsl_stats_variance(ecx, 1, ebx) ; stride 1 double, NumDoubles items

    Print Str$("\ns_variance= \t %5f", ST(0)) ; show as float, 5 digits precision

    fstp st

    gsl_stats_sd(ecx, 1, ebx) ; stride 1 double, NumDoubles items

    Print Str$("\nstat_stdev= \t %3f", ST(0)) ; show as float, 3 digits precision

    fstp st

    gsl_stats_skew(ecx, 1, ebx) ; stride 1 double, NumDoubles items

    Print Str$("\nstat_skew= \t %2f", ST(0)) ; show as float, 2 digits precision

    fstp st

    gsl_EXIT
    Exit
    end start

     
MongoDB Logo MongoDB