1

Recently was working on a project to include 64-bit code when assembled under ML64.exe. However, this code actually included the 32-bit MASM directives, so the build failed.

IFNDEF RAX

.686P
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
OPTION PROLOGUE:W32Prologue

ELSE

OPTION CASEMAP:NONE
OPTION PROLOGUE:W64Prologue
OPTION EPILOGUE:NONE

ENDIF

The fix was to change IFNDEF RAX to IFDEF RAX as follows

IFDEF RAX

OPTION CASEMAP:NONE
OPTION PROLOGUE:W64Prologue
OPTION EPILOGUE:NONE

ELSE

.686P
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
OPTION PROLOGUE:W32Prologue

ENDIF

Wrote a simple example to show why using IFNDEF RAX is misleading

rax.asm

option casemap:none

extrn printf:proc

.data

     szRAX db 'Symbol RAX exists',0ah,0
     
.code

main proc
    
     ifdef   RAX
     mov     rcx,offset szRAX
     call    printf
     endif     

     ifndef  RAX
     mov     rcx,offset szRAX
     call    printf  
     endif
  
     ret
     
main endp

end

Building and running shows this output

C:\masm32>ml64.exe rax.asm /link /subsystem:console /defaultlib:kernel32.lib /defaultlib:user32.lib /defaultlib:libcmt.lib
Microsoft (R) Macro Assembler (x64) Version 14.29.30146.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: rax.asm
Microsoft (R) Incremental Linker Version 14.29.30146.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/OUT:rax.exe
rax.obj
/subsystem:console
/defaultlib:kernel32.lib
/defaultlib:user32.lib
/defaultlib:libcmt.lib

C:\masm32>rax.exe
Symbol RAX exists
Symbol RAX exists

Why does MASM output the same string twice?

vengy
  • 1,548
  • 10
  • 18
  • I wouldn't expect RAX to be a preprocessor symbol / macro; it's a register name. Presumably there is something you can check for that does work reliably to detect bitness, similar to how C `#ifdef __x86_64__` works, but I don't know what MASM defines. Still I guess it's interesting that using a register name breaks MASM so badly that `ifdef` and `ifndef` are both true for the same name. – Peter Cordes Dec 25 '22 at 03:45
  • Very similar to https://stackoverflow.com/questions/2595550/detecting-architecture-at-compile-time-from-masm-masm64 – Michael Dec 27 '22 at 12:46

0 Answers0