flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Avoid multiple include

Author
Thread Post new topic Reply to topic
Dasn



Joined: 22 Nov 2004
Posts: 10
Dasn
Hi guys, is it possible to avoid including a file more than one time? For example, assuming 'a.inc' includes 'win32ax.inc'; and 'b.inc' also includes the 'win32ax.inc'; then if I write a 'main.asm' which include both 'a.inc' and 'b.inc', I will get some errors, because the 'win32ax.inc' was included twice.

Is it possible to use something like C preprocessor's :
#ifndef XXX
.... code ...
#endif

in fasm?

Thanks in advance.
Post 28 Dec 2009, 11:51
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1153
ManOfSteel
Include Windows headers in the assembly file only (main.asm) instead of the include files (a.inc and b.inc).

Why make things complicated when they are so simple?
Post 28 Dec 2009, 11:58
View user's profile Send private message Reply with quote
Dasn



Joined: 22 Nov 2004
Posts: 10
Dasn
ManOfSteel wrote:
Include Windows headers in the assembly file only (main.asm) instead of the include files (a.inc and b.inc).

Why make things complicated when they are so simple?


Thanks for your quick reply Man. Very Happy

Because I often keep my library functions in separate files like 'a.inc' and 'b.inc'. In this example, the 'a.inc' may contain a library function, say 'a_function'; and 'b.inc' may contain a 'b_function', both of which I want to use in my 'main.asm' program, so I include 'a.inc' and 'b.inc' in the 'main.asm'. And both 'a_function' and 'b_function' depend on 'win32ax.inc'.
Post 28 Dec 2009, 12:47
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7108
Location: Slovakia
vid
This is problematic to do in FASM. Therefor, most people use the system described by ManOfSteel - it surely has its ups and downs.

I think there WAS some macro which was capable of doing conditional include, years ago, but I can't remember anything specific
Post 28 Dec 2009, 13:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Hapkidoyoka



Joined: 03 Mar 2008
Posts: 13
Hapkidoyoka
Since eqtype was moved to the pre-parser, I have used something like this.

Code:
;example for 'filename.inc'

if inc_filename eqtype 1
 define inc_filename "included"
;**************************************************

Your code/equates/structures here

;**************************************************
end if ;end of include
    


You can then include the file normally, and as many times as you like,

Not too good for large projects though - as the file will be loaded a second time but not processed (beyond the wrapping if/end if).
Post 28 Dec 2009, 14:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16901
Location: In your JS exploiting you and your system
revolution
Put this in a file and 'include' it in the top of each module. Then use the marco 'includeonce' to include other files multiple times and only one instance is used.
Code:
match =files@included,files@included{

       macro nest_includeonce\{
              macro includeonce path,[instr]\\{\\common
                  file@include equ path
                       match head path tail,files@included\\\{file@include equ\\\}
                 match head path,files@included\\\{file@include equ\\\}
                      match file,file@include\\\{
                         files@included equ files@included path
                              nest_includeonce
                            include file
                                purge includeonce
                           irp i,instr\\\\{i\\\\}
                    \\\}
                \\}
  \}nest_includeonce

        macro show_included\{match x i,files@included\\{irps j,i\\\{display \\\`j,13,10\\\}\\}\}

   files@included equ x

}    
Post 28 Dec 2009, 14:51
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
vid wrote:
This is problematic to do in FASM.
Veeeery problematic:
Code:
; C version
#ifndef _SOME_FILE_H
#define _SOME_FILE_H
; code
#endif



; FASM version
match x, _SOME_FILE_INC
{
  define _SOME_FILE_INC
  ; code
}    
yeah I can see how problematic it is! Rolling Eyes

_________________
Previously known as The_Grey_Beast
Post 28 Dec 2009, 22:52
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Borsuc wrote:
vid wrote:
This is problematic to do in FASM.
Veeeery problematic:
Code:
; C version
#ifndef _SOME_FILE_H
#define _SOME_FILE_H
; code
#endif



; FASM version
match x, _SOME_FILE_INC
{
  define _SOME_FILE_INC
  ; code
}    
yeah I can see how problematic it is! Rolling Eyes
I don't get it.. won't the FASM one only define it if it is already defined? Why is the x there?? Match confuses me.. Confused

_________________
Post 31 Dec 2009, 13:53
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
Azu wrote:
I don't get it.. won't the FASM one only define it if it is already defined? Why is the x there?? Match confuses me.. Confused
But it's NOT defined before... so any variable name you input will be translated to the variable name, not empty.

Example:

Code:
db example  ; example is undefined    
doesn't translate to
Code:
db ; empty    
but to the literal 'example'

It's as if you would do this:
Code:
define example example    
that's how all symbolic constants start, not with empty value Wink

and the 'x' is used to "match anything" -- in other words, it matches any symbol(s) available, which happens all the time unless the symbolic constant is empty... that's what the define does Wink

_________________
Previously known as The_Grey_Beast
Post 31 Dec 2009, 15:38
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Yes I know but the C version only defines if it is empty so isn't it the opposite as the FASM version? Confused

_________________
Post 31 Dec 2009, 15:43
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
the C version defines it if it's not defined.

In FASM, the default value of a symbolic constant is its name, not an empty value. So it's the opposite: if you find it is not empty (which it is not by default!), you make it empty to prevent it the second time.

When you write:
Code:
label:
xor eax, eax    
"label" defaults to the value 'label' literally, not to an empty label:
Code:
:
xor eax, eax    


for example:
Code:
define label
label:
xor eax, eax    
gives error...

this is what it looks like by default:
Code:
define label label
label:
xor eax, eax    
Wink
Post 31 Dec 2009, 15:50
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
But.. won't that try to define it even if it is already defined? Like I put _SOME_FILE_INC equ 1 above it, it will not be empty, and it will try to define it again.. :/

I think the C version will only try to define it if you haven't already defined it yet.

_________________
Post 31 Dec 2009, 15:55
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
Why would you put _SOME_FILE_INC equ 1 above? Of course it would make it fail, it's not supposed to be defined anywhere else.

_________________
Previously known as The_Grey_Beast
Post 31 Dec 2009, 16:11
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Well the C version checks to make sure it isn't defined yet. If you don't care to check then a better comparison would be

Code:
; C version 
#define _SOME_FILE_H 
; code 



; FASM version 
define _SOME_FILE_INC 
; code 
    
Post 31 Dec 2009, 16:18
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
My method checks if it's empty or not... who said it has to be like C? It works as long as you don't fiddle with that symbolic constant, which applies to C too.

_________________
Previously known as The_Grey_Beast
Post 31 Dec 2009, 17:47
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
The C one won't define it and put in the code if it is already defined.

The ASM one will.

So they aren't different versions of the same thing. They are totally different things.
Post 31 Dec 2009, 17:58
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2468
Location: Bucharest, Romania
Borsuc
What are you talking about? Confused

The C one will check if it is defined -- first time it's not, then defines it. So second time it's included, it will be defined beforehand, and the "if" will be skipped. I mean that's the whole purpose: to include it only once.

The asm one does the same thing, except it uses the opposite signals. First time the constant is not empty, so the match matches and then makes it empty, so the second time (and third time etc) it gets skipped because it's empty. The only time it will match is the FIRST time the file gets included -- the exact same behavior as in the C case.

I'm not sure what you mean. Both include the file only once no matter how many times you subsequently include it.
Post 31 Dec 2009, 18:18
View user's profile Send private message 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-2019, Tomasz Grysztar.

Powered by rwasa.