flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [F1] MULTISECTION .code .data .stream (update on first post)

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
FINAL RELEASE OF NEXT-GEN MULTISECTION ENGINE

Features:
- Each section auto-generating additional subsection-directives like .code/..code/.codeonce/..codeonce
- Use subsections unlimited times, multisection macro will gather them to main sections.
- You can switch subsections inside procedures and macros.
- Additional one-time subsections for macros you want to use multiple times. Don't worry about already defined data any more.
- Comment whole block by .comment (limited by content inside).
- Unique .frame subsections that will be gathered to exact places you mark by .stream
- Auto-gathering multiple library/imput/export and removes already defined writes.
- Single line subsections (..) ignores global labels that allow them to be used between local labels in other section.

Rules:
- Remove quotes where declaring new sections.
- Don't use .stream/.frame/..frame for library/imput/export.
- Don't declare subsections inside if operators. You need separate if combination for each subsection.

How it works:
Where you declare new section it creates short names for subsections and gather them together after all. For example:
Code:
section .code code readable executable
   ; you can put your code here as usual
   ; but also, it generates new names for subsections that can be used in other sections or macros
.code
   ... ;your extra code is here
..code ... ;single line code subsection
.codeonce <unique name>
   ... ;one more way to add extra code just once, good for macros that you wish to use multiple times 
..codeonce <unique name>, ... ;single line code subsection      
All subsections can be used in macros unlimited times. Also you can update your library/imput/export writes by use .idata subsections any time, that writes combines together.
Another useful subsections group:
Code:
.frame <stream name>
   ... ;anything you want excluding another subsections and library/imput/export
..frame <stream name>, ... ;anything again by single line
.frameonce <stream name>,<unique name>
   ... ; once for that name of stream
..frameonce <stream name>,<unique name>, ... ;anything by single line but only once for named stream

.stream <unique name> -- the point where all of exact named frames gathered together    
All single lined subsections don't change current subsection where they placed.

To integrate multisections with your project just put it on header of your mainfile or include it there. Put .multisection before first section and remove quotes from all of them.

Example of header:
Code:
match any,runonce               ;speedup trick, unnecessary for most of scenarios
{       define runonce          ;but some includes may need it

        include 'multisection.inc'
        format PE64 GUI 5.0
        include 'win64a.inc'
}

.multisection
section .rsrc resource data readable
section .bss readable writeable
section .data data readable writeable
section .code code readable executable
section .idata import data readable writeable
   ... 
   ... your project is here
    

You may use .text instesd of .code or even rename it
Code:
match any,runonce       
{       define runonce  

        .code fix .text
        ..code fix ..text
        .codeonce fix .textonce
        ..codeonce fix ..textonce

        include 'multisection.inc'
        format PE64 GUI 5.0
        include 'win64a.inc'
        section '.rsrc' resource from 'Project.res' data readable 
}

.multisection
section .data data readable writeable
section .text code readable executable
section .idata import data readable writeable
   ... 
   ... your project is here     



