Search code examples
cgccassemblymasmgnu-assembler

How to force usage of GNU assembler directives in a MASM format assembly file, and vice versa


I have a MASM-format assembly file that is part of a C library. I want to have this file also assemble on *nix, which we assume will have gcc installed, and hence use the GNU assembler.

The problem I am running into is assembler directives; I wonder if there is a way to have both assemblers understand the same directives, or at least select from some options. There was some hope here:

http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

... but it was dashed when I realized it's for inline ASM.

When looking at Agner Fog's manuals, there was reference made to his 'objconv' tool, which I can use to take my object file, and then disassemble it into the form that I want. This is obviously less clean than what I had in mind.

I appreciate any suggestions. Thanks, -mw

example that works in MASM, but I need the GNUC part to contain 'reg1 = rdi', etc. when passed through 'as':

IFDEF _MSC_VER
    reg1 textequ <rcx>
    reg2 textequ <rdx>
    reg3 textequ <r8>
ELSEIFDEF __GNUC__
    reg1 textequ <rdi>
    reg2 textequ <rsi>
    reg3 textequ <rdx>
ELSE
    ;error
ENDIF

Solution

  • The solution from David Wohlferd above works. I'm just framing it in the context which solved my problem.

    To make Visual Studio pre-process this file which contains C preprocessor directives and valid assembly in Intel syntax (which is converted to AT&T syntax with a switch), make a pre-build event. Put "cl /EP topsecret.preasm > topsecret.asm" in that event for your project.

    Then, for each file (which you should include in your project sources) make a custom build event (right click file, go to properties, make it an item of type 'custom build tool'). Then make the command line contain the assembler command line that you want. For example,

    "ml64.exe /c -D "MS_COMPILER" /Fo"topsecret.obj" "Path\to\topsecret.asm"