NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: ben321 on March 12, 2023, 06:16:07 AM

Title: How do you make an NASM macro use different code for 16/32 bits?
Post by: ben321 on March 12, 2023, 06:16:07 AM
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]
Title: Re: How do you make an NASM macro use different code for 16/32 bits?
Post by: 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.
Title: Re: How do you make an NASM macro use different code for 16/32 bits?
Post by: ben321 on March 12, 2023, 11:13:33 PM
That's where I'd use things like #ifdefine and it's companions. Nasm doesn't write your code for you.

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?
Title: Re: How do you make an NASM macro use different code for 16/32 bits?
Post by: debs3759 on March 13, 2023, 01:37:32 AM
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: [Select]
#ifdef Is16Bits
;16bit code goes here
#else
;32bit code goes here
#endif

Title: Re: How do you make an NASM macro use different code for 16/32 bits?
Post by: fredericopissarra on March 13, 2023, 02:06:14 PM
Nasm doc 5.3:
Code: [Select]
%if __?BITS?__ == 16
...
%elif __?BITS?__ == 32
...
%elif __?BITS?__ == 64
...
%endif