In the assembler I use we have the concept of macro libraries. We set up an assembly by defining what macro libraries should be used and then when the assembler comes across anything that isn't a valid opcode it searches the macro libraries for a macro with the same name as what is in the opcode field. If the macro exists it is expanded and assembly continues. If not you get an undefined opcode error or something like that. In other words we don't have to do anything to use a macro except to place it into a library and then use it in code. There is no need to include it in the source or have any statement required to get the macro recognized by the assembler. If you reference it in code it will be found and used.
In NASM from the doc it looks to me like we should do a %include for the specific macro and then also add the -I on the command line specifying the path to the directory where the macro specified in the %include is found. Is that correct? I mean it works, but is that the right way to do this? If so, that seems to imply for every macro we use we must always have a specific %include statement. Is that the way this works or is there a simpler way?
I would like to have a macro directory where I keep all my macros and then just be able to use them by referring to them in an assembly source file, without a specific %include. How do you deal with programs that need many macros? Do you just nest an %include to include all the macros you have just in case they are needed, and is this ok for assembly performance etc. Or is there a better way.
If I understood you correctly, you are currently using a lot of files and each file contains one %macro ... %endmacro structure. If that is the case: it's unnecessary to have only one %macro ... %endmacro structure in an included file, you can put as many macro definitions into one single include file as you want. (However, nesting %include directives as you suggested is allowed as well, and actually using both nested %include directives and files containing a lot of %macro ... %endmacro definitions together might make sense.)
For example, I have a single file named CMMACROS.MAC in which I write all macros not related to one specific program. I %include that single file in my source code (.ASM) file and can then access any macro defined in the .MAC file. (Naming the file so that it ends in .MAC isn't necessary; you can for example name your included files so they end in .INC, or even .ASM; in fact, NASM doesn't care about the name at all.) To collect macros only used by one specific program, I sometimes keep .MAC files in the same directory as that program's source code - if that is the case, you can %include that file without even having to specify the -I option because the source code file's directory is automatically added to NASM's include pathes. However, sometimes I will simply specify the %macro ... %endmacro definition directly in the source code - that is allowed too; if you don't want to, you do not have to use %include for macro definitions at all.
2nd question is I am going through a book on assembly and the use of the symbol name with resb is not consisent in that book. In one case there is something like
a_name resb 1
and in another case
a_different_name: resb 1
both forms with and without colon seem to work. Is the colon name the preferred way to write this, seems to be from the NASM doc. Does the name without the colon work by mistake or is it just the coder's choice which one to use?
First, the name before resb is an ordinary label to NASM; therefore, everything which applies to labels in your program's source code applies to the label in front of resb too.
In NASM source code, specifying a colon is indeed the preferred way, although for compatibility omitting the colon is allowed. If nothing follows the colon, NASM warns you that you may have intended to write something else. (For example, if you type "rret" instead of "ret", it will be parsed as a label and assembling the source code will succeed. The warning can help you find such errors.) (If you prefer not to use colons when defining labels, you can disable that warning.) There is no warning in NASM for missing colons if something follows the label definition (for example, a mnemonic or resb).
While the suppressible warning that I described exists, both specifying colons and omitting them are valid and allowed.
Didn't know whether to start two threads or just put both questions into this post even though they're unrelated.
Well, I guess this is the correct sub-forum for both so I think it should be okay. However, the thread's title is still "Questions on macros".