What a hack ;D
Very nice, and pretty hardcore
Maybe at least header file could be done, where $PUSH, $POP ... could be defined.
O maybe BEGIN ASM stringName / END ASM blocks.
Wow, this is wild
Petr
For those loving ASM and its power, next thinBasic preview will have a modified version of CALL statement able to call dynamic assembly generated at runtime.
A big thanks to Charles Pegge from José Roca Forum
An example below. Code seems very long because it is commented but in reality it is very simple in its principle:
:
- identify the external function
- push parameter into the stack (attention to the REVERSE ORDER and to BYREF/BYVAL needs)
- call function
- get back value (only LONG or DWORD accepted)
[code=thinbasic]
'---------------------------------------------------------------------------
' Dynamic assembler using thinBasic CALL DWORD ... [TO ...] statement
'---------------------------------------------------------------------------
'---Reference:
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
DECLARE FUNCTION LoadLibrary LIB "KERNEL32.DLL" _
ALIAS "LoadLibraryA" _
(lpLibFileName AS ASCIIZ) AS LONG
DECLARE FUNCTION GetProcAddress LIB "KERNEL32.DLL" _
ALIAS "GetProcAddress" _
(BYVAL hModule AS DWORD, lpProcName AS ASCIIZ) AS LONG
dim hLib , _ '---Used to store external Lib handle
hFun , _ '---Used to store a pointer to function
psASM as long '---Used for passing a pointer to dynamic assembly
dim sASM , _ '---Dynamic assembly string
sMessage , _ '---MessageBox message
sTitle AS string '---MessageBox title
dim RetVal as long
'---Load library and get handle
hLib = LoadLibrary("User32.dll")
'---If handle was ok
if hLib then
'---Get function pointer
hFun = GetProcAddress(hLib, "MessageBoxA")
if hFun then
sTitle = "Dynamic Assembly Demo"
sMessage = "Hello World!"
'----------------------------------------------
' Compose ASSEMBLE MACHINE CODE STRING
'----------------------------------------------
sASM = _
CHR$(&h6 + MKL$(0) + _ ' 01 push 0 for style
CHR$(&h6 + MKL$(strPTR(sTitle)) + _ ' 06 push title address
CHR$(&h6 + MKL$(strPTR(sMessage)) + _ ' 11 push message address
CHR$(&h6 + MKL$(0) + _ ' 16 push 0 for hWnd handle
CHR$(&hFF) + CHR$(&h15) + MKL$(VARPTR(hFun)) + _ ' 21 call messagebox
CHR$(&HC3) ' 27 return
'----------------------------------------------
'---Get the address of the ASM string
psASM = strPTR(sASM) ' address of code
'---Fire the direct call using dynamic asm
CALL DWORD psASM to RetVal ' make the call and get the return value
'---Show return value
msgbox 0, RetVal
end if
end if
[/code]
We are still tuning it for example finding a way to avoid the CHR$ usage in the creation of the ASM string.
But so far it is working fine.
Ciao
Eros
PS:
Attached a thinCore.dll with new CALL DWORD ... statement implemented.
Any feedback appreciated.
Update:
attached file removed because new version updated
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
What a hack ;D
Very nice, and pretty hardcore
Maybe at least header file could be done, where $PUSH, $POP ... could be defined.
O maybe BEGIN ASM stringName / END ASM blocks.
Wow, this is wild
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
You guys are nuts. But in a positive way. I think thinBasic will be the first interpreter who support assembly. ;D
This is insane stuff. Way beyond me at the moment, but I can't believe you guys are able to get this working. Amazing thanks for showing us some really cool things.
Eros can you guys do a comparison loop to show how much faster ASM will be compared to regular fast thinBasic loop?
Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server
Kent,
this is not real ASM but the processor codes pushed/called directly as hex data.
We will not get any more speed because the interpreted time taken to make the string is huge compared to the direct function calling. Maybe we will get some ... tricks for some particular situation but nothing else as far as I can imagine now.
Also considering I'm not an ASM guru. I can just understand the basis, I've neved done anything with ASM.
Charles gave me some other ideas http://www.jose.it-berater.org/smffo...sg5529#msg5529
We will see
Ciao
Eros
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Well, I can see that by an assembler generated object code could be loaded into a string and then be called.
Eros,
if I understand it correctly, is this approach identical to using inline asm in PB, just with some minor possible slowdowns caused by string building and CALL DWORDing? So no extra preprocessing done inside thinBASIC, no emulation?
Bye,
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
Yes, Petr. There is no extra processing in thinBasic other than Power Basic does not support CALL DWORD ... TO ... but just CALL DWORD. To get back a value in Power Basic you need CALL DWORD USING ... clause.
Regardin inline ASM, thinBasic is not able to replicate it so far due to the fact that while interpreting it destroy current stack status. I do not think I will be able to replicate something like inline ASM. But it is something I really would like to study. I was always far from ASM but recently I feel very attracted by it.
New CALL DWORD ... TO ... statement is very similar to current external function interface thinBasic has. The big difference is that enternal function interface is driven by the DECLARE statement so, when needed, thinBasic has all the info to call the external function. With CALL DWORD you can difine all the needed data at script runtime on the fly.
Ciao
Eros
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Hi Eros,
thanks for the reply! Now I get it
I think it should be possible, as Charles Pegge did some machine code magic in its mini R$ RPN interpreter I think.
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
Well yes I bear much responsibility for this madness. ;D In fact opcodes are quite easy to remember and using them is nothing compared to the challenges of programming in the Windows SDK and its innumerable libraries.
But I have been toying with the idea of developing a full dynamic assembler using the regular Intel syntax. I think it would be quite easy to provide it in modular/DLL form.
Will ThinBasic support multiline string literals? This would be the ideal form in which to feed assembler source code to such a module. - I have been studying your module interface for FreeBasic, which I think would make a versatile host language for the dynamic assembler.
Your thoughts?
Bookmarks