使用`GCCs` pre处理器作为汇编 [英] Using `GCCs` pre-processor as an assembler

查看:181
本文介绍了使用`GCCs` pre处理器作为汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有各种开源汇编如气体 NASM 和的 YASM 。他们有不同的伪操作语法。对于许多开源项目,汇编为pre-处理,以取代常数和平台条件语句。

There are various open source assemblers such as gas, nasm, and yasm. They have different pseudo-ops and macro syntaxes. For many open source projects, assembler is pre-processed to replace constants and platform conditionals.

什么限制会 GCC 已创建汇编假设你可以使用目前所有的属性编译指示,不包括翻译的性能(编译/汇编为二进制时间)?

What limitations would gcc have creating assembler assuming you can use all current attributes and #pragmas, excluding translation performance (compile/assemble to binary time)?

我说的不是内联汇编

 #define MOV(RA,RB)  (OXFEB10000UL | RA << 16 | RB)  
 #define ADD(RA,RB)  (OXFEB20000UL | RA << 16 | RB)  
 #define RET         (OXFEB7ABCDUL)  

 unsigned long add4[] __attribute(section(".text")) =
 {
    ADD(R0,R1),
    ADD(R2,R3),
    MOV(R1,R2),
    ADD(R0,R1),
    RET()
 };

我相信使用指针运算可以让的模拟。标签。也许这是一个 XY问题;我试图理解为什么有这么多的装配在所有。好像一切都可以由pre处理器来完成,并汇编确实是一个程序员preference;或者有我缺少的技术限制。

I believe that using pointer arithmetic can allow simulation of . and other labels. Perhaps this is an XY problem; I am trying to understand why there are so many assemblers at all. It seems like everything can be done by the pre-processor and the assembler is really a programmer preference; or there is a technical limitation I am missing.

我想这可能与'有些事情,你可以用汇编,你不能用的壳code

I guess this might be related to 'Something you can do with an assembler that you can't do with shell code'.

编辑:我已经重新标记这从的 C 的到的编译的。我感兴趣的是一个汇编程序的技术细节。这纯粹是一个 1-1 翻译和发射搬迁(作为一个编译器),或者有更多?正如我上文所述,我不意味着人们code和汇编。我想了解一下装配在做什么。我不相信有一个龙书作为装配。当然,pre处理器不能创建一个二进制本身,需要额外的机械;只翻译文本。

I have re-tagged this from C to compiler. I am interested in the technical details of an assembler. Is it simply a 1-1 translation and emitting relocations (as a compiler will) or is there more? I don't mean for people to code assembler as I have outlined above. I am trying to understand what the assemblers are doing. I don't believe there is a Dragon book for assemblers. Of course, the pre-processor can not create a binary by itself and needs additional machinery; it only translates text.

推荐答案

我觉得 XY问题是一个错误的描述。现在的问题是更多的概念一个是需要评估的概念B

I think that XY Problem is a wrong description. The question is more "Concept A is needed to evaluate Concept B".

概念?答:什么是汇编

请参阅:组装机和装载机,通过的大卫所罗门。 [智慧一些珍珠,一些古老的琐事]

See: Assemblers and Loader, by David Solomon. [some pearls of wisdom, some archaic trivia]

我很快发现,在这一领域缺乏文献。在严格的对比编译器,为其广泛的文学存在,很少曾经被写在组装机和装载机。

I very quickly discovered the lack of literature in this field. In strict contrast to compilers, for which a wide range of literature exists, very little has ever been written on assemblers and loaders.

这是汇编组成,


  • 系统符号表以方便用户通过某个对象格式的链接。

  • 词法并的分析器文本转换为数据结构或直接向机器code。

  • 是否通过2次获取最有效的分支和子程序调用。

  • 一个运算code表。

  • A Symbol table to facilitates linking through some object format.
  • Lexer and Parser for converting the text to a data structure or directly to machine code.
  • Does 2 passes for most efficient branch and sub-routine calling.
  • An opcode table.

这是汇编一般是 1-1 翻译。然而,经常几个分支,并呼吁将存在的变体;一般被称为的的和的的版本。使用将取决于的距离的目的地运算code;需要两遍编译器优化的前向分支。提到由哈罗德

An assembler is generally a 1-1 translation. However, often several variants of branches and calls will exist; generally known as long and short version. The opcode used will depend on the distance to the destination; a two pass compiler is needed to optimize forward branches.Alluded to by Harold

概念B:使用'C'pre处理器作为汇编

Concept B: Using the 'C' pre-processor as an assembler.

最好的一个'C'pre-处理器可以模仿是1通汇编。一大类的CPU /指令可以连接codeD这样的;虽然宏可能是麻烦的。将不会有任何的列表的或的外部参照的,但大多数人不会错过这些功能。此外,语法是奇数,由于pre处理器的限制。这将是困难的交易与地址固定窗口为标签使用指针或手codeD要么重复使用'C'符号表的#define 的标签偏移。这限制了这种方法来什么,但一个基本块

The best a 'C' pre-processor could emulate is a 1-pass assembler. A large class of CPU/instructions can be encoded like this; although the macros could be cumbersome. There would be no listings or xrefs, but most people would not miss those features. Also, the syntax would be odd due to limitation of the pre-processor. It would be difficult dealing with address fix-ups as labels would either re-use the 'C' symbol table by using pointers or a hand coded #define for the label offset. This limits this approach to anything but a basic block.

大汇编程序,如YUV / RGB转换或MP3解码是高度不太可能采用这种方式。

Large assembler routines such as YUV/RGB transforms or MP3 decoding are highly unlikely to be used this way.

多重架构code是相当普遍的。例如,一个ARM芯片的WiFi可能有它嵌入在Linux内核固件code。它是可能的,这种技术可能是有用的在这里。然而,使用单独的编译器/汇编程序的不同的架构,然后使用 objcopy把来嵌入他们的是更为理智。

Multiple architecture code is quite common. For example an ARM wifi chip may have it's code embedded in a Linux kernel as firmware. It is possible that this technique could be useful here. However, using separate compilers/assembler for the different architectures and then using objcopy to embedded them is far more sane.

这可能是最有用的。其实很多工具,比如链接器和加载有高水平的功能,这在运行时补丁code。它也可以用于在运行时有条件地更改例行;函数指针几乎一样快,更容易理解,更何况高速缓存一致性的问题。

This is probably the most useful. In fact many tools, such as linkers and loaders have high level functions which patch code at run time. It could also be used to conditionally change a routine at runtime; function pointers are almost as fast and easier to understand, not to mention the cache coherency issues.

另请参阅:金博客,通过的伊恩·兰斯泰勒的。 [虽然他使用&LT;模板&GT; ]

See also: Gold Blog, by Ian Lance Taylor. [although he uses <templates>]

这篇关于使用`GCCs` pre处理器作为汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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