The MASM Forum

Miscellaneous => Miscellaneous Projects => Topic started by: guga on February 22, 2022, 06:07:12 PM

Title: FreeBasic Runtime Library test
Post by: guga on February 22, 2022, 06:07:12 PM
Hi Guys,

I´m trying to build a runtime library for FreeBasic so we can try to use it on a external dll. This is what i´ve done so far. Didn´t tested yet, but it seems that the functions are working. Still need to build more then 500 of those thing. OUUCH !!!!

(Edited: Updated with more then 400 export now) :greensml: :greensml:
Title: Re: FreeBasic Runtime Library test
Post by: jj2007 on February 22, 2022, 08:00:45 PM
That's an impressive number of exported functions, Guga. Where do they come from, RosAsm? What does _fb_DevFileReadLineWstr@0 do, for example? Read a line from the default file, no parameters needed?
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 22, 2022, 09:36:19 PM
Quote from: jj2007 on February 22, 2022, 08:00:45 PM
That's an impressive number of exported functions, Guga. Where do they come from, RosAsm? What does _fb_DevFileReadLineWstr@0 do, for example? Read a line from the default file, no parameters needed?

Hi JJ

Tks :)

The functions comes from RosAsm, Idapro and Ollydbg. I was trying to create an alternative algorithm for the atan2 function and remembered the LolRemez algorithm we talked about a long time ago. Then, i searched for it and found our old posts on FreeBasic forum and downloaded the Fb version. (I also downloaded the LolRemnez gcc version from github, but didn´t have time to analyse it yet.)

Then, i did this:

1 - Downloaded Fb (Binary and it´s source code)
2 - Compiled LolRemez in Fb with full debug information.
3 - Disassembled it in idapro 7 to analyze their symbols
4 - Disassembled it in RosAsm to make a compilable and working version. (Well..kind of   :greensml: :greensml: :greensml:)
5 - Uses olly to see the cases where Fb fools idapro.
6 - Exported each one of the "fb_xxxx" functions (from inside the compiled LolRemez) and assembled them with masm, taking care to check on the source code if the functions can really be converted to a Runtime Library (Some functions are not the original api, because Fb uses crappy mingwin libraries to compile the files)


About fb_DevFileReadLineWstr, for what i saw so far, it simply read the inputed string when the user opens a file with fb_FileOpenEx and fb_FileOpen, but internally converts it to Unicode and assign the string to a data used by FreeBasic. In fact, the majority of Fbasic seems to be only a huge parser (full of crap inside, btw). Some of the functions, for example "eats" the data that belongs to other ones, like a function called "hRnd_MTWIST32" that was supposed to do some sort oof randomization using a unique buffer, but, in fact it have a internal failure that causes some of the pointers to point inside a data that is being used by the app. :dazzled: :dazzled:

For what i saw so far, Fb is not that bad, but it do have bugs that needs to be fixed, specially in what concern memory allocation. I´ll continue to try to create a dll of it, just to see if the version i made for LolRemez to RosAsm can uses the Fb libraries. I would prefer that the guys who did FB have already made a sort of dll for external usage, but it seems they have 0 intention to do so.

What is a bit hard is the fact that the source code is not totally documented    :sad:

Anyway..i´ll give a try to see if i can do it. I tried yesterday to create the dll using the library file generated by Fb, but i couldn´t be able to make it work. The linker (masm linker) was accusing hundreds of unreferenced libraries etc etc. That´s why i decided to do it from scratch last night.

If you have idapro, i can send to you the files i´m working on to try to create this thing.
Title: Re: FreeBasic Runtime Library test
Post by: jj2007 on February 22, 2022, 10:33:15 PM
Quote from: guga on February 22, 2022, 09:36:19 PMIf you have idapro, i can send to you the files i´m working on to try to create this thing.

I have neither IdaPro nor the time ;-)

It sounds like an interesting project, though - keep us posted :thumbsup:
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 23, 2022, 09:49:54 AM
New version released :) Now i´m trying to work with the graphics Api
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 24, 2022, 10:23:34 AM
New version. I guess it is more complete now. 408 exported functions. It maybe missing some more, but i didn´t checked yet. I´m releasing it here so we don´t loose the focus and try to make this thing works as it should be. I´m attaching here and updating the 1st post. Later i´ll try to create a app using this dll to see what happens (And perhaps, add some more exports - I guess it may have still 100 more to be build or something - I´ll check the Fb source code later to see exactly how many functions are missing that could be converted to a Runtime Library). :mrgreen: :mrgreen:

