Page 1 of 5 123 ... LastLast
Results 1 to 10 of 44

Thread: Dynamic assembly machine code in thinBasic

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Dynamic assembly machine code in thinBasic

    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

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dynamic assembly machine code in thinBasic

    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

  3. #3

    Re: Dynamic assembly machine code in thinBasic

    You guys are nuts. But in a positive way. I think thinBasic will be the first interpreter who support assembly. ;D

  4. #4
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: Dynamic assembly machine code in thinBasic

    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

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Dynamic assembly machine code in thinBasic

    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

  6. #6

    Re: Dynamic assembly machine code in thinBasic

    Well, I can see that by an assembler generated object code could be loaded into a string and then be called.

  7. #7
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dynamic assembly machine code in thinBasic

    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

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: Dynamic assembly machine code in thinBasic

    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

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Dynamic assembly machine code in thinBasic

    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

  10. #10

    Re: Dynamic assembly machine code in thinBasic

    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?

Page 1 of 5 123 ... LastLast

Similar Threads

  1. DEP and machine code
    By Petr Schreiber in forum Machine Code
    Replies: 4
    Last Post: 13-03-2008, 22:13

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •