user9370454
user9370454

Reputation:

Quotation mark in inline assembly

How can I use assembly instructions that contain quotation marks inside asm()? like:

asm(
".section .rdata,"dr"\n"
);

The code above gives me this error:

expected ')' before 'dr'

Upvotes: 0

Views: 1172

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 365277

Use "\"" to put a double quote inside a C string literal, as usual.


Doing what you're asking isn't necessarily 100% safe. You can have problems like Can't declare .data in inline assembly if you don't restore the right section.

If you change sections inside inline-asm, then it might be safe if the inline asm statement is at the very end of the C source file.

But preferably you should use .pushsection so you can leave the assembler in the same section you were in at the start, using .popsection. Doing this automatically for an existing .S file might only require somewhat more complicated text processing, and still be possible with a simple algorithm.

Although you're probably safe assuming that the assembler is currently in the .text section at the start of your inline asm, in which case .section .text at the end of your inline-asm string should be safe. (Guessing here.)


This is why multiple people have told you that it's definitely better to assemble a stand-alone .S file separately, and link the two object files.

I guess if you're using an online compiler that only allows one translation unit, this might be a useful hack, but for anything other than one-off testing, you should just get your build system to support .S files instead of this ugly hack of turning compiler output into inline-asm statements. (I assume this is compiler output, or you'd just have written it by hand.)

Upvotes: 1

Any string literal, not just the ones required by an asm declaration, can contain double quotes if they are escaped. You escape them with a \. So when applied to your example:

asm(
".section .rdata,\"dr\"\n"
);

Upvotes: 2

Related Questions