Btw, since the source is a true mess and huge, i was forced to split it in 3 asm files. As i said previously, Fb is not that bad, but internally is a huge piece of crap due to problems with the linker they are using to build this thing. I don´t have time to analyses each function to fix them all, but if someone wants to, then it is worth-full to give a try to improve this. Many functions are simple, so porting them to masm (or RosAsm, fasm etc etc) shouldn´t be that hard at all. Just be carefull with the Table Pointers, because gcc did a complete mess when they compile this. That´s why you will see on thee code i made several functions (procedures) containing internally 2 "::" after a label because the jmp table used by this thing is looooong, masm linker simply don´t find the proper pointers on a regular jmp, so i was forced to use "::" in some of the functions (Specially the graphic one)


Title: Re: FreeBasic Runtime Library test
Post by: guga on February 25, 2022, 02:21:23 PM
A Test version more complete (many thanks to Jack for helping compile this thing)

This version was built directly in mingw32 with the good help of Jack guiding me to  build this dll, so we can test it later. It contains more then 1000 Apis (That care that some of them are exported data and not code - See the file FbRtl32.def attached here)

I´m including 2 versions.
1 - With full debug information (and some of the functions contains mangled names) - FbRtl32D.dll
2 - Another version with the debug information stripped and also with the functions without any weird char "_" "@' and friends...so names are unmangled. - FbRtl32.dll

Included also the batch file i used to create these things and the lib file we can use to reassemble with others apps, perhaps.

I strongly advice to you read FreeBasic source code in order to try using these Apis, but get ready for crashes, gdi leaks, memory allocation problems etc (That are a common problems in internal Fb functions, btw - As i said previous, the idea of FB is not bad, but it´s implementation and coding technique approach is terrible)

Tks also to the author of this article that helped me compiling this thing unmangling the names of the function - https://www.transmissionzero.co.uk/computing/advanced-mingw-dll-topics/
Title: Re: FreeBasic Runtime Library test
Post by: TimoVJL on February 25, 2022, 06:24:34 PM
In import table additional gnu dlls:
libffi-7.dll
libgcc_s_sjlj-1.dll
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 25, 2022, 11:54:56 PM
guga, you did ok with the FbRtl32D.dll where you used "-shared -static" but on FbRtl32.dll you used "-shared -s"
you need to use -static
just did some more testing on my end with the dll's I made, when using the dll's you must supply all function/sub parameters whether optional or not
at least the ones builtin FB/FBgfx, have not tested whether this is true also for your own functions/subs
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 26, 2022, 04:23:20 AM
Hi Guga,

Thanks, impressive work :thumbsup:

I tried to build a simple FreeBASIC project with your latest library. The executable reported a missing DLL :

D:\FreeBASIC>D:\PellesC\bin\podump.exe /IMPORTS FbRtl32.dll | findstr ".dll"
Dump of FbRtl32.dll
        ADVAPI32.dll
        libffi-7.dll
        libgcc_s_sjlj-1.dll
        GDI32.dll
        KERNEL32.dll
        msvcrt.dll
        USER32.dll


libgcc_s_sjlj-1.dll exists in the folder \FreeBASIC\bin\win32 but libffi-7.dll does not exist on my computer.

Source code sample.bas :

Dim As String s="This is a test."

Dim As Integer i

For i=1 To 3

   Print s

Next i


Command to build the sample :

D:\FreeBASIC>fbc32 sample.bas FbRtl32.a

Operating system : Windows 7 64-bit
FreeBASIC Version : 1.09
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 26, 2022, 05:29:20 AM
Vortex, try FbRtl32D.dll
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 26, 2022, 05:49:04 AM
Hi Jack,

Thanks. After some renaming the DLLs, the sample worked :

D:\FreeBASIC>ren FbRtl32.dll FbRtl32.dll.bak

D:\FreeBASIC>ren FbRtl32D.dll FbRtl32.dll

D:\FreeBASIC>sample.exe
This is a test.
This is a test.
This is a test.

Title: Re: FreeBasic Runtime Library test
Post by: jack on February 26, 2022, 06:15:18 AM
Hi Vortex
I found out that when using the dll any FB/FBgfx sub/function that has default/optional parameters you need to supply them otherwise you have crash, it's particularly tricky with the graphic sub/functions
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 26, 2022, 10:57:37 AM
Hi Guys...


I´m giving a test trying to port The LolRemez to see if all the functions works correctly. It´s a hell of work trying to find out what exactly are the parameters being used, but i´m succeeding to make some small tests. I did not reached the graphics part yyet, but here is some of the internal code (and mess) that FB does for a simple command line:


