6

I have a bunch of magic numbers that I would like to include in both a C program and an assembly file to be compiled by nasm or yasm.

In plain C the file would look something a series of defines, like:

#define BLESS   55378008
#define ANSWER        42
...

In nasm or yasm, the same include could be implemented as:

%define BLESS   55378008
%define ANSWER        42
...

The only difference is that leading character before the define: # for C and % for nasm.

Is there any way to write a polygot include that allows me to include it in both C and nasm and only list the constants once?

Yes, I'm aware that I could just use sed or whatever to generate one file from the other.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • 5
    You can run also C preprocessor over the asm file, including the only-#define C header. – Ped7g Oct 25 '17 at 00:48
  • 4
    Without some external tool, the answer is no. This has been an ongoing question in the NASM forums for years and there is no way without some external parsing to do what you want. NASM has a contributor based project called h2incn. Many external options m4, cpp, sed etc. – Michael Petch Oct 25 '17 at 00:48
  • @MichaelPetch - if you write that as an answer it would be accepted. – BeeOnRope Dec 07 '17 at 19:11

1 Answers1

6

NASM by itself has no way to include C header files in assembly code. This has been brought up in the NASM forum through the years. You will need an external tool to parse the C header files into something usable with NASM assembly syntax.

One such 3rd party contribution that is suppose to be compatible with NASM is h2incn. I haven't tested it thoroughly enough so can't say it is stable or usable enough for all use cases.

The alternative is to pre-process the files with other tools like m4, cpp, or even translating with sed

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Has anything changed in the latest version of NASM? Or it is still required to run some external C-header files preprocessor to convert it into a NASM defines? – St.Antario Oct 01 '19 at 05:47
  • 1
    @St.Antario Nothing has changed, a third party utility is still required. – Michael Petch Oct 01 '19 at 05:49