NASM Forum > Using NASM

How do you make an NASM macro use different code for 16/32 bits?

(1/1)

ben321:
Lets say a macro included something like this:
MOV EAX,[ptrData]

That's for 32 bit code. Let's say I wanted the macro to be usable in both 32 bit and 16 bit code though, so I didn't need to write 2 separate macros. Is there like predefined constant that NASM can set, that will let you tell your macro to behave differently? For the above example, I'd want the 16 bit code to say:
MOV AX,[ptrData]

debs3759:
That's where I'd use things like #ifdefine and it's companions. Nasm doesn't write your code for you.

ben321:

--- Quote from: debs3759 on March 12, 2023, 03:42:01 PM ---That's where I'd use things like #ifdefine and it's companions. Nasm doesn't write your code for you.

--- End quote ---

Isn't ifdefine a C instruction though? I didn't know NASM could do that. I had hoped it would. So I'd use it like this then.

#ifdefine Is16Bits
;16bit code goes here
#else
;32bit code goes here
#endif


How do I do the "Is16Bits" part though? Does NASM provide a compiler-set constant that is either defined or not defined, based on what bitness mode is currently being used?

debs3759:
I just looked it up in the manual (section 4.4.1, you will find it helpful to read the manual), and I should have said #ifdef. The code would be


--- Code: ---#ifdef Is16Bits
;16bit code goes here
#else
;32bit code goes here
#endif

--- End code ---

fredericopissarra:
Nasm doc 5.3:

--- Code: ---%if __?BITS?__ == 16
...
%elif __?BITS?__ == 32
...
%elif __?BITS?__ == 64
...
%endif
--- End code ---

Navigation

[0] Message Index

Go to full version