Proc fb_ctor__LolRemez:
    Local @ZValue, @Order, @NPoints, @NPoints2, @DnValue, @TmpVar3
    Uses ebx

    call 'FbRtl32.fb_Init' 1, D$CommandLine, 0
    call 'FbRtl32.fb_Command' 1
    call 'FbRtl32.fb_StrInit' GValue, 0-1, eax, 0-1, 0
    call 'FbRtl32.fb_StrLcase2' GValue, 0
    call 'FbRtl32.fb_StrAssign' GValue, 0-1, eax, 0-1, 0
    call 'FbRtl32.fb_Cls' 0FFFF0000
    call 'FbRtl32.fb_StrLen' GValue, 0-1
    If eax = 0
        ;call StdOut Sz_InstructionNeeded | C_call 'msvcrt.system' {B$ "cmd", 0} | call 'FbRtl32.fb_End' 0
        call 'FbRtl32.fb_PrintVoid' 0, 1
        call 'FbRtl32.fb_PrintVoid' 0, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "Poly Needs an instruction", 0}, 25
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "type poly help at the command prompt", 0}, 36
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "cmd", 0}, 3
        call 'FbRtl32.fb_Shell' eax
        call 'FbRtl32.fb_End' 0
    End_If
    call 'FbRtl32.fb_StrAllocTempDescZEx' { B$ "help", 0}, 4
    call 'FbRtl32.fb_StrLcase2' eax, 0
    call 'FbRtl32.fb_StrCompare' GValue, 0-1, eax, 0-1
    .If eax = 0
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "poly ", 022, "start x value, finish x value, function (in x)", 022, " ", 0}, 54
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "Everything must be inside quotes", 0}, 32
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "No spaces allowed, use a comma to separate flags", 0}, 48
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "operators ^ ( ) / * + - mod ", 0}, 28
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "available functions :", 0}, 21
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call LIST
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "Example", 0}, 7
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_StrAllocTempDescZEx' {B$ "poly ", 022, "-3,3,sin(x)", 022, " ", 0}, 19
        call 'FbRtl32.fb_PrintString' 0, eax, 1
        call 'FbRtl32.fb_End' 0
    .End_If



All of those printvoid, fb_StrAllocTempDescZEx, fb_PrintString and friend can simply be replaced with something like this:


