15

Code:

  %define x 0x03
  x equ 0x03

What's the difference between them?

alex
  • 438,662
  • 188
  • 837
  • 957
sam
  • 1,683
  • 3
  • 17
  • 25

1 Answers1

21

%define is a far more powerful way of doing macros, akin to the C pre-processor. In your simplistic case, there is not a lot of difference, using x in the source code will result in the constant 3 being substituted. There's a subtle difference in that equ statements are evaluated once, when they are first read and %define macros are evaluated whenever they are encountered in the source.

You can see the difference there between the two statement snippets:

addr   equ       $
       %define   addr $

In that case, addr, when encountered in the code, will have different values. In the first case, $ will be the location of the assembly position at the place where the equ is. In other words, where it's defined.

In the second case, it evaluates to the assembly location at the place where addr is used.

Where %define shines is with something like:

%define thricexplusy(x,y) (3 * x + y)

or:

%define ctrl(c) (c & 0x1F)
: :
mov     al, ctrl('z')

(or even considerably more complex things) which allow you to pass parameters to your macro, something not possible with a simple equ.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841