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

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

问题描述

有各种开源汇编器,例如 .它们有不同的 pseudo-opsmacro 语法.对于许多开源项目,汇编程序经过预处理以替换常量和平台条件.

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.

假设您可以使用所有当前的 attributes#pragmasgcc 对创建汇编程序有什么限制,不包括翻译性能(编译/汇编到二进制时间)?

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()
 };

我相信使用指针算法可以模拟.和其他labels.也许这是一个 XY 问题;我试图理解为什么有这么多汇编程序.似乎一切都可以由预处理器完成,而汇编器确实是程序员的偏好;或者我缺少技术限制.

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.

我想这可能与你可以用汇编程序做的事情你不能用 外壳代码'.

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

我已将它从 C 重新标记为 compiler.我对汇编程序的技术细节感兴趣.它只是一个 1-1 翻译和发射重定位(就像编译器那样)还是还有更多?正如我上面所概述的那样,我并不是要人们编写汇编程序.我试图了解汇编程序在做什么.我不相信有适合装配工的龙书.当然,预处理器不能自己创建binary,需要额外的机器;它只翻译文本.

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 问题 是错误的描述.问题更多的是Concept A需要评估Concept B".

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

概念 A:什么是汇编程序?

参见:汇编器和装载器,作者 David Solomon.[一些智慧的珍珠,一些古老的琐事]

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.

一个汇编程序包括,

  • 一个符号表,便于通过某些对象格式进行链接.
  • LexerParser 用于将文本转换为数据结构或直接转换为机器代码.
  • 执行 2 遍 以实现最有效的分支和子例程调用.李>
  • 一个操作码表.
  • 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 翻译.但是,通常会存在几种分支和调用的变体;通常称为 longshort 版本.使用的操作码取决于到目的地的距离;需要一个两遍编译器来优化前向分支.Harold

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"预处理器用作汇编器.

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

C"预处理器可以模拟的最好的是 1-pass 汇编器.一大类 CPU/指令可以这样编码;尽管宏可能很麻烦.不会有 listingsxrefs,但大多数人不会错过这些功能.此外,由于预处理器的限制,语法会很奇怪.处理地址修正会很困难,因为标签会通过使用指针或手动编码的 #define 来重新使用C"符号表作为标签偏移量.这将这种方法限制在基本块之外的任何东西.

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.

多架构代码很常见.例如,ARM wifi 芯片可能将其代码作为固件嵌入到 Linux 内核中.这种技术可能在这里有用.但是,为不同的架构使用单独的编译器/汇编器,然后使用 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.

这可能是最有用的.事实上,许多工具,例如链接器和加载器,都具有在运行时修补代码的高级功能.它还可以用于在运行时有条件地更改例程;函数指针几乎同样快速且易于理解,更不用说缓存一致性问题了.

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.

另请参阅:Gold 博客,作者 Ian兰斯·泰勒.[虽然他使用 <templates>]

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

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

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