从 Windows 移植到 Linux.汇编命令翻译 [英] Porting from Windows to Linux. Assembler command translation

查看:41
本文介绍了从 Windows 移植到 Linux.汇编命令翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习从 Windows 移植到 Linux.我一直在将程序从 Intel 语法转换为 AT&T 语法,并将其从 x32 转换为 x64.而且由于我对汇编程序特别是 AT&T 来说是新手,所以我在移植时遇到了一些麻烦.顺便提一下:我故意不使用 .intel_syntax 指令.

I have recently started learning porting from Windows to Linux. I've been translating program from Intel syntax to AT&T syntax also converting it from x32 to x64. And since I'm new enough to assembler and especially AT&T I've faced some troubles while porting. Just to mention: I'm intentionally not using .intel_syntax directive.

所以我不得不翻译这些命令:

RTLWriteIntegerBuffer: TIMES 3 DB 0x90,0x8D,0x40,0x00

紧随其后:

LEA EDI,[OFFSET RTLWriteIntegerBuffer+ECX-1]

另一个:

LEA EBX,[EDX+'0']

还有一个:

ReadCharInited: DB 0
CMP BYTE PTR ReadCharInited,0

另一个问题是:AT&T 语法和 Intel 语法之间是否存在 1:1 的映射?或者是否存在 AT&T 不支持的特定 Intel 命令?

Another question is: Is there 1:1 mapping between AT&T syntax and Intel syntax? Or are there specific Intel commands that are not supported in AT&T?

也许有人知道这样的函数:

And maybe someone knows about functions like this:

HEAP_NO_SERIALIZE=1
HEAP_GENERATE_EXCEPTIONS=4
HEAP_ZERO_MEMORY=8
...
INVOKE HeapAlloc,EAX,HEAP_GENERATE_EXCEPTIONS+HEAP_ZERO_MEMORY+HEAP_CREATE_ALIGN_16,4194332

这可能是 Borland Turbo Assembler 特定的调用 kernel32.dllHeapAlloc 的方法,但我不确定.能否翻译成fallocate系统调用?

This one is probably Borland Turbo Assembler-specific way to call kernel32.dll's HeapAlloc, but I'm not sure. Can it be translated to fallocate syscall?

提前致谢

推荐答案

在谈论AT&T 语法"与Intel 语法"时,通常仅指指令助记符与操作数顺序和格式之间的区别.

When talking about "AT&T syntax" versus "Intel syntax", it normally only refers to the difference between instruction mnemonics and operand ordering and format.

>

例如,这是 AT&T 语法中的指令:

So, for example, this is an instruction in AT&T syntax:

movl $1, (%esi)

这是使用 Intel 语法的相同指令:

and this is the same instruction using Intel syntax:

mov  DWORD PTR [esi], 1

对于可以用 Intel 语法表示的每条指令,在 AT&T 语法中都有该指令的等效表示.

For every instruction representable in Intel syntax, there's an equivalent representation in AT&T syntax for that instruction.

由于不再有 AT&T 汇编器和 Intel 汇编器,因此指令(除指令之外的所有内容)是另一回事.GNU 汇编器 (GAS) 支持 AT&T 和 Intel 语法,但仅支持它自己的指令,这些指令是 AT&T 汇编器使用的指令的扩展.Microsoft 的 MASM 仅支持 Intel 语法,但仅支持它自己的指令,这些指令是原始 Intel 汇编器的扩展.从一个汇编程序的指令到另一个汇编程序的指令并不总是有直接的等价物.在某些情况下,它们使用不同的目标文件格式这一事实可能会妨碍找到在使用不同目标文件格式的不同汇编器中实现指令功能的任何方式.(或者甚至是使用不同格式的相同汇编程序,就像 GNU 汇编程序一样.)

Since there's no AT&T assembler and no Intel assembler any more, the directives (everything other than the instructions) are a different matter. The GNU assembler (GAS) supports AT&T and Intel syntax, but only its own directives, which are an extension of the directives used by the AT&T assembler. Microsoft's MASM supports only Intel syntax but also only its own directives, which are an extension of the original Intel assembler's. There isn't always a direct equivalent from one assembler's directives to another assembler's. In some cases the fact that they use different object file formats may prevent finding any way of implementing the functionality of a directive in a different assembler using a different object file format. (Or even the same assembler using a different format, as can be the case with the GNU assembler.)

