多行内联汇编宏与字符串 [英] Multi line inline assembly macro with strings

查看:157
本文介绍了多行内联汇编宏与字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个宏( MY_MACRO ),该宏在某个部分( my_section)。
示例: MY_MACRO(200,我的第一个字符串%u%x);

I'm trying to implement a macro ("MY_MACRO"), which stores a string preceded by a 32 bit integer number in a certain section ("my_section"). Example: MY_MACRO(200, "my first string %u %x");

此处是我尝试过的选择以及面临的问题。我将不胜感激任何帮助。

Here are the options I tried and the problems I'm facing with. I would appreciate any help. (gcc 4.7.3. MIPS cpu)

#define MY_MACRO(_num, _string)\
asm volatile(".pushsection .my_section");\
asm volatile(".byte %0, %1, %2, %3" : : "i"((_num >> 24) & 0xFF), "i"((_num >> 16) & 0xFF), "i"((_num >> 8) & 0xFF), "i"(_num & 0xFF)); /* Store the number */ \
asm volatile(".ascii " #_string);\
asm volatile(".popsection");

编译错误(并非每次使用宏都会发生):

Compile error (it doesn't occur for each usage of the macro):

c:\Temp\ccpDEDnt.s: Assembler messages:
c:\Temp\ccpDEDnt.s:1024: Warning: .popsection without corresponding .pushsection; ignored

我认为原因是编译器优化会更改指令顺序(尽管每个asm指令都是易变的,则允许编译器更改顺序)。
问:是否可以在没有 #pragma 的情况下仅针对这些行的范围禁用编译器优化?
这个问题使我找到了将四个asm指令统一的解决方案。

I think the reason is a compiler optimization which changes the instructions order (although each asm instruction is volatile, the compiler is allowed to change the order). Q: Is there any way to disable the compiler optimizations just for the scope of these lines without #pragma? This issue led me to find a solution in which the four asm instructions are unified.

#define MY_MACRO(_num, _string)\
asm volatile(".pushsection .my_section\n\t" \
             ".byte %0, %1, %2, %3\n\t" \
             ".ascii " #_string "\n\t" \
             ".popsection" \
             : : "i"((_num >> 24) & 0xFF), "i"((_num >> 16) & 0xFF), "i"((_num >> 8) & 0xFF), "i"(_num & 0xFF));

编译器错误:

foo.c:733:13: error: invalid 'asm': operand number missing after %-letter
foo.c:733:13: error: invalid 'asm': operand number out of range

由于字符串包含百分号(%),因此编译器将其解释为asm操作数。

Since the string includes the percent sign (%), the compiler interprets it as an asm operands.

#define MY_MACRO(_num, _string)\
asm volatile(".pushsection .my_section\n\t" \
             ".byte %0, %1, %2, %3\n\t" \
             ".ascii %4\n\t" \
             ".popsection" \
             : : "i"((_num >> 24) & 0xFF), "i"((_num >> 16) & 0xFF), "i"((_num >> 8) & 0xFF), "i"(_num & 0xFF), "X"(#_string));

在这里,我尝试将字符串作为操作数传递。我什至不知道这是否可行。
我没有设法编译这段代码。

Here I tried to pass the string as an operand. I don't even know if it's feasible. I didn't manage to compile this code.

推荐答案

选项B 是正确的方法,但是您需要将字符串中出现的所有百分号()加倍,因为这被解释为嵌入式asm中的操作数占位符。

Option B is the right way, however you will need to double all percent signs (%) occurring in your string because that's interpreted as an operand placeholder in the inline asm.

如果您不特别在意订购或内联,也可以让 gcc 为您处理:

If you don't particularly care about ordering or inlining, you could also let gcc handle it for you:

struct mystruct
{
    int num;
    char string[0];
};

#define MY_MACRO(_num, _string)\
{ static struct mystruct entry __attribute__ ((section (".my_section"))) = { _num, _string }; }

这篇关于多行内联汇编宏与字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