News:

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

Main Menu

MasmBasic library for FreeBasic: Rand()

Started by jj2007, July 18, 2020, 02:36:07 PM

Previous topic - Next topic

jj2007

Attached a library to make MasmBasic's Rand() available to FreeBasic. It contains MbRand.lib, plus the latest PCG32II.bas from this post by deltarho[1859], and Pcg32jj.bas for testing both random number generators

Pcg32jj.bas:
#define iterations 20000000
#Include "Windows.bi" ' needed for HeapFree
#inclib "MbRand" ' link with MbRand.lib
Extern "Windows" ' otherwise error: undefined reference to `MBRAND@8' (thanks to srvaldez)
   Declare Function MbRand(byval bytes as Long, byval seed As Long) As Long Ptr
End Extern

Dim As ULong i
Dim As Double t=Timer
Dim MbNumbers as Long ptr=MbRand(iterations, 0) ' create an array of random numbers

t=Timer-t
Print "MasmBasic Rand took";int(1000*t);" ms to generate";iterations;" random integers"
For i = 0 To 9
  Print MbNumbers[i] ' show some examples
Next
HeapFree(GetProcessHeap(), 0, MbNumbers) ' recommended: free the buffer after use

#Include "PCG32II.bas"
Dim As pcg32 pcg
Print
t=Timer
Dim PcgNumbers as Long ptr=HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, 4*iterations)
For i = 0 To iterations-1
  PcgNumbers[i]=pcg.rand
Next

t=Timer-t
Print "PCG32II took";int(1000*t);" ms"
For i = 0 To 9
  Print PcgNumbers[i] ' show some examples
Next

Sleep ' wait for any key


Typical output:
MasmBasic Rand took 79 ms to generate 20000000 random integers
1204268221
1112404322
1732203802
533173446
-885981894
1063236145
-1231518066
-1824631447
-291564557
2014909854

PCG32II took 915 ms
-1381938689
115389793
2050711904
1863274496
390953194
-2043367799
146861628
-904154128
1774280983
1388669250


On a side note, the package contains MakeMinGwHappy (source+exe), which serves to suppress ten absolutely useless warnings:
Warning: .drectve `-defaultlib:\masm32\lib\masm32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\gdi32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\user32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\kernel32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\Comctl32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\comdlg32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\shell32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\oleaut32.lib ' unrecognized
Warning: .drectve `-defaultlib:\masm32\lib\ole32.lib ' unrecognized
Warning: corrupt .drectve at end of def file


There are many people on the web who had this problem with Windows libraries in a MinGW environment, and nobody found a workaround so far, it seems. So I solved that, but it cost me a couple of hours. Every time I touch C/C++, it costs me hours of searching the web for trivial little problems :cool:

jj2007

Playing with TinyIDE - the exe has 7168 bytes, and IMHO that's the right size ;-)

Extract to the folder where your fbc.exe sits, then drag TinyGui.rtf over TinyIDE.exe and hit F6 :cool:

P.S.: VirusTotal proudly announces that 19 of 69 engines detected dangerous stuff in the exe - so I generously decided to publish the source, too; open it in RichMasm (or WordPad, or M$ Word) and hit F6 to build it. Or read the posts in our AV Software sh*t list :greenclp:

hutch--

JJ,

Open the exe in ResourceHacker and add a manifest and version control block and it may not trigger as many AV scanners. I recently tweaked my old 2001 Micrografx image app by adding a manifest into it and it modernised the appearance and runs OK.

jj2007

That works for some AV but it 1. increases file size of this demo and 2. just demonstrates how utterly crappy these AV are.

If I were a virus writer, I would add a manifest and a VCB in order not to be detected :greenclp:

The appearance btw is exactly the same.

TimoVJL

May the source be with you

jj2007

Quote from: TimoVJL on September 08, 2020, 03:40:55 PM
AVG: Win32:Malware-gen in both cases.

Sure :tongue:

The source is above, it's not even MasmBasic - pure Masm32 :cool:

Vortex

Hi Jochen,

Could you try to remove the directive section with the GNU tool objconv?

\MinGW\bin\objcopy --remove-section .drectve objectfile.o

https://sourceware.org/binutils/docs/binutils/objcopy.html

jj2007


Vortex

Hi Jochen,

Here is a quick test :

\masm32\bin\ml /c /coff Window.asm
copy Window.obj Window2.obj
C:\MinGW\bin\objcopy --remove-section .drectve Window2.obj
\masm32\bin\polink /SUBSYSTEM:WINDOWS /ENTRY:start Window2.obj
\masm32\bin\polink /SUBSYSTEM:WINDOWS /ENTRY:start /LIBPATH:\masm32\lib Window2.obj kernel32.lib user32.lib


The first attempt to link the object module Window2.obj will not work as the directive section is removed by objcopy. The correct import libraries are originally specified by the missing section.

jj2007

I get some errors...

POLINK: error: Unresolved external symbol '_GetModuleHandleA@4'.
POLINK: error: Unresolved external symbol '_GetCommandLineA@0'.
POLINK: error: Unresolved external symbol '_ExitProcess@4'.
POLINK: error: Unresolved external symbol '_LoadIconA@8'.
POLINK: error: Unresolved external symbol '_LoadCursorA@8'.
POLINK: error: Unresolved external symbol '_RegisterClassExA@4'.
POLINK: error: Unresolved external symbol '_CreateWindowExA@48'.
POLINK: error: Unresolved external symbol '_UpdateWindow@4'.
POLINK: error: Unresolved external symbol '_GetMessageA@16'.
POLINK: error: Unresolved external symbol '_TranslateMessage@4'.
POLINK: error: Unresolved external symbol '_DispatchMessageA@4'.
POLINK: error: Unresolved external symbol '_PostQuitMessage@4'.
POLINK: error: Unresolved external symbol '_DefWindowProcA@16'.
POLINK: fatal error: 13 unresolved external(s).


