Detecting architecture at compile time from MASM/MASM64
Asked Answered
T

3

8

How can I detect at compile time from an ASM source file if the target architecture is I386 or AMD64?

I am using masm(ml.exe)/masm64(ml64.exe) to assemble file32.asm and file64.asm. It would be nice to create a single file, file.asm, which should include either file32.asm, or file64.asm, depending on the architecture. Ideally, I would like to be able to write something like:

IFDEF amd64
include file64.asm
ELSE
include file32.asm
ENDIF

Also, if needed, I can run ml.exe and ml64.exe with different command line options.

Thanks!

Tampere answered 7/4, 2010 at 20:8 Comment(1)
Small nit: you assemble ASM files, you don't compile them :)Cameliacamella
A
7

If I understand you correctly, you're looking for some sort of built-in define that has a different value among 32 and 64 bit MASM versions. I once looked for something like that, but didn't find anything suitable.

However, it's easy enough to just define your own, e.g. AMD64 equ 1 at the start of your source file to select your desired code path, or at the ML/ML64 command-line, like /DAMD64. And then use IFDEF/IFNDEF, as you suggest.

Amal answered 8/4, 2010 at 7:3 Comment(2)
Thanks! I didn't see /Dmacro option from the command line, even though I looked at the command line options and even though it is the same as the option used by the C compiler :))Tampere
"you're looking for some sort of built-in define..." - what does MASM define? For CL, it defines _M_IX86 for x86 and _M_X64 for x64.Cameliacamella
M
13
IFDEF RAX

  ECHO "WIN64"

ELSE

  ECHO "WIN32"

ENDIF
Miun answered 6/2, 2012 at 3:59 Comment(1)
The weird thing is IFNDEF RAX doesn't seem to work... rax is always considered undefined by IFNDEF in either ML or ML64. So apparently one must IFDEF RAX/64-bit only/ELSE/32-bit only/ENDIF (ie. use only IFDEF, not its inverse)Raffle
A
7

If I understand you correctly, you're looking for some sort of built-in define that has a different value among 32 and 64 bit MASM versions. I once looked for something like that, but didn't find anything suitable.

However, it's easy enough to just define your own, e.g. AMD64 equ 1 at the start of your source file to select your desired code path, or at the ML/ML64 command-line, like /DAMD64. And then use IFDEF/IFNDEF, as you suggest.

Amal answered 8/4, 2010 at 7:3 Comment(2)
Thanks! I didn't see /Dmacro option from the command line, even though I looked at the command line options and even though it is the same as the option used by the C compiler :))Tampere
"you're looking for some sort of built-in define..." - what does MASM define? For CL, it defines _M_IX86 for x86 and _M_X64 for x64.Cameliacamella
O
1

The x86 and x64 instruction sets (not even mentioning stack usage and restrictions) are soooooo different that I wonder if that would make sense, anyway... IOW, writing efficient architecture-independent x86 / x64 code looks close to impossible to me. This is assembly, not some portable HLL.

That's also most likely the reason why there are two specific assemblers, ml.exe and ml64.exe, rather than a single ml.exe that would handle it all with the help of some extra directives.

Outfit answered 8/4, 2010 at 7:54 Comment(2)
For some reason, I really need to expose a single ASM file for both 32 and 64 targets. This file will only include either file32.asm, or file64.asm and nothing more.Tampere
"I wonder if that would make sense...." - a single source with an X86 and X64 implementations guarded with a C-like preprocessor macro makes a lot of sense to me.... That's why I am here.Cameliacamella

© 2022 - 2024 — McMap. All rights reserved.