[Sz_InstructionNeeded: B$ "Poly Needs an instruction
type poly help at the command prompt", 0]

call StdOut Sz_InstructionNeeded


_____________________________________________________________________________________________

Proc StdOut:
    Arguments @lpszText
    Local @hOutPut, @StringLenght, @BytesWritten
    Uses ecx, edx

    call 'kernel32.GetStdHandle' &STD_OUTPUT_HANDLE
    mov D@hOutPut eax

    call 'kernel32.lstrlenA' D@lpszText
    mov D@StringLenght eax

    lea eax D@BytesWritten
    call 'kernel32.WriteFile' D@hOutPut, D@lpszText, D@StringLenght, eax, &NULL
    mov eax D@BytesWritten

EndP



The main problem of the internal code used by FB is that it is a complete mess. The compiler tries to guess what are the variables to be used and insetad creating tem individually, it seems that they put all together on the stack. As a result we have a stack containing tons of variables that are being reused here and there all along the function. Not to mention that gcc ruins the code flow, putting the parameters of some functions ( push etc) way before the call to the function. In some cases, it tries to save the parameters on a register used lines before the main call, saves it (push), perform a call to another function and just later, use it on the proper call to the function that really uses it. On the Main procedure, for example, we have several structures created by FB that are stored on the stack :rolleyes: :rolleyes: :rolleyes: :rolleyes:

Also, on each one of these fb_StrAllocTempDescZEx , fb_PrintString and friends, it tries to allocates or deallocate memory, what is completely unnecessary specially if we are dealing with 1 single byte.


Not to mention that gcc puts hundreds of wweird code on the start of the file. But, all is needed is a simple call to commandline Api followed by the main routine to start. So, this is how actually, LolRemez starts:

Main:

    call 'KERNEL32.GetCommandLineA' | mov D$CommandLine eax
    call fb_ctor__LolRemez
    call 'KERNEL32.ExitProcess' 0



This is LolRemez running (on the part i succeeded to convert).

(https://i.ibb.co/8YRqWCb/fgdd-Untitled.jpg) (https://ibb.co/QDqL2gk)

Title: Re: FreeBasic Runtime Library test
Post by: jj2007 on February 26, 2022, 02:50:59 PM
Quote from: guga on February 26, 2022, 10:57:37 AMThe main problem of the internal code used by FB is that it is a complete mess. The compiler tries to guess what are the variables to be used and insetad creating tem individually, it seems that they put all together on the stack. As a result we have a stack containing tons of variables that are being reused here and there all along the function. Not to mention that gcc ruins the code flow, putting the parameters of some functions ( push etc) way before the call to the function. In some cases, it tries to save the parameters on a register used lines before the main call, saves it (push), perform a call to another function and just later, use it on the proper call to the function that really uses it. On the Main procedure, for example, we have several structures created by FB that are stored on the stack :rolleyes: :rolleyes: :rolleyes: :rolleyes:

Also, on each one of these fb_StrAllocTempDescZEx , fb_PrintString and friends, it tries to allocates or deallocate memory, what is completely unnecessary specially if we are dealing with 1 single byte.

That's what C/C++ compilers do, Guga :biggrin:

PowerBasic and MasmBasic are written in Assembly, that makes a big difference.
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 26, 2022, 06:03:01 PM
HI JJ

I´m aware of that. Why FB developers insists to use gcc to compile this, is something way out of my head. It could be much better developed if it was written in masm, fasm, NAsm, GoAsm or even RosAsm (Although i never had time to build a version that uses lib yet). I remember an old Basic compiler that was also very good, one called BCX Translator, that could also be used to translated basic code to C, but used LCC for that (An old and very good C compiler. It was good specially because it was really simple and the internal runtime routines well organized). But, at least BCX was able to compile itself without dependencies and the internal coding was excellent. I ported hundreds of BCX routines to RosAsm years ago on the old RosAsm forum.

The developers of FB could make it a more robust project if they simply get rid of gcc and force FB to compile itself without those dependencies and under total control of the user, or at least use a assembler to create their apps and routines. Is not a bad project, but those internal problems results on buggy and slow apps and hard to port.
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 26, 2022, 08:36:26 PM
Hi Guga,

QuoteI´m aware of that. Why FB developers insists to use gcc to compile this, is something way out of my head. It could be much better developed if it was written in masm, fasm, NAsm, GoAsm or even RosAsm

I think the choice of gcc depends on the cross-platform audience of FreeBASIC. The compiler can be used on Linux and FreeBSD too. gcc supports those operating systems including Windows.

Personally, I like the FreeBASIC project. The projects of FB forum members Paul Squires, Xusinboy Bekchanov and Vanya are impressive. There are also other members there doing nice jobs.

Another nice Basic compiler, PureBasic is moving to gcc :

Quoteà partir de la version 6.0 PureBasic utilisera gcc pour compiler (à la place de Fasm), plus d'infos sur le blog https://www.purebasic.fr/blog/

Petite correction : gcc ne va pas remplacer Fasm, les 2 seront maintenus. Cependant j'imagine que si PureBasic devait fonctionner pour un nouveau processeur, Fred ne développera pas une version ASM, dans ce cas il faudra compiler avec gcc. à suivre ...

Translation :

QuoteBeginning from version 6.0, PureBasic will use gcc to compile (instead of Fasm), more info on the blog https://www.purebasic.fr/blog/

Small correction: gcc will not replace Fasm, both will be maintained. However I imagine that if PureBasic should work for a new processor, Fred will not develop an ASM version, in this case it will have to be compiled with gcc. to be continued ...

https://purebasic.developpez.com/actu/315253/PureBasic-6-0-Alpha-1-est-disponible-sur-votre-compte/

https://www.purebasic.fr/blog/
Title: Re: FreeBasic Runtime Library test
Post by: TimoVJL on February 26, 2022, 08:45:46 PM
Perhaps a LLVM optimizer and libs are good reason for that.
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 26, 2022, 11:59:08 PM
Hi Guga and Jack,

Today, I am trying to rebuild the same simple FB example posted to the forum. Running the executable built by the FB compiler, I am receiving the error message :

QuoteThe procedure entry point of fb_End could not be located in the dynamic link library FbRtl32.dll

D:\FreeBASIC>fbc32 Sample.bas FbRtl32.a

D:\FreeBASIC>\PellesC\bin\podump.exe /IMPORTS Sample.exe | find "fb_"
         10E  fb_End
         1F4  fb_Init
         269  fb_PrintString
         2B0  fb_StrDelete
         2B4  fb_StrInit
         3F7  fb_hRtExit
         3F8  fb_hRtInit


D:\FreeBASIC>\PellesC\bin\podump.exe /EXPORTS FbRtl32.dll | find "fb_End"
            105   104  636E79A0  fb_End@4


FbRtl32.dll is the renamed copy of FbRtl32D.dll

Attached is the sample code.
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 27, 2022, 01:53:32 AM
I dont know what FB is doing when trying to compile the example.  What procedure entry point means ? Is the function fb_end it's talking about ? Do FreeBasic uses a def file too ? Maybe that´s the reason why it´s not finding the exported function.

The function uses one single parameters (code exit) to be used Internally it points to msvcrt exit. Also internally, it checks either a pointer to a variable used in another function called _set_mode is used or not. If the pointer is not being used, the functon goes directly to exit api from msvcrt (Take a look on the disassembled source code i provided earlier, maybe it can help)

In dodicat example i´m trying to port, all is needed is:

call 'FbRtl32.fb_End' 0

or

push 0
call 'FbRtl32.fb_End'

Perhaps Fb was not initialized somehow. Try using the older version of the Dll i made in masm to see what happens. If you want, i can try to send the full library from mingw to you try to compile. Maybe you got a better luck trying to compile this, getting rid of mangled names and also keeping the debug info. I tested it last night once again, i succeeded to remove gcc dependencies but one gcc dll was still being used (except on the FbRtl32D version).


Btw...here your example works ok. Try using the old dll made in masm to see what happens.
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 27, 2022, 02:07:20 AM
Quote from: Vortex on February 26, 2022, 08:36:26 PM
Personally, I like the FreeBASIC project. The projects of FB forum members Paul Squires, Xusinboy Bekchanov and Vanya are impressive. There are also other members there doing nice jobs.

I forgot..i don´t doubt the Fb users do their best to try to create great projects. My main concern with FB is the internal organization of the code, which btw the end user don´t have any control of. It´s also not fault of the developers, but the compiler they are using to build this thing.  (Well...it is only their fault for a matter of choice, not a matter of coding style, specially because they also have no control on what gcc is doing internally) Just take a look at the disassembled version of the dll and you will understand  what i mean with disorganized internal code (the previous version with the masm source)
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 27, 2022, 05:35:36 AM
Hi Guga,

The problem is clear. As you can see in my previous posting, the DLL exports a decorated version of fb_End and the executable imports the non-decoration symbol. This explains why I receive the error message.
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 27, 2022, 05:43:14 AM
Quote from: Vortex on February 27, 2022, 05:35:36 AM
Hi Guga,

The problem is clear. As you can see in my previous posting, the DLL exports a decorated version of fb_End and the executable imports the non-decoration symbol. This explains why I receive the error message.

The "D" version you mean, right ? Yes, this version contains decorated names (I didn´t succeed to remove them), while on the undecorated version, it needs gcc dlls . I don´t know yet how to fix it, except continue to disassemble all the apis the way i was doing originally. If you want i can send to you the ".a" files to you try to see if you can use gcc to fix the newer version and unmangle all apis and also avoid the dependency of gcc dlls. What i tried to do is create 2 files.
1 - Containing full debug info and the undecorated names (for debugging purposes. I tried to create it without the decorated names, but didn´t suceeded))
2 - Other with all names undecorated without the debug info.

I never used gcc before. So i don´t know their commands to fix that properly. Let me know if you want the libraries and i´ll send to you all the contents of msys directory i used to fix these.
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 27, 2022, 05:55:13 AM
Hi Guga,

Yes, I used the debug version, exactly. With thanks to Jack who sent me the new libraries, I managed to build and run my sample project. It would be interesting to use the FB Runtime library with Masm.

gcc and Clang are very popular for cross-platform programming. The problem is that if you wish to support multiple platforms, you haven't much choices for backend C\C++ compilers.
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 27, 2022, 06:00:24 AM
Hi Guga,

The 32-bit version of the FreeBASIC compiler does not depend on gcc at least on my first test. Here is a quick demonstration :

D:\FreeBASIC>ren D:\FreeBASIC\bin\win32\gcc.exe gcc.exe.bak

D:\FreeBASIC>fbc32 Sample.bas

D:\FreeBASIC>Sample.exe
This is a test.
This is a test.
This is a test.


D:\FreeBASIC>ren D:\FreeBASIC\bin\win32\as.exe as.exe.bak

D:\FreeBASIC>fbc32 Sample.bas
Sample.bas() error 91: Executable not found: "D:\FreeBASIC\bin\win32\as.exe"


Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 06:07:39 AM
Quote from: guga on February 27, 2022, 05:43:14 AM

The "D" version you mean, right ? Yes, this version contains decorated names (I didn´t succeed to remove them), while on the undecorated version, it needs gcc dlls . I don´t know yet how to fix it
I commented on a previous post that you need to use -static not -s
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 27, 2022, 06:22:58 AM
This time, testing the 64-bit compiler :

D:\FreeBASIC>ren D:\FreeBASIC\bin\win64\gcc.exe gcc.exe.bak

D:\FreeBASIC>fbc64 Sample.bas
Sample.bas() error 91: Executable not found: "D:\FreeBASIC\bin\win64\gcc.exe"

Using the -R switch :

fbc32 -R Sample.bas

The .asm file is preserved

fbc64 -R Sample.bas

The .c file is preserved for further processing by gcc.
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 27, 2022, 06:30:47 AM
Quote from: jack on February 27, 2022, 06:07:39 AM
Quote from: guga on February 27, 2022, 05:43:14 AM

The "D" version you mean, right ? Yes, this version contains decorated names (I didn´t succeed to remove them), while on the undecorated version, it needs gcc dlls . I don´t know yet how to fix it
I commented on a previous post that you need to use -static not -s

Inded, you are right. I tried yesterday during night using static token, but wasn´t succeeding to remove the name mangling or dependency.

Now i gave another try and it seems to work ok, unmangling the names, without gcc dependency and also containing the debug info :)

