Alexandru Cimpanu
Alexandru Cimpanu

Reputation: 1069

Can you add preprocessor directives in assembly?

I would like to execute some assembly instructions based on a define from a header file.

Let's say in test.h I have #define DEBUG.

In test.asm I want to check somehow like #ifdef DEBUG do something...

Is such thing possible? I was not able to find something helpful in the similar questions or online.

Upvotes: 7

Views: 8825

Answers (3)

old_timer
old_timer

Reputation: 71516

Sure but that is no longer assembly language, you would need to feed it through a C preprocessor that also knows that this is a hybrid C/asm file and does the c preprocessing part but doesnt try to compile, it then feeds to to the assembler or has its own assembler built in.

Possible, heavily depends on your toolchain (either supported or not) but IMO leaves a very bad taste, YMMV.

Upvotes: 1

Wouter Verhelst
Wouter Verhelst

Reputation: 1292

The C preprocessor is just a program that inputs data (C source files), transforms it, and outputs data again (translation units).

You can run it manually like so:

gcc -E < input > output

which means you can run the C preprocessor over .txt files, or latex files, if you want to.

The difficult bit, of course, is how you integrate that in your build system. This very much depends on the build system you're using. If that involves makefiles, you create a target for your assembler file:

assembler_file: input_1 input_2
        gcc -E < $^ > $@

and then you compile "assembler_file" in whatever way you normally compile it.

Upvotes: 7

Jester
Jester

Reputation: 58762

Yes, you can run the C preprocessor on your asm file. Depends on your build environment how to do this. gcc, for example, automatically runs it for files with extension .S (capital). Note that whatever you include, should be asm compatible. It is common practice to conditionally include part of the header, using #ifndef ASSEMBLY or similar constructs, so you can have C and ASM parts in the same header.

Upvotes: 8

Related Questions