gcc asm命令中指令的语法是什么? [英] What is the syntax for directives in gcc asm command?

查看:236
本文介绍了gcc asm命令中指令的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在gcc扩展asm命令中使用.ascii指令,但是我一直在遇到编译器错误.扩展的asm中的指令的确切语法是什么?

I'm trying to use .ascii directive in the gcc extended asm command but I keep getting compiler errors. What is the exact syntax for directives inside extended asm?

我尝试了以下选项,但没有一个起作用:

I tried the following options but none of the worked:

asm ("NOP;"
".ASCII ""ABC"""
);

我收到错误:行尾有垃圾,第一个无法识别的字符是'/'"

I got "Error: junk at end of line, first unrecognized character is `/'"

asm ("NOP;"
".ASCII "ABC""
);

我收到错误消息:行尾有垃圾,第一个无法识别的字符是'/'"

I got Error: junk at end of line, first unrecognized character is `/'"

asm ("NOP;"
.ASCII "ABC"
);

我在"/"令牌之前收到错误:预期为':'或')'

I got "error: expected ‘:’ or ‘)’ before ‘/’ token"

推荐答案

asm中指令的语法与编写GNU汇编器相同,因此您可以参考GNU汇编器手册中的相关语法.

The syntax for directives inside the asm is identical to writing GNU Assembler, so you can reference the GNU Assembler manual for the relevant syntax.

示例:

#include <stdio.h>
int
main (void)
{
  char *string;
  asm (".pushsection .rodata\n"
"0:\n"
"   .ascii \"Testing 1 2 3!\"\n"
"   .popsection\n"
"   mov $0b, %0\n":"=rm" (string));
  puts (string);
}

在该示例中,我们使用扩展的asm将字符串的地址复制到char *,然后将其传递给puts以打印字符串.

In the example we use an extended asm to copy the address of a string to a char * and then pass that to puts to print the string.

需要将字符串放置在适当的链接器部分中,而不仅仅是添加到当前部分中(通常是代码部分,即.text).因此,您首先将要存储字符串的部分压入汇编器的部分堆栈中.在此示例中,我给出了大多数字符串所在的只读数据部分(.rodata).然后,将该部分从部分堆栈中弹出,以返回到编译器留给您的任何部分,并使用字符串地址进行操作.诀窍是使用诸如0之类的本地标签来引用字符串,并让汇编器和链接器为您计算偏移量.如果您是PIE或PIC,这可能需要更多的工作,具体取决于您引用的复杂程度或它们是否需要重定位.

The string needs to be placed into the appropriate linker section, not just added to the current (usually the code section i.e. .text). So you begin by pushing the section you want the string stored to into the assembler's section stack. In this example I give it's the read only data section (.rodata) where most strings live. Then you pop the section off the section stack to get back to whatever section the compiler left you in, and do your operation with the string address. The trick is to use a local label like 0 to reference the string and let the assembler and linker compute the offset for you. This may require more work if you're PIE or PIC depending on how much more complicated your references become or if they require relocations.

这篇关于gcc asm命令中指令的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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