The batch file i rewrite it as:


C:\msys64\mingw32\bin\gcc.exe -o FbRtl32D.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows
C:\msys64\mingw32\bin\gcc.exe -o FbRtl32.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows,--output-def,FbRtl32.def
C:\msys64\mingw32\bin\gcc.exe -o FbRtl32.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows,--kill-at
C:\msys64\mingw32\bin\dlltool.exe --kill-at -d FbRtl32.def -D FbRtl32.dll -l FbRtl32.a
pause


Please guys, see if it is ok now :)

The only thing i didn´t succeed yet is to remove the exported data on it. Jack, how to i remove from the exports, the DATA on the def file ? Can i simply edit the def file removing them ?

Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 06:52:31 AM
QuoteCan i simply edit the def file removing them ?
I don't see why not, there should be no problem
Title: Re: FreeBasic Runtime Library test
Post by: Vortex on February 27, 2022, 07:06:14 AM
Hi Guga,

I tested FbRtl32New.zip with FreeBASIC, it works fine on my preliminary tests. Here is the Masm32 version :

Converting FbRtl32.a to FbRtl32.lib :

lib2def FbRtl32.a

def2lib FbRtl32.def


FbRTDllTest.asm :

include     \masm32\include\masm32rt.inc

fb_StrInit      PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
fb_PrintString  PROTO :DWORD,:DWORD,:DWORD
fb_StrDelete    PROTO :DWORD

