3

What is the difference between %assign and %define in NASM assembly x86? Because for me it looks like they are the same thing, well, not really. Is %assign just a %define for which you can change later in the code?

ecm
  • 2,583
  • 4
  • 21
  • 29
C. Cristi
  • 569
  • 1
  • 7
  • 21

1 Answers1

3

They are not part of x86 assembly, they’re specific to nasm. They’re similar but not quite: %assign defines a numeric constant and it can be redefined. It cannot take arguments.%define defines a numeric or string valued macro, it can take arguments, and can also be redefined.

More information in the documentation

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • 1
    `%define` is always a text substitution, isn't it? So the ability to redefine it doesn't let you do stuff like `%define i i+1` in a `%rep`. – Peter Cordes Mar 20 '22 at 22:32
  • @PeterCordes: `%assign` also works if you have some arithmetic involving `$` or `$$`, which would be evaluated later than the directive, at use of the macro, if used with `%define`. – ecm Mar 21 '22 at 05:58
  • 2
    @ecm: Someone should probably write up a more detailed answer to this, since [Is there a point in using %assign macro if %define macro has more functionality (NASM)?](https://stackoverflow.com/q/71551054) has now been updated to clarify that this Q&A wasn't sufficiently helpful. I may get around to that, but if you'd like to cook up an answer with some useful examples that'd be great. – Peter Cordes Mar 21 '22 at 06:28
  • @PeterCordes: If you end up writing it, also consider `%xdefine`. Your `i + 1` example could actually use `%xdefine` too, it would just need way more space and time for NASM to keep track of the expansion. (Eg in a `%rep` loop of a thousand iterations and starting at `%define i 0` the define would end up using thousands of bytes.) – ecm Mar 21 '22 at 07:06