Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between %define and equ in NASM?

Tags:

nasm

Code:

%define x 0x03
x equ 0x03

What's the difference between them?

like image 892
sam Avatar asked Jan 01 '12 04:01

sam


1 Answers

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

like image 80
paxdiablo Avatar answered Oct 11 '22 12:10

paxdiablo