简体   繁体   English

在64位平台上运行的32位汇编代码

[英]32-bit assembly code running on 64-bit platform

I am studying x86 assembly language via the book called Programming from the Ground Up by Jonathan Barlett. 我正在通过乔纳森·巴列特(Jonathan Barlett)的《从头开始编程》一书学习x86汇编语言。 Recently I updated my system to 64-bits platform and problems with the syntax of the assembly code appeared, the pushl instruction to be more specific. 最近,我将系统更新为64位平台,出现了汇编代码的语法问题,pushl指令更具体了。 I spent some time looking for info on x86_64 isa, but I thought would be better to finish learning the basics of x86. 我花了一些时间在x86_64 isa上查找信息,但是我认为最好学习x86的基础知识。 In order to do that I was wondering if there's a way of assembling the older syntax into a 64-bits object, or something like that. 为了做到这一点,我想知道是否有一种方法可以将较旧的语法组装到64位对象中,或者类似的东西。 Or there were major changes which makes that impossible? 还是发生了重大变化,使之不可能? I am using Ubuntu 10.10 and GNU portable assembler. 我正在使用Ubuntu 10.10和GNU便携式汇编器。

Anyway. 无论如何。 Would be nice if you indicate a good book, or any source of info, on the x86_64 or in the differences between it and his predecessor. 如果您在x86_64上或其前身之间有差异,则表明您是一本好书或任何信息来源,那就太好了。

edit: Thanks! 编辑:谢谢! I got what I needed. 我得到了我所需要的。 Both answers were quite useful. 这两个答案都非常有用。 And don't worry, I'm looking forward to let go of the X86. 不用担心,我期待着释放X86。

Linux uses the x86_64 ABI specified here . Linux使用此处指定的x86_64 ABI。 The best thing you can do is take a read through the bits of the ABI most relevant to your area. 最好的办法是通读与您所在地区最相关的ABI信息。 Here's a few that come to mind: 这里有一些想法:

  • Unlike x86 there is only one calling convention on x86_64; 与x86不同,x86_64上只有一个调用约定。 two if you count the fact that Microsoft Windows is totally different again. 如果您算出Microsoft Windows完全不同的事实,则为两个。
  • In x86, arguments to functions are commonly passed on the stack. 在x86中,函数的参数通常在堆栈上传递。 in x86_64, the first few are in registers rdi , rsi , rdx , rcx , r8 and r9 for integer types. 在x86_64中,前几个在整数类型的寄存器rdirsirdxrcxr8r9中。
  • Memory address types are all QWORD so if you're using full registers ( r10 etc) with AT&T syntax you'll need movq , addq etc. 内存地址类型都是QWORD因此如果您使用具有AT&T语法的完整寄存器( r10等),则需要movqaddq等。

Now, part of the design of x86_64 was that it be backwards-compatible with x86 ie able to execute x86 code if the operating system is set up correctly. 现在, x86_64部分设计是它与x86向后兼容,即如果正确设置了操作系统,则能够执行x86代码。 So, there is nothing wrong with for example doing this: 因此,例如这样做没有任何问题:

nasm -felf32 myprog.asm
gcc -o myprog -m32 myprog.o

However, be aware; 但是,请注意; the more you start to rely on other code being available, the more you need 32-bit copies of everything. 您越依赖其他可用代码,就越需要所有内容的32位副本。 Note that this is basically using code in 32-bit mode entirely, complete with calling convention and everything. 请注意,这基本上是完全使用32位模式的代码,包括调用约定和所有内容。 In short, it should execute on a 32-bit machine too. 简而言之,它也应该在32位计算机上执行。 As Jerry says (+1), you cannot compile 32-bit assembly using 32-bit conventions in 64-bit mode. 正如Jerry所说(+1),您不能在64位模式下使用32位约定编译32位程序集。 You have to comply with the 64-bit ABI. 您必须遵守64位ABI。

Of course, beyond that, you are free inside your routines to use registers like eax , just be aware it affects the whole of rax or rather the half that is `eax. 当然,除此之外,您可以在例程中自由使用诸如eax寄存器,只要知道它会影响整个rax或半数的eax。

There are enough differences between 32-bit and 64-bit operation at the assembly language level that you almost certainly wouldn't want to just try to treat 32-bit code as if it was 64-bit code and hope for the best, or anything like that. 汇编语言级别的32位和64位操作之间存在足够的差异,您几乎可以肯定不会希望仅将32位代码视为64位代码,并希望获得最好的结果,或者像那样的东西。

When you're working as "close to the metal" as writing assembly language, it's usually for a reason -- you generally wouldn't/won't/don't appreciate the assembler trying to guess at what you really intended, and modifying your code to suit. 当您像编写汇编语言一样“接近金属”时,通常是有原因的-通常,您不会/不会/不喜欢汇编器试图猜测您的实际意图,并且修改代码以适合。 In some cases you'd just lose the advantages (eg, speed) and in others it would probably break what you were doing entirely (eg, when/if it sign extends something to 64 bits that should really have been zero-extended). 在某些情况下,您可能会失去优势(例如,速度),而在另一些情况下,则可能会破坏您的整体工作(例如,当/如果它的符号将某些内容扩展到64位,那么本来应该进行零扩展)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM