I'm new to assembly language and this question came across to my mind: Which is better solution for structuring my code, macros or procedures? While code included in a macro is just copied to the sections needed, and allowing executions of code without costly jumps, it does add extra instructions that must be loaded at the execution of the program. Also, at procedures, while it reduces the code that need to be loaded in memory, it also adds up expensive jumps, going from a section of cod to another. Which one is better? Macros or procedures?
-
5Rule of thumb: macros for small chunks of code, procedures for larger chunks. – Paul R Dec 06 '12 at 17:30
-
1no different than macros vs functions in C or other languages. Has nothing to do with assembly. – old_timer Dec 06 '12 at 17:48
2 Answers
In general, you should be using procedures. Procedures help organize your code, can make it easier to debug and avoids duplicating the same code. Macros can be used when creating a common syntax for smaller tasks makes your code easier to read.
Of course, in cases where you are really concerned about performance and not at all concerned about the size of your code, you might stretch this general approach to favor a bit more macro use.
In the end, you have to look at each specific case. But I can tell you that I've inherited code before that overused macros. Not only did it duplicate more code, but it took a long time to figure out the source code because I had to track down all those macro definitions and figure out what they were actually doing.

- 65,341
- 71
- 269
- 466
Generally procedures make your code easier to read than macros but remember there are jump costs. Most important tip is this; if its a small chunk of code (i.e. you just want to do one or two things within the body of code), use a macro since what you're doing with the code is not worth the cost of the jump/procedure call. However, if you're writing a large chunk of code (i.e. you're doing many things within the body of code), you'll be better off with the procedure as the cost of the jump will be very small in such a case.

- 145
- 2
- 8