Brain
Brain

Reputation: 321

How to use preprocessor macro in GCC basic asm?

I need to write a basic asm code in GCC, which uses an immediate constant defined in a header file. I know how to do this in extended asm, but how can I do it in basic asm, which does not have any input and output parameters?

Upvotes: 1

Views: 601

Answers (1)

Michael Petch
Michael Petch

Reputation: 47553

You can use a stringize type C preprocessor macro to convert a constant value to a string. You can then use that string to construct a basic inline assembly statement. An example would be:

#define STRINGIZE1(x) #x
#define STRINGIZE(x) STRINGIZE1(x)

#define STACK_ADDR 0x1000

int main()
{
    asm ("movl $" STRINGIZE(STACK_ADDR) ", %esp");

    return 0;
}

This example should generate this assembly instruction:

movl $0x1000, %esp

Note: this code is not meant to be a runnable example.

Upvotes: 2

Related Questions