FBSTRING STRUCT

  char  dd ?
  len   dd ?
  size_ dd ?

FBSTRING ENDS

.data

MyStr       db 'This is a test.',0

MyStrLen    equ $ - MyStr

.data?

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses ebx

LOCAL s:FBSTRING

    mov     ebx,3

    invoke  fb_StrInit,ADDR s,-1,ADDR MyStr,\
            MyStrLen,0
_loop:

    invoke  fb_PrintString,0,ADDR s,1

    dec     ebx
    jnz     _loop

    invoke  fb_StrDelete,ADDR s
    ret

main ENDP

END start
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 07:15:23 AM
 @Vortex :thumbsup:
Title: Re: FreeBasic Runtime Library test
Post by: guga on February 27, 2022, 07:56:04 AM
Hi Guys

Good to know it´s working Vortex  :thumbsup: :thumbsup:

Jack, i found out why it was not working during my last test of compiling during the morning. On the 1st or 2nd line of the batch file, gcc outputed hundreds of object files starting with "duecs000xxx.o" that was causing linker problems of duplicated symbols etc. This time, what i did was simply delete those file and compile it again. :smiley: :smiley:

About the removal of DATA, it didn´t worked. I did this:

1- Created the normal dll with  "DllCreator.bat"
2 - Deleted all "ducs000xxx.o" generated by the gcc linker
3 - Edited manually the def file removing the DATA from it and saved it as "FbRtl32N.def" containing this new list of exports (renumbered):
EXPORTS
    fb_ASC@8 @1
    fb_AllocateMainFBThread @2
    fb_ArrayBoundChk@20 @3
    fb_ArrayClear@4 @4
    fb_ArrayClearObj@12 @5
    fb_ArrayDestructObj@8 @6
    fb_ArrayDestructStr@4 @7
    fb_ArrayErase@4 @8
    fb_ArrayEraseObj@12 @9
    fb_ArrayGetDesc@4 @10
    fb_ArrayLBound@8 @11
    fb_ArrayLen@4 @12
    fb_ArrayRedimEx @13
    fb_ArrayRedimObj @14
(....)


4 - Created another batch file and on the 4th line (used by dlltool.exe) i settled it to use FbRtl32N.def instead FbRtl32.def. So, the new batch file resulted in:
C:\msys64\mingw32\bin\gcc.exe -o FbRtl32D.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows
C:\msys64\mingw32\bin\gcc.exe -o FbRtl32.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows,--output-def,FbRtl32.def
C:\msys64\mingw32\bin\gcc.exe -o FbRtl32.dll *.o -shared -static -lgdi32 -lwinspool -lffi -lwinmm -Wl,--subsystem,windows,--kill-at
C:\msys64\mingw32\bin\dlltool.exe --kill-at -d FbRtl32N.def -D FbRtl32.dll -l FbRtl32.a
pause


