flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [solved]conditional include | self-include once

Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 02 Aug 2022, 18:10
Hey everyone
How can I prevent include extraction?
I need it for self-include mechanics where mainfile includes itself for some reason but only once and prevent loop extraction.


Last edited by Overclick on 04 Aug 2022, 08:36; edited 1 time in total
Post 02 Aug 2022, 18:10
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 02 Aug 2022, 19:30
main.asm
Code:
format binary as "txt"
define __MAIN__ ".\test.asm"
include __MAIN__    

test.asm
Code:
db "Test string",13,10
match any, __MAIN__ { define __MAIN__ ".\stub.asm"
include any }    

stub.asm
Code:
; empty    

result
Code:
fasm -m 1024 ./main.asm
flat assembler  version 1.73.30  (1024 kilobytes memory)
1 passes, 26 bytes.

hd -C ./main.txt
00000000  54 65 73 74 20 73 74 72  69 6e 67 0d 0a 54 65 73  |Test string..Tes|
00000010  74 20 73 74 72 69 6e 67  0d 0a                    |t string..|
0000001a    
Post 02 Aug 2022, 19:30
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 02 Aug 2022, 19:46
main.asm
Code:
match , __MAIN__ { format binary as "txt"
define __MAIN__ ".\main.asm" }
db "Test string   ", 13, 10
match any, __MAIN__ { define __MAIN__ ".\stub.asm"
include any }    

stub.asm
Code:
; empty    

result
Code:
fasm -m 1024 ./main.asm -d __MAIN__=
flat assembler  version 1.73.30  (1024 kilobytes memory)
1 passes, 32 bytes.

hd -C ./main.txt
00000000  54 65 73 74 20 73 74 72  69 6e 67 20 20 20 0d 0a  |Test string   ..|
00000010  54 65 73 74 20 73 74 72  69 6e 67 20 20 20 0d 0a  |Test string   ..|
00000020    
Post 02 Aug 2022, 19:46
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 02 Aug 2022, 20:06
may be this:
TEST24.ASM
Code:
match =%include, include {
        display 'successed include',13,10
        macro %include any& \{ \} }
match =include, include {
        macro %include any& \{ include any \} }
include fix %include
;macro skip any& {}
match file,__file__ { display 'try include ',`file,13,10 }
include 'test24.asm'
include fix include ; after that point includes operated as usual
include 'test23.asm' 
db 2    

TEST23.ASM
Code:
db 2    

output
Code:
1 passes, 4 bytes
try include C:\fasmpack\TESTS\TEST24.ASM
successed include
try include C:\fasmpack\TESTS\test24.asm
    

each test23 & test24 produce by 1 byte, test24 nested once - in summary 4 bytes
Post 02 Aug 2022, 20:06
View user's profile Send private message Send e-mail Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 798
Location: Russian Federation, Sochi
ProMiNick 02 Aug 2022, 20:37
more clear variant (in first case "match =include, include" was always true - so it additional delay to preprocessor, more tricker match fix that)
TEST24.ASM
Code:
match first =%include rest, :include %include : {
        match :,first \{
        display 'include once',13,10
        macro %include any& \\{ \\} \}
        match :,rest \{
        display 'prepare include once',13,10
        macro %include any& \\{ include any \\} \} }
include fix %include
include 'test24.asm'
include fix include

include 'test23.asm'
db 2    
without displays macro(match block) is shorter enought
TEST23.ASM
Code:
db 2    

output
Code:
1 passes, 4 bytes
prepare include once
include once
    
Post 02 Aug 2022, 20:37
View user's profile Send private message Send e-mail Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4020
Location: vpcmpistri
bitRAKE 02 Aug 2022, 22:09
In fasmg there is the sneeky:
Code:
if __source__=__file__
        display "main",9,__file__,10
        eval 'include "',__file__,' "' ; note: extra trailing space
else
        display "inc",9,__file__,10
end if    
... to include the primary source file one additional time. This works with full paths, whereas the ".\" method only works with relative paths unless the path is parsed.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 02 Aug 2022, 22:09
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Aug 2022, 21:45
Thanks for good tips but no one fill my needs yet.
Extra file and -d parameter is bad idea.
Touching 'Include' not allowed by fasm or I doing something wrong.
And I don't want to use FasmG.
I trying a lot of different combo and fixing other issues at the moment but first of all I found this to pass first run only:
Code:
 match any,skip {
                define skip
                ...
                        }
    
Post 03 Aug 2022, 21:45
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 926
Location: Russia
macomics 03 Aug 2022, 22:28
Overclick wrote:
Touching 'Include' not allowed by fasm or I doing something wrong.

You are definitely doing something wrong.

./main.asm
Code:
format binary as "txt"
match text, __file__ { db `text, 13, 10 }
define __MAIN__ "./test.asm" ; This constant must be defined outside of the self-included file. If you need to set it for the main file, then I used the -d key
include "./self.asm"
match text, __file__ { db `text, 13, 10 }
include "./test.asm"
match text, __file__ { db `text, 13, 10 }    


./test.asm
Code:
match text, __file__ { db `text, 13, 10 }
match any, __MAIN__ { define __MAIN__
                      include any }    


./self.asm
Code:
match text, __file__ { db `text, 13, 10 }
match f=%include r, :include %include : {
  match :,f \{ macro %include any& \\{ \\} \}
  match :,r \{ macro %include any& \\{ include any \\} \} }
include fix %include
include "./self.asm"
include fix include
include "./test.asm"    


result
Code:
fasm -m 1024 ./main.asm
flat assembler  version 1.73.30  (1024 kilobytes memory)
1 passes, 134 bytes.    


output: ./main.txt
Code:
./main.asm         ; ./main.asm [2]
././self.asm       ; ./main.asm [4] -> ./self.asm [1]
./././self.asm     ; ./main.asm [4] -> ./self.asm [7] -> ./self.asm [1]
././././test.asm   ; ./main.asm [4] -> ./self.asm [7] -> ./self.asm [9] -> ./test.asm [1]
./././././test.asm ; ./main.asm [4] -> ./self.asm [7] -> ./self.asm [9] -> ./test.asm [3] -> ./test.asm [1]
./././test.asm     ; ./main.asm [4] -> ./self.asm [9] -> ./test.asm [1]
./main.asm         ; ./main.asm [5]
././test.asm       ; ./main.asm [6] -> ./test.asm [1]
./main.asm         ; ./main.asm [7]    

I added comments to the result to make it clearer from which file which line generated the corresponding entry (if you can't decipher the tree of leading points).
Post 03 Aug 2022, 22:28
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 669
Location: Ukraine
Overclick 03 Aug 2022, 23:01
As I said I don't need extra files.
Also I already done it by macro:
main.asm
Code:
match ,skip 
{
   macro runinclude params& \{ \}
}
match any,skip 
{
   define skip
   macro runinclude params& \{ params \}
}
runinclude include 'main.asm'
    
Post 03 Aug 2022, 23:01
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.