flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > useful macroinstruction

Author
Thread Post new topic Reply to topic
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
Hello:
finally I was able to get this to work(macros are not really my thing).
it's an overload of the invoke macro , that allow to pass quoted strings as parameters to the normal invoke.
but these strings won't be put among the code,instead they will be appended to a list and with the use of other macro (that I choose to name: IN_DATA) this list will be dump to wherever you use this IN_DATA macro.
in addition the macro will guarantee that the strings that are exactly the same won't be defined twice:

Code:

macro IN_DATA [string,label]
{
        label db string,0
}
macro IN_DATA
{
        match params,GlobalList \{ IN_DATA params\}
}

        GlobalList equ
        LocalList equ
macro appendGlobal item
{
        match any,GlobalList\{GlobalList equ GlobalList,item\}
        match    ,GlobalList\{GlobalList equ item\}
}

macro appendLocal item2
{
        match any,LocalList\{LocalList equ LocalList,item2\}
        match    ,LocalList\{LocalList equ item2\}
}
macro CreateLabel
{
        local A
        appendGlobal A
        appendLocal  A
}
macro search element,[texto,label]
{
    common
        searchflag equ 'no'
    forward
        match element,texto\{
                                appendLocal label
                                searchflag equ 'yes'
                           \}

    common
        match 'no',searchflag \{
                        appendGlobal element
                        CreateLabel
                        \}
}
macro invoke [args]
{
        invokeflag equ 'no'
        match `args,args \{
                        match params,GlobalList \\{search args,params\\}
                        match ,GlobalList\\{search args\\}
                        invokeflag equ 'yes'
                        \}

        match 'no',invokeflag \{appendLocal args\}

     common
        match params,LocalList\{ invoke params\}
        LocalList equ
}

    


if you copy and save this code to a file (overinvoke.inc) and include it in a program like:

Code:

format pe gui
include 'win32ax.inc'
include 'overinvoke.inc'

section 'code' code executable

start:
        invoke MessageBox,0,'Hello there1','first window',MB_OK
        invoke MessageBox,0,'Hello there2','first window',MB_OK
        invoke MessageBox,0,'Hello there3','first window',MB_OK
        invoke MessageBox,0,'Hello there4','first window',MB_OK
        invoke MessageBox,0,'first window',second,MB_OK
        invoke ExitProcess

section 'data' data readable writeable
IN_DATA
second db 'second window',0

.end  start

    


you will find that the string 'first window' that is used in the five invokes is only defined in the data section once.and you can see that you still can use the offset of a manually defined string like in the final MessageBox.

LIMITATIONS:
you must include the file(overinvoke.inc) after the normal invoke gets defined.

you must put the IN_DATA macro after the las invoke that uses quoted strings or you will get an 'error undefined symbol'

you must not declare or use the symbol names that come in the 'overinvoke' code or they will be overloaded.
(if these enter in conflict with your macros... well change their names)

you don't have access to the strings offsets,since they are created as local labels but if you want to use the same string inside an invoke, just type it exactly.

if you use this overload is no longer possible to put strings among the code since quoted strings will never be passed to the standar invoke.

you don't have control over the order in which the strings are defined,the order is as they appear in code: from left to right and top to bottom.

I haven't tested how long the list can be.

I suppose the code can be modifyed to add a parameter to the IN_DATA macro to define a ending character ,so we can overload a DOS message macro and define '$' terminated strings

if you find any error please let me know.
Post 20 Mar 2007, 06:54
View user's profile Send private message Reply with quote
dead_body



Joined: 21 Sep 2005
Posts: 187
Location: Ukraine,Kharkov
dead_body
what about the advantages of this macro before the "libcall" macro...
Post 20 Mar 2007, 10:23
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
dead_body: one advantage is
Quote:
you will find that the string 'first window' that is used in the five invokes is only defined in the data section once


I decided against doing that in libcall macro because compilation then takes quite a longer and eats lot more memory.

daluca: similar macro was existing on board few years ago
Post 20 Mar 2007, 11:30
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
dead_body



Joined: 21 Sep 2005
Posts: 187
Location: Ukraine,Kharkov
dead_body
Quote:

I decided against doing that in libcall macro because compilation then takes quite a longer and eats lot more memory.

but the source code will be more beautifull and optimized...
Post 20 Mar 2007, 19:39
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
what source code? This has nothing to do with source code... It's just whether you just declare string, or try to find same one already declared and use that Question
Post 20 Mar 2007, 21:39
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
dead_body



Joined: 21 Sep 2005
Posts: 187
Location: Ukraine,Kharkov
dead_body
sorry, not source code, i mean the compiled exe).
Post 20 Mar 2007, 22:31
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
dead_body: yes, it will, but you might need few extra megabytes of RAM to compile. Anyway, i could make some such macro. I will try to add it (sometimes), and see how big slowdown / RAM eater it is. And still i can make optional optimization module or define, which will be used only in "final" release.
Post 20 Mar 2007, 22:45
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
well I made some tests using the traditional way (define strings in data and pass offsets to invoke),the data_among_code way(putting the strings inside the invoke but without overload),and the overload way

for 1000 messageboxes with no repited strings
the results for the traditional and data_among_code were almost the same but the data_among_code exe was a little biger than the traditional(for the inserted jumps i guess)

for the overload way I couldn't get the 1000 messageboxes; for a 'out of memory error' I only get to 500 ( approximately) but I think is not really a matter of how much memory You have since I get the 500 in a computer with
1 GB of RAM and in another with only 240 MB the diference was the time it took:
1 GB =4.9 sec
240 MB = 770.1 sec (and a lot of hard disk activity)

well... the diference was also the microprocesor: 1400 MHz vs 667 MHz

I think these results limit the usage of the overload to small projects.

if someone want to test this I have made 3 files that when compiled display text that can be saved to a file and compiled again to produce the exe:

traditional way:
Code:
macro disp number
{
        a=number
if a=0
        display '0'
else

        sign=(number) and 0x8000000000000000
        if sign<>0
           display '-'
           a = -a
        end if

        first0=0
        base= 1000000000000000000
             ;9223372036854775808
             repeat 19
                    digit=a/base
                    if digit=0
                       if first0=1
                          display '0'
                       end if
                    else
                       digit=digit+'0'
                       display digit
                       first0=1
                    end if

                    a=a mod base
                    base=base/10
              end repeat
end if
}

display 'format pe gui',13,10
display 'include ',0x27,'win32ax.inc',0x27,13,10
display 13,10,13,10,13,10
display 'section ',0x27,'code',0x27, ' code executable',13,10
display 'start:',13,10,13,10,13,10

repeat 1000
display '       invoke MessageBox,0,'
display 'label_'
disp %
display 0x2C,'LABEL_'
disp %
display 0x2C,'MB_OK',13,10
end repeat
display 13,10
display '       invoke ExitProcess,0',13,10
display 13,10,13,10,13,10
display 'section ',0x27,'data',0x27,' data readable writeable',13,10

repeat 1000
display 'label_'
disp %
display ' db ',0x27,'Large Message to be displayed number: '
disp %
display 0x27,0x2C,'0',13,10
display 'LABEL_'
disp %
display ' db ',0x27,'Large title of window  number: '
disp %
display 0x27,0x2C,'0',13,10
end repeat
display 13,10,13,10,13,10,'.end start'

    


data_among_code way:
Code:

macro disp number
{
        a=number
if a=0
        display '0'
else

        sign=(number) and 0x8000000000000000
        if sign<>0
           display '-'
           a = -a
        end if

        first0=0
        base= 1000000000000000000
             ;9223372036854775808
             repeat 19
                    digit=a/base
                    if digit=0
                       if first0=1
                          display '0'
                       end if
                    else
                       digit=digit+'0'
                       display digit
                       first0=1
                    end if

                    a=a mod base
                    base=base/10
              end repeat
end if
}




display 'format pe gui',13,10
display 'include ',0x27,'win32ax.inc',0x27,13,10
display 13,10,13,10,13,10
display 'section ',0x27,'code',0x27, ' code executable',13,10
display 'start:',13,10,13,10,13,10

repeat 1000
display '       invoke MessageBox,0,'
display 0x27,'Large Message to be displayed number: '
disp %
display 0x27,0x2C,0x27,'Large title of window  number: '
disp %
display 0x27,0x2C,'MB_OK',13,10
end repeat
display 13,10,'         invoke ExitProcess,0',13,10
display 13,10,13,10,13,10
display 'section ',0x27,'data',0x27,' data readable writeable',13,10

display 'db 0',13,10,13,10,13,10
display '.end start'

    


overload way:

Code:

macro disp number
{
        a=number
if a=0
        display '0'
else

        sign=(number) and 0x8000000000000000
        if sign<>0
           display '-'
           a = -a
        end if

        first0=0
        base= 1000000000000000000
             ;9223372036854775808
             repeat 19
                    digit=a/base
                    if digit=0
                       if first0=1
                          display '0'
                       end if
                    else
                       digit=digit+'0'
                       display digit
                       first0=1
                    end if

                    a=a mod base
                    base=base/10
              end repeat
end if
}




display 'format pe gui',13,10
display 'include ',0x27,'win32ax.inc',0x27,13,10
display 'include ',0x27,'overinvoke.inc',0x27,13,10
display 13,10,13,10,13,10
display 'section ',0x27,'code',0x27, ' code executable',13,10
display 'start:',13,10,13,10,13,10

repeat 500
display '       invoke MessageBox,0,'
display 0x27,'Large Message to be displayed number: '
disp %
display 0x27,0x2C,0x27,'Large title of window  number: '
disp %
display 0x27,0x2C,'MB_OK',13,10
end repeat
display 13,10,'         invoke ExitProcess,0',13,10
display 13,10,13,10,13,10
display 'section ',0x27,'data',0x27,' data readable writeable',13,10
display 'IN_DATA',13,10

display 13,10,13,10,13,10
display '.end start'

    
Post 21 Mar 2007, 08:32
View user's profile Send private message Reply with quote
hopcode



Joined: 04 Mar 2008
Posts: 563
Location: Germany
hopcode
Hallo daluca,
perhaps useful this for the case.
http://board.flatassembler.net/topic.php?t=1890
file long_myvar3.zip
check in, and let me know!
Hope this helps,
Smile
Post 29 Mar 2008, 17:51
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.