Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 44

Thread: Dynamic assembly machine code in thinBasic

  1. #11
    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

    Ciao Charles and welcome here.

    Well, FreeBasic SDK interface for thinBasic is very rudimental compared with the one for PowerBasic but I can bring it to a similar lever.

    What do you mean by "multiline string literals"?
    I suppose you are referring to the syntax you suggested here: http://www.jose.it-berater.org/smffo...sg5529#msg5529

    [code=asm]sasm={
    68 NL0
    68 #sTitle
    68 #sMessage
    68 NL0
    FF 15 #hFun
    C3
    }[/code]

    thinBasic already manage multiline strings. For example the following is a valid thinBasic statement:
    [code=thinbasic]
    dim MyString as string value "this is a multiline
    string up to ...
    a lot of bytes"

    msgbox 0, MyString
    [/code]

    If you give me some ideas I can think to a way to parse it.

    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

  2. #12

    Re: Dynamic assembly machine code in thinBasic

    Thanks Eros, I am delighted to be here!

    That example is almost perfect. The only thing you can't do is embed inner quotes within the string.

    For example " mov al,'a' ". Hence my use of curly braces to signify an envelope of data which could incorporate quote marks

    [code=thinbasic]dim MyString as string {
    this is a multiline
    _string up to ...
    a lot of bytes
    including quote marks
    ' ' " "
    and further nested { .. } curly braces
    }

    msgbox 0, MyString

    [/code]

    As you can see from languages that adopt C syntax, the versatile braces encompass a multitude of sins. I think you will find them useful for ThinBasic in a whole variety of ways of expressing data and language extensions.

    Hwyl!
    Charles

  3. #13

    Re: Dynamic assembly machine code in thinBasic


    Here is a parsing function and test code written in PB. It parses string s from b and returns the boundary index e. It enables the structure to be captured in its entirety.


    [code=thinbasic]
    ' PB v8.04
    #COMPILE EXE
    #DIM ALL

    SUB pcb(s AS STRING, b AS LONG, e AS LONG)
    LOCAL k,n AS LONG
    LOCAL p AS BYTE PTR
    p=STRPTR(s)
    e=p+LEN(s)
    p=p+b-1
    n=0 ' nesting level
    DO
    IF p>=e THEN p=e: EXIT DO
    k=@p ' ascii
    IF k=123 THEN INCR n ' {
    IF k=125 THEN DECR n: IF n<=0 THEN INCR p:EXIT DO ' }
    IF (k=34)OR(k=39) THEN ' skip quotes
    DO
    INCR p
    IF p>=e THEN EXIT DO
    IF @p=k THEN EXIT DO
    LOOP
    END IF
    INCR p
    LOOP
    e=p-STRPTR(s)+1
    END SUB

    FUNCTION PBMAIN () AS LONG
    LOCAL s AS STRING
    LOCAL b,e AS LONG
    ' test string: { {{}} "{" '{' } !!
    s=" { {{ }} "+CHR$(34)+"{"+CHR$(34)+" "+CHR$(39)+"{"+CHR$(39)+" } !!"
    b=1
    e=LEN(s)
    pcb s,b,e
    MSGBOX MID$(s,b,e-b)
    END FUNCTION
    [/code]

  4. #14
    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

    Charles,

    thanks for interesting ideas!
    Only problem I would see with curly braces is that it is next token to parse in special way and that it is not much BASIC looking. But the functionality is perfect, thats is for sure.

    Now the string you show using {} can be written too, as:
    [code=thinbasic]
    dim MyString as string = "
    this is a multiline
    string up to ...
    a lot of bytes
    including quote marks
    "+CHR$(39)+" "+CHR$(39)+" "+$DQ+" "+$DQ+"
    and further nested { .. } curly braces
    "
    msgbox 0, MyString
    [/code]


    We will see what Eros thinks about it ,
    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

  5. #15
    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

    Thanks a lot for those ideas an even for code.
    I think here it is not a parsing problem, I have all the intrument in thinCore to parse whatever I can imagine.

    I think the problem here is the sintax we want to apply to have the assembly string done. I'm sorry not to be an ASM person so I do not have all the knowledge to understand what it the right road. But I can implement the parsing if someone give me the rule.

    For example the following code is for me a rule:
    [code=asm]sasm= ASM
    68 NL0
    68 #sTitle
    68 #sMessage
    68 NL0
    FF 15 #hFun
    C3
    END ASM[/code]

    So ASM/END ASM give the start/end of the block. Than what's inside the block?
    Every new line is an instruction. Instructions starts with an hex number (32bit code) followed by ... [here I lose my knowledge]. What do I have to expect?

    If # sign if found, it will assume a variable just after. In this case it will return 4 bytes pointer to that variable
    And so on.

    Even if in priciple I like { ... } scoping blocks I would ike to avoid them because not in the philosophy of BASIC languages. But this is not a decision, just thinking anc confronting ideas.

    I hope to have expressed my doubts.

    As reference, FBSL script engine has CallAbsolute function that is something very similar to what we are talking about. See http://gedd123.free.fr/Fbsl/Help/files/callabsolute.xml

    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. #16

    Re: Dynamic assembly machine code in thinBasic

    Hi Petr and Eros,
    Yes curly braces are alien to traditional Basic, but we see them in FreeBasic for assigning data to arrays. For UDT element data it uses round brackets. And arrays of UDT data would look like this:

    dim as MyUDT a(1 to 4) => { (..),(..),(..),(..) }


    But the curly braces are only notional. Your scheme will do the same and also patch in the variable pointers.

    I would call this hexadecimal stuff machine code. Assembler, by comparison is a high level language


  7. #17
    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

    OK, I think I've got something.

    I've created a new function called MC_Eval$ (stands for Machine Code Eval). An example usage:
    [code=thinbasic]
    sASM = MC_Eval$ "
    68 NL0
    68 #sTitle
    68 #sMessage
    68 NL0
    FF 15 #hFun
    C3
    "
    [/code]
    MC_Eval$ accept a string expression as input.
    String expression is evaluated and parsed using some of the Charle suggestions done in the following post. For the moment just the following rules are parsed but I will add more:
    • hex bytes: FF 52 5A ... or ... &Hff &H68 &H15 ...
    • Decimal longs: NL followed by a decimal number. Example: NL1234
    • Automatic variable addresses can be given using # prefix followed by the variable name.
      The following variable types are handled: any numeric type, dynamic strings (STRPTR will be used), fixed strings (VARPTR is used), GUID, VARIANT, UDT (just main variable name and not UDT elements with dotted notation).
      Variables will be taken from the current scoping level (function level). If not found it will be taken from the global scope level
      Another limit (for the moment) is that if variable is an array, you cannot indicate element position but just the variable name. A pointer to the first element will be generated.


    Here an example using the previous posting code example:
    [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 FreeLibrary LIB "KERNEL32.DLL" _
    ALIAS "FreeLibrary" _
    (BYVAL hLibModule AS DWORD) 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 = MC_Eval$ "
    68 NL0
    68 #sTitle
    68 #sMessage
    68 NL0
    FF 15 #hFun
    C3
    "
    '----------------------------------------------
    '---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

    FreeLibrary(hLib)

    end if
    [/code]



    And below the attached new thinCore.dll (substitute the one you have in \thinBasic\ directory)
    ATTENTION: not much error proof so save your work before using because it can hang your OS if wrong data is passed.


    UPDATEs:
    • 2008.03.06
      Added MC_Exec(MachineCodeString) function. MC_Exec substitute CALL DWORD ... statement avoiding to have to calculate pointer to the machine code string.
    • In case of MC_Eval found some parsing error, no runtime errors are generated but &HC3 string will be returned
    • 2008.03.10
      Attached file removed because included in a sticky post in this forum



    Let me know if all ok.
    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

  8. #18

    Re: Dynamic assembly machine code in thinBasic

    Yes that works fine Eros. You might like to try this:

    [code=thinbasic]
    '----------------------------------------------
    ' Compose ASSEMBLE MACHINE CODE STRING
    '----------------------------------------------
    sASM = MC_Eval$ "
    b8 #sMessage ' load address into eax then patch the string
    c7 40 06 45 72 6f 73 ' Eros
    c6 40 0a 21 ' !
    c6 40 0b 20 ' space

    68 NL0 '
    68 #sTitle '
    68 #sMessage '
    68 NL0 '
    FF 15 #hFun '
    C3
    "
    '----------------------------------------------
    [/code]

    With regard to error trapping, say an undefined variable, I suggest replacing the string content with C3. to prevent GPFs.

  9. #19
    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

    I hate the position in which I am because I love this new stuff but I cannot understand it as I would like :-[
    I definitely need to get into processors and machine code.

    Thanks a lot for the new example and the suggestion about runtime errors. I'm still deciding what to do in case I find some error in parsing string: generate a script runtime error or just return C3 code ang go on. I suppose a runtime error is better because there can be important steps inside the machine code string. Will see.

    I will preperare more examples other than simple MessageBox execution.

    Ciao and thanks again.
    Eros

    PS: do you know a good place where to find info about machine codes and their usage?
    A "... for dummies" introduction
    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

  10. #20

    Re: Dynamic assembly machine code in thinBasic

    This is the ultimate and essential reference manual for x86 32 bit coding. It is not for dummies but it is superbly written, has excellent tables and goes into the finest iota of detail should you need it.

    http://www.intel.com/design/intarch/manuals/243191.htm

    Zipped below a rough extract from Appendix B which I use for quick reference.

    You will find that you only use a very small subset of these.

    Presumably ThinBasic already has a large number of SDK addresses for its own use. - are these accessible?
    Attached Files Attached Files

Page 2 of 5 FirstFirst 1234 ... 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
  •