5 - Runned the new batch file again, but the Data exports are still there. What i´m doing wrong ? There´s a special token in the def file or gcc command to we exclude specific exports to be created ?
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 08:33:15 AM
honestly I don't know but it puzzles me why editing the def didn't work, the way I see it the data is by the functions and def  file is only needed to build the import lib
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 10:59:17 AM
just in case anyone wants to try my build of the FB runtime dlls
Title: Re: FreeBasic Runtime Library test
Post by: jack on February 27, 2022, 11:02:38 AM
and here are the 64-bit versions
Title: Re: FreeBasic Runtime Library test
Post by: guga on March 01, 2022, 04:52:06 AM
Great work, Jack :thumbsup: :thumbsup: :thumbsup:

Guys, i´m suceeding to port the FB LolRemez version, and as soon i finish i´ll upload it here. One question...
Anybody knows the commands used in FB version of LolRemez ? I`m using thde default command poly "-6,8,cos(x)*sin(x)"  to test the app, but i needed more examples of usage to see if the port i did was correct.


In his version he uses some math or logical  operations such as Not, and, or xor, / * , \ etc. Someone have a example of using those ? And...are those commands on FB version the same as the ones in the original from github ?
Title: Re: FreeBasic Runtime Library test
Post by: jack on March 01, 2022, 05:44:35 AM
Hi guga
dodicat's code is not related to lolremez on GitHub
in the example you posted  "-6,8,cos(x)*sin(x)" the first 2 numbers are the range that the function will be approximated, in his program, pressing the return key will increase the degree of the polynomial and approximate the function again to the higher degree, pressing the escape key exits the program
you can try any number of functions and operators for your function, for example "0,1,exp(x)" will approximate the exp function in the range 0 to 1
Title: Re: FreeBasic Runtime Library test
Post by: guga on March 01, 2022, 06:51:27 AM
I Don´t get it. So what is the correct (Most accurate) app to use ? Dodicat´s, LolRemez or a new version of Dodicat ?

I saw on FB forum another discussion about it and with another solution of Dodicat using minmax
https://www.freebasic.net/forum/viewtopic.php?p=280855&hilit=lolremez#p280855




Sub GaussJordan(matrix() As Double,rhs() As Double,ans() As Double)
      Dim As Long n=Ubound(matrix,1)
      Redim ans(0):Redim ans(1 To n)
      Dim As Double b(1 To n,1 To n),r(1 To n)
      For c As Long=1 To n 'take copies
            r(c)=rhs(c)
            For d As Long=1 To n
                  b(c,d)=matrix(c,d)
            Next d
      Next c
      #macro pivot(num)
      For p1 As Long  = num To n - 1
            For p2 As Long  = p1 + 1 To n 
                  If Abs(b(p1,num))<Abs(b(p2,num)) Then
                        Swap r(p1),r(p2)
                        For g As Long=1 To n
                              Swap b(p1,g),b(p2,g)
                        Next g
                  End If
            Next p2
      Next p1
      #endmacro
      For k As Long=1 To n-1
            pivot(k)              'full pivoting each run
            For row As Long =k To n-1
                  If b(row+1,k)=0 Then Exit For
                  Var f=b(k,k)/b(row+1,k)
                  r(row+1)=r(row+1)*f-r(k)
                  For g As Long=1 To n
                        b((row+1),g)=b((row+1),g)*f-b(k,g)
                  Next g
            Next row
      Next k
      'back substitute
      For z As Long=n To 1 Step -1
            ans(z)=r(z)/b(z,z)
            For j As Long = n To z+1 Step -1
                  ans(z)=ans(z)-(b(z,j)*ans(j)/b(z,z))
            Next j
      Next    z
End Sub

'Interpolate through point via a polynomial (spline)
Sub Interpolate(x_values() As Double,y_values() As Double,p() As Double)
      Var n=Ubound(x_values)
      Redim p(0):Redim p(1 To n)
      Dim As Double matrix(1 To n,1 To n),rhs(1 To n)
      For a As Long=1 To n
            rhs(a)=y_values(a)
            For b As Long=1 To n
                  matrix(a,b)=x_values(a)^(b-1)
            Next b
      Next a
      'Solve the linear equations
      GaussJordan(matrix(),rhs(),p())
End Sub

Function polyval(Coefficients() As Double,Byval x As Double) As Double
      Dim As Double acc
      For i As Long=Ubound(Coefficients) To Lbound(Coefficients) Step -1
            acc=acc*x+Coefficients(i)
      Next i
      Return acc
End Function

Sub chebypoints(min As Double,max As Double,order As Integer,c()As Double)
      Redim c(1 To order)
      Dim pi As Double=4*Atn(1)
      Dim count As Integer
      For k As Integer=order To 1 Step -1
            count=count+1
            c(count)= Cos((pi/2)*(2*k-1)/order)
            c(count)=(max-min)*(c(count)+1)/2+min
      Next k
      Print "chebychev points in range"
      For n As Long=Lbound(c) To Ubound(c)
            Print n,c(n)
      Next
      Print
End Sub

Function nest(pol() As Double) As String
      Dim As String xx,tmp,ch=""
      For a As Long=Ubound(pol) To Lbound(pol) Step -1
            Dim As String op
            If a=Lbound(pol) Then xx="" Else xx="*x"
            Dim As Double p=pol(a)
            If Sgn(p)>=0 Then op="+" Else op=""
            tmp="("+ch+op+Str(p)+")"+xx
            ch="("+ch+op+Str(p)+")"+xx
            If Len(tmp)>50 Then ch=ch+" _ " +Chr(10) '80
      Next a
      Return ch
End Function

Function func(x As Double) As Double
return (((((-0.01019205450473583)*x+0.08530805745877951)*x-0.3142876923553593)*x _
+0.9127809568362455)*x _
+0.3264033259436233)
End Function

Dim As Long n=5 '<----------------------  number of terms
Dim As Double lowerx=1 '<-----------------  LIMITS
Dim As Double upperX=2 '<------------------ LIMITS

Dim As Double x(1 To 5)
Dim As Double y(1 To 5)

Redim As Double points()
lowerx=1
upperx=2

chebypoints(lowerX,upperX,n,points())
For z As Double=1 To 5
      x(z)=points(z)
      y(z)=sqr(points(z))
Next
Redim As Double Poly(0)
interpolate(x(),y(),poly())

Print "Polynomial Coefficients:"
Print
For z As Long=1 To Ubound(Poly)
      If z=1 Then
            Print "constant term  ";Tab(20);Poly(z)
      Else
            Print Tab(8); "x^";z-1;"   ";Tab(20);Poly(z)
      End If
Next z
Print
Print "nested form:"
Print nest(poly())
Print


Print "  x           sqrt(1+x)                error"
For x As Double=0 To 1.05 Step 0.1
      Var y=func(1+x)
      Print Using "##.###";x;
      Print "  ";y, y-Sqr(1+x)
Next

Sleep


What versions are the correct ones ?
Title: Re: FreeBasic Runtime Library test
Post by: jack on March 01, 2022, 09:39:06 AM
I don't use his code so I can't help you there, but while his code works to some extent it's limited because of it's use of double precision, if you wanted to approximate your function for single precision then it's probably good enough but if you want higher precision approximation then use something else, the lolremez from GitHub is better but I wouln't use that either
rational minimax approximations are usually more accurate, I would recommend Mathematica and you get it for free on the Raspberry PI, for example here's an approximation to log(1+x) in the range 0 to 1

(x (2569.904056423691126968 +
     x (5756.381534657531664647 +
        x (4502.370849703438634267 +
           x (1456.290562569254837547 +
              x (176.9521118930255743703 +
                 5.235866454158384429507 \
x))))))/(2569.904056423691455251 +
   x (7041.333562869242358112 +
      x (7166.402945672550756556 +
         x (3334.856861651063659714 +
            x (701.9321435126277119667 +
               x (56.23432384224838579736 + x))))))

maximum relative error -1.27741479491948110288e-16
Title: Re: FreeBasic Runtime Library test
Post by: guga on March 01, 2022, 11:18:53 AM
H Guys

The Dodicat[s lolremez version is running now with the FBRTl32.dll  :thumbsup: :thumbsup: :azn: :azn: :azn:


(https://i.ibb.co/JkqXFZf/bvcbb-Untitled.jpg) (https://ibb.co/3hvtYKD)

It still have some flaws, but i[m working on it. The graphic part seems to work ok, now i need to fix a bit more to show the lines etc.

Jack...I do[t know about mathematica. I wanted to build a standalone version for us to use, So, mathematica don't have a library to use that fits our purposes. Also, it seems that it´s not free anymore
https://community.wolfram.com/groups/-/m/t/1511397


About "rational minimax approximations". Isn´t that what Dodicat did on his last post ? If not..how to implement this minimax approximations ?
Title: Re: FreeBasic Runtime Library test
Post by: jack on March 01, 2022, 11:49:13 AM
that post on the wolfram groups is 3 years old, I just installed Mathematica on my PI 4 about a week ago, I don't know how you would implement rational minimax sorry, perhaps one the math experts can you some advise
nmerical recipes has code to do rational chebyshev approximation http://phys.uri.edu/nigh/NumRec/bookfpdf/f5-13.pdf