nasm include

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

nasm include

Post by huh »

I use nasm for my work. Say I have a file named init.asm executed by the bootloader which includes some files but 1 of the included files need a system call from a file not included by init.asm, so I could include the needed file in the include file BUT then if init.asm executes another file called main.asm which includes the files included in init.asm PLUS the file needed for 1 of those included files nasm would show a "conflicting defenitions" error. So is there any way of including a file and making sure that no other files include it one more time?
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: nasm include

Post by bubach »

I don't see the problem, why complicate it so much when all you need to do is to only include everything needed in the main file? If something is needed by an included file, you just make sure to include that too in the main file. Also, finding what you needed took 30 seconds with google, it's in the nasm manual:
4.5 Including Other Files

Using, once again, a very similar syntax to the C preprocessor, NASM's preprocessor lets you include other source files into your code. This is done by the use of the %include directive:

%include "macros.mac"

will include the contents of the file macros.mac into the source file containing the %include directive.

Include files are searched for in the current directory (the directory you're in when you run NASM, as opposed to the location of the NASM executable or the location of the source file), plus any directories specified on the NASM command line using the -i option.

The standard C idiom for preventing a file being included more than once is just as applicable in NASM: if the file macros.mac has the form

%ifndef MACROS_MAC
%define MACROS_MAC
; now define some macros
%endif


then including the file more than once will not cause errors, because the second time the file is included nothing will happen because the macro MACROS_MAC will already be defined.

You can force a file to be included even if there is no %include directive that explicitly includes it, by using the -p option on the NASM command line (see section 2.1.7).
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: nasm include

Post by qw »

Some good advice: don't put code in include files. EQU's, %define's and %macro's, fine, but no code. If you need some code, it should be in INIT.ASM to start with.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: nasm include

Post by Solar »

As opposed to C/C++, where the calling code needs to know certain things about the called code (parameter list, return type), such requirements do not exist for ASM. (Besides, even C/C++ can give you trouble if you include code in header files.)

You can have init.asm calling a function implemented in lib.asm, and you can have main.asm calling the same function in lib.asm, and you don't need to "include" anything. You simply assemble each of the *.asm files into an *.o file and link them together, just like that.

If you want to share certain %defines or %macros across files, +1 to what Hobbes said.
Every good solution is obvious once you've found it.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: nasm include

Post by bluemoon »

Solar wrote:You can have init.asm calling a function implemented in lib.asm, and you can have main.asm calling the same function in lib.asm, and you don't need to "include" anything. You simply assemble each of the *.asm files into an *.o file and link them together, just like that.
I think you need EXTERN external_function_name for that? although you can have that line in your ASM, or centralize it in a header file.
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: nasm include

Post by invalid »

Also, you don't need to declare a symbol "before use" (i.e. in some previous source line, as it is in C). So, if you split the code into several files, then in your main file you can do:

Code: Select all

%include "somefile.asm"
%include "otherfile.asm"
The symbols in both files will be visible between them (as long as the main file is an input to nasm!).
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: nasm include

Post by invalid »

berkus wrote:That looks a lot more like a bad side-effect rather than something desirable.
Well, that's a feature - not a bug - so the outcome depends on the user.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: nasm include

Post by bubach »

berkus wrote:
ydoom wrote:The symbols in both files will be visible between them (as long as the main file is an input to nasm!).
That looks a lot more like a bad side-effect rather than something desirable.
It's not. That's standard behavior. Besides, that's exactly what I tried to tell him in my first reply almost a month ago. No idea where this sudden interest from everybody to reply so late came from.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply