Author Topic: Questions on macros  (Read 14246 times)

Offline JoeCoder

  • Jr. Member
  • *
  • Posts: 72
  • Country: by
Questions on macros
« on: June 09, 2011, 10:38:47 AM »
First of all thanks to everybody involved in making NASM and NASMX available. That's a hell of alot of work that went into this project and thank you for saving the world from as(s).

Ok I am trying to learn x86 assembly language with NASM. Alot of questions I ask may sound strange because I use assembler for my job in a totally different hardware/software environment. I am trying to understand how the way I am used to working maps onto the x86 and NASM world if at all ;-)

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.

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? Didn't know whether to start two threads or just put both questions into this post even though they're unrelated. Thanks everybody.

Joe
« Last Edit: June 09, 2011, 10:42:21 AM by JoeCoder »
If you can't code it in assembly, it can't be coded!

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Questions on macros
« Reply #1 on: June 09, 2011, 12:09:39 PM »
The %include directive is the way to go when you need to include additional files into your source, whether they be macros or even other source code.
The -I command line switch tells Nasm where to find the files you reference when you don't include the path in the directive.
For example, the following two are identical as far as assembling:

file1.asm:
Code: [Select]
%include "/usr/src/macros/mymacros.inc"

assemble with:
nasm -f elf32 file1.asm file1.o

compared to:

file1.asm
Code: [Select]
%include "mymacros.inc"

assemble with:
nasm -f elf32 -I/usr/src/macros/ file1.asm file1.o


The include switch prevents you from having to hardcode paths directly in the source file.  This is the preferred way for maintaining projects.
The environment variable NASMENV=-I/usr/src/macros/ is yet another way.


Next, labels "should" be post-fixed with a colon although nasm allows generation of code without it.  You will eventually receive warning messages regarding label alone on a line if you don't use the colon.

Hope that helps.




Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: Questions on macros
« Reply #2 on: June 09, 2011, 12:21:18 PM »
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.

Quote
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.

Quote
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".
« Last Edit: June 09, 2011, 12:25:07 PM by cm »
C. Masloch

Offline JoeCoder

  • Jr. Member
  • *
  • Posts: 72
  • Country: by
Re: Questions on macros and includes
« Reply #3 on: June 09, 2011, 12:36:39 PM »
Thanks alot Rob and cm! You guys gave me good info. I guess I can export the nasm environment path in my shell scripts and point to my macro library which I would normally use for all projects. I didn't realize I could group multiple macros in one source file. I'm not sure I want to do that but it would definitely simplify the inclusion of them in one statement. Thanks for clearing up my confusion on the label issue as well. I guess the book wasn't proofread very well since the examples compiled they must have figured it works! I will go with the colons so I don't get messed up later on.
If you can't code it in assembly, it can't be coded!