I have tried quite a number of options, none of them works or has an effect :sad:

With RichMasm, you can insert this line for playing around:
OPT_BatA C:\MinGW\bin\strip --strip-debug *.obj & echo trying to strip the debugging symbols

"BatA" means "do this batch line after the assembly"

Vortex

Hi Jochen,

The error messages are the result of a deliberate attempt to demonstrate that the directive section ( .drectve ) is removed from the object file.

The last line in the batch file :

\masm32\bin\polink /SUBSYSTEM:WINDOWS /ENTRY:start /LIBPATH:\masm32\lib Window2.obj kernel32.lib user32.lib

links the object module. You can examine the two object modules Window.obj and Window2.obj with Pelle's Podump. Window2.obj lacks the directive section. This is why polink "fails" in the fist attempt.

Disassembling the original object file Window.obj with Agner Fog's objconv tool :
.drectve SEGMENT BYTE PUBLIC 'CONST'                    ; section number 4

        db 2DH, 64H, 65H, 66H, 61H, 75H, 6CH, 74H       ; 0000 _ -default
        db 6CH, 69H, 62H, 3AH, 5CH, 6DH, 61H, 73H       ; 0008 _ lib:\mas
        db 6DH, 33H, 32H, 5CH, 6CH, 69H, 62H, 5CH       ; 0010 _ m32\lib\
        db 6BH, 65H, 72H, 6EH, 65H, 6CH, 33H, 32H       ; 0018 _ kernel32
        db 2EH, 6CH, 69H, 62H, 20H, 2DH, 64H, 65H       ; 0020 _ .lib -de
        db 66H, 61H, 75H, 6CH, 74H, 6CH, 69H, 62H       ; 0028 _ faultlib
        db 3AH, 5CH, 6DH, 61H, 73H, 6DH, 33H, 32H       ; 0030 _ :\masm32
        db 5CH, 6CH, 69H, 62H, 5CH, 75H, 73H, 65H       ; 0038 _ \lib\use
        db 72H, 33H, 32H, 2EH, 6CH, 69H, 62H, 20H       ; 0040 _ r32.lib

        db      248 dup (?)                             ; 0048 _
.drectve ENDS

jj2007

Partial success: I removed the .drectve sections in the obj files of the MasmBasic library, with three exceptions - when I removed these three, I got 40 errors regarding missing symbols.

Without these sections, \Masm32\MasmBasic\MasmBasic.lib shrunk from 133906 to 126332 bytes. The zipped version, however, is only 580 bytes shorter (meaning most .drectve sections are identical).

The errors concern only a few groups of API calls, Crypt*, Reg* and Gdip*:
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptAcquireContextA@20
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptCreateHash@20
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptDestroyHash@4
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptGetHashParam@20
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptHashData@16
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _CryptReleaseContext@8
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegCloseKey@4
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegEnumKeyExA@32
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegEnumValueA@32
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegOpenKeyExA@20
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegQueryValueExA@24
MasmBasic.lib(LibTmpAQ.obj) : error LNK2001: unresolved external symbol _RegSetValueExA@24
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipCreateBitmapFromHBITMAP@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipCreateBitmapFromStream@8
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipCreateFromHDC@8
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipCreateFromHDC@8
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipCreateHBITMAPFromBitmap@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipDeleteGraphics@4
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipDeleteGraphics@4
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipDisposeImage@4
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipDrawImageRectI@24
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipDrawImageRectRectI@56
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetImageEncoders@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetImageEncodersSize@8
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetImageHeight@8
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetImageWidth@8
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetPropertyItem@16
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipGetPropertyItemSize@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipImageGetFrameCount@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipImageGetFrameDimensionsList@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipImageSelectActiveFrame@12
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdipSaveImageToFile@16
MasmBasic.lib(LibTmpAS.obj) : error LNK2001: unresolved external symbol _GdiplusStartup@12
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipCreatePen1@16
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipCreateSolidFill@8
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipDeleteBrush@4
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipDeletePen@4
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipDrawLinesI@16
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipFillEllipseI@24
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipFillRectangleI@24
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipSetPenLineJoin@8
MasmBasic.lib(LibTmpAY.obj) : error LNK2001: unresolved external symbol _GdipSetSmoothingMode@8

Vortex

Hi Jochen,

Did you supply the missing import libraries in your batch file building the executable?  Reading your output list, I see that Advapi32.lib and Gdiplus.lib are missing.

Could you try something similar like this one ? :

\masm32\bin\polink /SUBSYSTEM:WINDOWS /ENTRY:start /LIBPATH:\masm32\lib File.obj kernel32.lib user32.lib advapi32.lib gdiplus.lib

jj2007

Hi Erol,

Adding the libs to the linker command line eliminates the errors but increases the size of the library from 126332 to 1012506 bytes...

Vortex

Hi Jochen,

Can you see the additional elements increasing the size of the library with Timo's TLPEView?

http://masm32.com/board/index.php?topic=7435.0