首页 > 解决方案 > 如何将 asm() 与 const char* 一起使用而不是字符串文字?

问题描述

如果我这样做:

const char *str = "some assembly instructions";
asm(str);

CLion 会说“'asm' 中的预期字符串文字”

标签: cassemblyinline-assemblystring-literals

解决方案


asm不是在运行时调用的普通 C 函数,而是在编译时发出您指定的汇编代码的特殊指令。asm需要在编译时知道汇编指令,这只有在参数是字符串文字时才有可能:

正确的:

asm("some instruction");   // "some instruction" is known at compile time

不正确:

asm(str);    // at compile time it is potentially not known where
             // str points to

结论:您不能将字符串文字以外的任何内容传递给asm.


推荐阅读