MACRO:
Code:
macro .multisection
{       match ,skipmultisection
        \{      end if
                match any,skipstream
                \\{     match arg,cuRRentSection
                        \\\{macro arg
                                \\\\{
                                        end if
                                        define skipstream
                                        if 1
                                \\\\}
                                macro .\\\#arg string&
                                \\\\{
                                        end if
                                        match any,skipstream
                                        \\\\\{
                                                define skipstream
                                                if 1
                                                        string
                                                .comment ;end
                                        \\\\\}
                                        match ,skipstream
                                        \\\\\{
                                                if 1
                                                        string
                                                define skipstream
                                        \\\\\}
                                \\\\}
                                macro arg\\\#once param
                                \\\\{
                                        match ,arg\\\\#once\\\\#param \\\\\{.comment\\\\\}
                                        match some,arg\\\\#once\\\\#param
                                        \\\\\{
                                                end if
                                                define skipstream
                                                define arg\\\\\#once\\\\\#param
                                                if 1
                                        \\\\\}
                                \\\\}
                                macro .\\\#arg\\\#once param,line&
                                \\\\{
                                        match ,arg\\\\#once\\\\#param \\\\\{ \\\\\}
                                        match some,arg\\\\#once\\\\#param
                                        \\\\\{
                                                define arg\\\\\#once\\\\\#param
                                                .\\\\\#arg line
                                        \\\\\}
                                \\\\}
                        \\\}
                        macro section string
                        \\\{ match arg rest,string \\\\{ arg \\\\} \\\}
                        macro library string&
                        \\\{
                                match ,skipstream
                                \\\\{
                                        match ,libraryonce \\\\\{ \\\\\}
                                        match some,libraryonce
                                        \\\\\{
                                                define libraryonce
                                                match arg,librarygathering
                                                \\\\\\{
                                                        library arg
                                                \\\\\\}
                                        \\\\\}
                                \\\\}
                        \\\}
                        macro import namelib,string&
                        \\\{
                                match ,skipstream
                                \\\\{
                                        match ,importlibonce\\\\#namelib \\\\\{ \\\\\}
                                        match some,importlibonce\\\\#namelib
                                        \\\\\{
                                                define importlibonce\\\\\#namelib
                                                match arg,importgathering\\\\\#namelib
                                                \\\\\\{
                                                        import arg
                                                \\\\\\}
                                        \\\\\}
                                \\\\}
                        \\\}
                        macro export namelib,string&
                        \\\{
                                match ,skipstream
                                \\\\{
                                        match ,exportlibonce\\\\#namelib \\\\\{ \\\\\}
                                        match some,exportlibonce\\\\#namelib
                                        \\\\\{
                                                define exportlibonce\\\\\#namelib
                                                match arg,exportgathering\\\\\#namelib
                                                \\\\\\{
                                                        export arg
                                                \\\\\\}
                                        \\\\\}
                                \\\\}
                        \\\}
                \\}
                match ,skipstream
                \\{     define skipstream 'nostream'
                        macro .stream param \\\{ \\\}
                        macro .frame param
                        \\\{
                                .comment ;end
                                match =param,streamcuRRent
                                \\\\{
                                        end if
                                        define skipstream
                                        if 1
                                \\\\}
                        \\\}
                        macro ..frame param,string& 
                        \\\{
                                match =param,streamcuRRent
                                \\\\{
                                        end if
                                        define skipstream
                                        if 1
                                                string
                                        .comment ;end
                                \\\\}
                        \\\}
                        macro .frameonce param,uniname,string&
                        \\\{
                                .comment ;end
                                match =param,streamcuRRent
                                \\\\{
                                        match ,uniname\\\\#once\\\\#param \\\\\{ \\\\\}
                                        match some,uniname\\\\#once\\\\#param
                                        \\\\\{
                                                define uniname\\\\\#once\\\\\#param
                                                end if
                                                define skipstream
                                                if 1
                                        \\\\\}
                                \\\\}
                        \\\}
                        macro ..frameonce param,uniname,line&
                        \\\{
                                match =param,streamcuRRent
                                \\\\{
                                        match ,uniname\\\\#once\\\\#param \\\\\{ \\\\\}
                                        match some,uniname\\\\#once\\\\#param
                                        \\\\\{
                                                define uniname\\\\\#once\\\\\#param
                                                ..frame param,line
                                        \\\\\}
                                \\\\}
                        \\\}
                        macro library string& \\\{ \\\}
                        macro import string& \\\{ \\\}
                        macro export string& \\\{ \\\}
                        macro proc string& \\\{.comment\\\}
                        macro endp \\\{.comment\\\}
                        macro section string \\\{.comment\\\}
                        match arg,cuRRentSection
                        \\\{
                                macro arg \\\\{.comment\\\\}
                                macro .\\\#arg string& \\\\{ \\\\}
                                macro arg\\\#once param \\\\{.comment\\\\}
                                macro .\\\#arg\\\#once param,string& \\\\{ \\\\}
                        \\\}
                \\}
                if 0
        \}
        match any,skipmultisection
        \{      define skipmultisection
                @__file__@ equ __file__
                define cuRRentSection
                macro .comment
                \\{
                        end if
                        define skipstream 'nostream'
                        if 0
                \\}
                macro section allargs
                \\{
                        end if
                        define skipstream 'nostream'
                        match arg rest,allargs
                        \\\{
                                macro arg \\\\{ .comment \\\\}
                                macro .\\\#arg string&
                                \\\\{
                                        match ,cuRRentSection
                                        \\\\\{
                                                match firstarg somerest,string 
                                                \\\\\\{
                                                        match =library,firstarg \\\\\\\{string\\\\\\\}
                                                        match =import,firstarg \\\\\\\{string\\\\\\\}
                                                        match =export,firstarg \\\\\\\{string\\\\\\\}
                                                \\\\\\}
                                        \\\\\} 
                                \\\\}
                                macro arg\\\#once param \\\\{.comment\\\\}
                                macro .\\\#arg\\\#once param,string&
                                \\\\{
                                        match ,cuRRentSection
                                        \\\\\{
                                                match firstarg somerest,string 
                                                \\\\\\{
                                                        match =library,firstarg \\\\\\\{string\\\\\\\}
                                                        match =import,firstarg \\\\\\\{string\\\\\\\}
                                                        match =export,firstarg \\\\\\\{string\\\\\\\}
                                                \\\\\\}
                                        \\\\\} 
                                \\\\}
                                macro .endmultisection
                                \\\\{
                                        .endmultisection
                                        section \\\\`arg rest
                                        cuRRentSection equ arg
                                        define skipstream 'nostream'
                                        if 0
                                        match f, @__file__@ \\\\\{ include f \\\\\}
                                        end if
                                                purge section,arg,.\\\\#arg,arg\\\\#once,\
                                                        .\\\\#arg\\\\#once,library,import,export
                                \\\\}
                        \\\}
                        if 0
                \\}
                macro .endmultisection
                \\{
                        end if
                        purge section,library,import,export,.stream
                        macro .stream frames
                        \\\{     
                                match ,skipstream
                                \\\\{
                                        streamcuRRent equ frames
                                        if 0
                                                match f, @__file__@ \\\\\{ include f \\\\\}
                                        end if
                                        restore streamcuRRent
                                        match arg,cuRRentSection \\\\\{purge arg,.\\\\\#arg,\
                                                arg\\\\\#once,.\\\\\#arg\\\\\#once\\\\\}
                                        purge .stream,.frame,..frame,.frameonce,..frameonce,\
                                                section,library,import,export,proc,endp
                                        define skipstream
                                \\\\}
                        \\\}
                \\}
                macro .stream param \\{ \\}
                macro .frame param \\{ .comment \\}
                macro ..frame param,string& \\{ \\}
                macro .frameonce param& \\{ .comment \\}
                macro ..frameonce param& \\{ \\}
                define librarygathering
                macro library [namelib,filelib]
                \\{ \\common
                        end if
                        \\forward
                        match ,libraryonce\\#namelib \\\{ \\\}
                        match some,libraryonce\\#namelib
                        \\\{define libraryonce\\\#namelib
                                match some,librarygathering
                                \\\\{librarygathering equ librarygathering,namelib,filelib\\\\}
                                match ,librarygathering
                                \\\\{librarygathering equ namelib,filelib\\\\}
                        \\\}
                        \\common
                        if 0
                \\}
                macro import namelib,[nameimp,apiimp]
                \\{
                        \\common
                        end if
                        \\forward
                        match ,importonce\\#nameimp \\\{ \\\}
                        match some,importonce\\#nameimp
                        \\\{
                                define importonce\\\#nameimp
                                match ,importnamelibonce\\\#namelib
                                \\\\{
                                        importgathering\\\\#namelib equ importgathering\\\\#namelib,nameimp,apiimp
                                \\\\}
                                match some,importnamelibonce\\\#namelib
                                \\\\{
                                        define importnamelibonce\\\\#namelib
                                        define importgathering\\\\#namelib namelib,nameimp,apiimp
                                \\\\}
                        \\\}
                        \\common
                        if 0
                \\}
                macro export namelib,[nameexp,apiexp]
                \\{
                        \\common
                        end if
                        \\forward
                        match ,exportonce\\#namelib\\#nameexp \\\{ \\\}
                        match some,exportonce\\#namelib\\#nameexp
                        \\\{define exportonce\\\#namelib\\\#nameexp
                                match ,exportnamelibonce\\\#namelib
                                \\\\{
                                        exportgathering\\\\#namelib equ exportgathering\\\\#namelib,nameexp,apiexp
                                \\\\}
                                match some,exportnamelibonce\\\#namelib
                                \\\\{
                                        define exportnamelibonce\\\\#namelib
                                        define exportgathering\\\\#namelib namelib,nameexp,apiexp
                                \\\\}
                        \\\}
                        \\common
                        if 0
                \\}
                macro proc string&
                \\{     end if
                        if 0
                        match ,skipstream 
                        \\\{match arg,string 
                                \\\\{
                                        end if
                                        proc arg 
                                        if 1
                                \\\\}
                        \\\}
                \\}
                macro endp
                \\{     end if
                        if 0
                        match ,skipstream 
                        \\\{
                                end if
                                endp
                                if 1
                        \\\}
                \\}
                postpone \\{.endmultisection\\}
                macro   invoke param& \\{match ,skipstream \\\{invoke param\\\} \\}
                macro   cominvk param& \\{match ,skipstream \\\{cominvk param\\\} \\}
                macro   comcall param& \\{match ,skipstream \\\{comcall param\\\} \\}
                macro   align param& \\{match ,skipstream \\\{align param\\\} \\}
                macro   stdcall param& \\{match ,skipstream \\\{stdcall param\\\} \\}
                macro   fastcall param& \\{match ,skipstream \\\{fastcall param\\\} \\}
                macro   du param& \\{match ,skipstream \\\{du param\\\} \\}
                if 1
        \}
}    

Let me know if you want something like gathered resource directives or some other functionality.

Examples:


Description:
Filesize: 27.75 KB
Viewed: 510 Time(s)

Example2.PNG


Description:
Filesize: 26.65 KB
Viewed: 510 Time(s)

example1.PNG




Last edited by Overclick on 02 Sep 2022, 04:07; edited 12 times in total
Post 21 Jul 2020, 11:27
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18844
Location: In your JS exploiting you and your system
revolution
Post 21 Jul 2020, 11:30
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Seems like idea is to use some sort of different names, but I think we can use files to store all subsections together.
Post 21 Jul 2020, 11:36
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1251
Roman
Good macro somthing like this:

macro .dll [arg] {
section '.idata' import data readable writeable
library arg,`arg#'.DLL',\


import_#arg

}

In code:
.dll kernel32,user32,gdi32
Post 22 Jul 2020, 12:39
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
Overclick wrote:
This is why I like masm -- simple sections, sticked together by compilation. I mean .code .data...Any macro can make parts for sections. What's wrong with fasm?


I don't know man, the last time I tried, MASM's '.data' macro still unable to align to 32-byte boundaries. Horrible macro for dealing with YMM data. Had to move down to raw "dseg segment". I hope it is already fixed by now.
Post 24 Jul 2020, 01:03
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
I did it
Code:
macro .multisection entry_file {        
        macro .data \{ 
                end if 
                if current='data'
        \}
        macro .idata \{ 
                end if 
                if current='idata'
        \}
        macro .code \{
                end if
                if current='code'
        \}
        current='idata'
        section '.idata' import data readable writeable
        if current='idata'
                include entry_file
        end if
        current='data'
        section '.data' data readable writeable 
        if current='data'
                include entry_file
        end if
        current='code'
        section '.text' code readable executable
        entry $
        sub rsp,8
        if current='code'
                include entry_file
        end if
}       

.multisection 'MyWorkFile.asm'     

Wink
Post 30 Jul 2020, 14:01
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1251
Roman
How use this ?
What code in MyWorkFile.asm ?
Post 30 Jul 2020, 14:50
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Put this macro on header/Main file or include it to there.
Write your program on some separate file then put it there by
.multisection '<your program>'
Post 30 Jul 2020, 14:57
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Upd: Use it carefully with "fix" where you need it. Declare it before or after multisection.
Post 31 Jul 2020, 16:14
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
UPDATED RELEASE Cool :
Code:
macro .multisection [entry_file] {
        common
        macro proc [param] \{ 
                end if
                if cuRRent='code'
        \}      
        macro endp \{
                end if
                if cuRRent='code'
        \}
        macro .data \{ 
                end if
                if cuRRent='data'
        \}
        macro .idata \{ 
                end if
                if cuRRent='idata'
        \}
        macro .code \{
                end if
                if cuRRent='code'
        \}
        macro .comment \{
                end if
                if cuRRent='comment'
        \}  
        section '.idata' import data readable writeable 
        cuRRent='idata'
        forward
        if cuRRent='idata'
                include entry_file
        end if
        common
        section '.data' data readable writeable 
        cuRRent='data'
        forward
        if cuRRent='data'
                include entry_file
        end if
        common
        cuRRent='code'
        purge proc
        purge endp
        macro proc [param] \{ 
                \common
                end if
                proc param
                if cuRRent='code'
        \}      
        macro endp \{
                end if
                endp
                if cuRRent='code'
        \}
        section '.text' code readable executable
        entry $
        sub rsp,8
        forward
        if cuRRent='code'
                include entry_file
        end if
}    

FEATURES:
1. Linker. It will combine all your sections in all project files at same time.
2. Proc. You can use multisections inside procedures.
3. Unlimited sections any time you need it.
4. Easy to add some more kind of sections like .rsrc
5. Short names like masm .code .data (up to you actually)
6. No need of .end
7. Comment whole block by .comment

Do not use fix inside linked files. Do not declare sections inside if operators.
To enjoy it put(include) the macro to Header(Main file) then link your project files by:
Code:
.multisection '<your project file 1>','<your project file 2>','<your project file n...>'
    


Last edited by Overclick on 09 Sep 2020, 23:45; edited 1 time in total
Post 07 Aug 2020, 06:26
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
New feature:
.comment
Code:
        macro .comment
        \{
                end if
                if cuRRent='comment'
        \}    

Easiest way to comment whole block of code when you need it. Enjoy Wink
Post 09 Sep 2020, 23:43
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
New feature:
.dataonce <unique name>
Code:
macro   .dataonce name
        {
        end if
        if name=0 &cuRRent='data'
                name=1
        }    

Example:
Code:
dataSomeMacro = 0                     ;declare unique name
macro SomeMacro [arg]
        .dataonce dataSomeMacro          ;once again
                Param1 db ?
                Param2 db ?
        .code                                    ;finish job by declaring next section.
                mov [Param1],1
                lea arg,[Param2]
        }    

SomeMacro rsi
...
SomeMacro rdi
...
SomeMacro rax

Data block will be added to data section just once. No matter how many times you use your macro. Don't try to use multisection blocks inside 'if -end if' constructions, just in main macro body. But you can use 'if - end if' inside that sections as normally do.
Have a look to my new macro .function. That one can be used with 'if - and if' without problems.
https://board.flatassembler.net/topic.php?t=21847
I tried to hide that ugly unique names for all, but not success. Have to be used with names for a while Smile
Post 11 Apr 2021, 12:38
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Release updated, check the first comment

Added .namespace
Fixed .dataonce -- no need to declare unique name before macro any more, only next to directive
Post 05 Apr 2022, 02:11
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 652
Location: Russia
macomics
Overclick wrote:
Code:
macro .namespace param
        \{      
                end if
                define currentnamespace param
                if cuRRent='data'
                        restruc dd
                        struc dd data&
                        \\{
                                common
                                restore .
                                . #@\\#param dd data
                                . equ . #@\\#param
                                list@\\#param equ .,list@#param
                        \\}
                end if
                current@namespace equ list@\#currentnamespace fake@fake
                setnamespace \current@namespace
                if cuRRent=lastcuRRent
        \}    

Is only `dd` enough?
And, in my opinion, when using the argument with &, it is necessary to check for <empty>.
Code:
struc dd data& { if ~ data eq
...
end if }    
Post 05 Apr 2022, 06:04
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Quote:

And, in my opinion, when using the argument with &, it is necessary to check for <empty>.

empty data gonna generate some error, but why not

dd just for demonstration. I created db dw and dq but need to fix proc before release.
Some passed values undefined yet.
Post 05 Apr 2022, 13:56
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
All fixed.
I'll do structures and jumps later
Post 11 Apr 2022, 09:04
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
How about this? Do you like single line data includes? Even more it works inside labelspaces )


Description:
Filesize: 55.37 KB
Viewed: 2346 Time(s)

Capture.JPG


Post 20 Apr 2022, 14:48
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
I'm trying to get possible to use subsections inside label-spaces but it woun't. So the solution is a single line data macro only. (Not updated yet)
Post 20 Apr 2022, 15:05
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
OMG I fixed that issue too


Description:
Filesize: 52.68 KB
Viewed: 2332 Time(s)

Capture.JPG


Post 20 Apr 2022, 15:20
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 576
Location: Ukraine
Overclick
Anonce:
New superior directives coming soon:
.frame <unique name>
.stream <unique name>

Absolute modular programming. Your may place any data/code to any predefined places.

Example:
Code:
.data
  ;...
  somestructure:
  .stream one
.code
  ;...
  .stream one
  .stream two
  ;...
  .data  ;may be a macros
    .frame one
      >>elements of structure or any other data sticked together
  .code
    .frame one
      >>element of code sticked together
    .frame two
  ;...
  .data  ;may be macros
    .frame one
      >>elements of structure or any other data sticked together
  .code
    .frame one
      >>element of code sticked together    

You also may stream frames multiply times
Post 21 Apr 2022, 08:53
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:  
Goto page 1, 2, 3  Next

< 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.