例如,以下是一些 GAS 指令:

As an example, here's some GAS directives:

.rept 3
.byte 0x90, 0x8D, 0x40, 0x00
.endr

这里是等效的 MASM 指令:

And here are the equivalent MASM directives:

REPT 3
DB 90h, 8Dh, 40h, 00h
ENDM

但是没有与以下 GAS 指令等效的 MASM,因为它特定于 MASM 不支持的 ELF 对象格式:

But there's no MASM equivalent of the following GAS directive, because it's specific to the ELF object format, which MASM doesn't support:

.protected foo

另一方面,没有直接等效于以下 MASM 指令,因为 GAS 不支持任何复杂的高级语言指令:

On the other hand there's no direct equivalent to the following MASM directive, because GAS doesn't support any complex high level language directives:

INVOKE HeapAlloc,EAX,HEAP_GENERATE_EXCEPTIONS+HEAP_ZERO_MEMORY+HEAP_CREATE_ALIGN_16,4194332

要移植以前的 ELF 特定指令,您必须重新设计应用程序以处理 Windows 如何处理共享库.要移植后来的特定于 MASM 的指令,您必须创建自己的宏来确定如何正确传递所有参数,或者只是手动写出此调用所需的所有汇编指令.Linux x86-64 ABI.(您还必须找到一个合适的 Linux 函数来调用和传递一组不同的参数,但这与翻译指令本身是一个单独的问题.)

To port the former ELF-specific directive you'd have to redesign the application to deal with how Windows handles shared libraries. To port the later MASM-specific directive you'd either have to create your own macro that did the work of figuring out how to pass the all the arguments correctly, or just manually write out all the assembly instructions necessary for this call according to the Linux x86-64 ABI. (You'd also have to find an appropriate Linux function to call and pass a different set of arguments, but that's a separate issue from translating the directive itself.)

一些汇编器试图与其他汇编器兼容;例如 Borland 的 TASM 试图与 MASM 兼容,尽管它是 MASM 的旧版本.因此,在 TASM(在其默认 MASM 模式下)中有效的内容通常在 MASM 中也有效,反之亦然.然而,许多汇编程序基本上使用他们自己版本的 x86 汇编语言.

Some assemblers try to be compatible with other assemblers; for example Borland's TASM tries to be compatible with MASM, although it's an older version of MASM. So what works in TASM (in its default MASM mode) will usually work in MASM and vice versa. Many assemblers, however, use essentially their own version of x86 assembly language.

例如,您在帖子中显示的代码似乎使用了两种不同的汇编语言版本,并且无法由任何单个汇编程序进行汇编.您的第一行代码使用 TIMES 指令,但该指令仅受 NASM 支持,它不使用 AT&T 语法或 Intel 语法.它有自己的指令语法,尽管它与 Intel 语法没有什么不同.它也有自己不兼容的指令集,不是基于任何特定的东西,比如你展示的 TIMES 指令.

For example, the code you've shown in your post seems to be using two different assembly language versions and can't be assembled by any single assembler. Your first line of code uses the TIMES directive, but this directive is only supported by NASM, which doesn't use AT&T syntax nor Intel syntax. It has its own instruction syntax, although it's not that different from Intel syntax. It also has its own incompatible set of directives, not based on anything in particular, like that TIMES directive you showed.

您的其余代码似乎采用 MASM 语法.除了第三行,它不能用 NASM 正确组装(第一行也不能用 MASM 正确组装).我也不确定是否会与 TASM 一起组装,因为 INVOKE 指令是在 MASM 6 中添加的.

The rest of your code appears to be in MASM syntax. Except for the third line, it wouldn't assemble correctly with NASM (nor would the first line assemble correctly with MASM). I'm not sure if would assemble with TASM either, since the INVOKE directive was added in MASM 6.

请注意,鉴于您的代码的性质,用汇编语言编写它可能没有任何好处,您最好将其翻译成 C、C++ 或其他您更熟悉的语言.

Note that, given the nature of your code, it probably gains nothing by being written in assembly language and you might be far better off translating it into C, C++, or some other language you're more familiar with.

这篇关于从 Windows 移植到 Linux.汇编命令翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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