为什么要code对准在x86偶数地址边界? [英] Why should code be aligned to even-address boundaries on x86?

查看:193
本文介绍了为什么要code对准在x86偶数地址边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过工作硖欧文的汇编语言的x86处理器,第六版并真的很享受它。

I am working through Kip Irvine's "Assembly Language for x86 Processors, sixth edition" and am really enjoying it.

我刚刚读到下一段的记忆NOP:

I have just read about the NOP mnemonic in the following paragraph:

"It [NOP] is sometimes used by compilers and assemblers to align code to 
 even-address boundaries."

给出的例子是:

00000000   66 8B C3   mov ax, bx
00000003   90         nop
00000004   8B D1      mov edx, ecx

这本书则指出:

"x86 processors are designed to load code and data more quickly from even 
 doubleword addresses."

我的问题是:是这样的原因是因为对于x86处理器的书是指(32位),CPU的字长为32位,因此它可以拉动与NOP中和过程的说明他们一气呵成?如果是这样的话,我假设有四字的字长64位处理器将与一个假想的5个字节code加上NOP的做到这一点?

My question is: Is the reason this is so is because for the x86 processors the book refers to (32 bit), the word size of the CPU is 32 bits and therefore it can pull the instructions with the NOP in and process them in one go ? If this is the case, I am assuming that a 64 bit processor with a word size of a quadword would do this with a hypothetical 5 bytes of code plus a nop ?

最后,我写我的code后,我应该通过与NOP的正确定位,以优化它,或者将编译器(MASM,在我的情况),对我来说做到这一点,作为文本似乎暗示?

Lastly, after I write my code, should I go through and correct alignment with NOP's to optimize it, or will the compiler (MASM, in my case), do this for me, as the text seems to imply ?

谢谢,

斯科特

推荐答案

code。所以,当加载如果你的指令没有对齐,然后有一个摊位。

Code that's executed on word (for 8086) or DWORD (80386 and later) boundaries executes faster because the processor fetches whole (D)words. So if your instructions aren't aligned then there is a stall when loading.

但是,你不能双字对齐每个指令。好吧,我想你可以,但你会浪费空间,并且处理器将不得不执行NOP指令,它会杀死对准指令的任何性能优势。

However, you can't dword-align every instruction. Well, I guess you could, but then you'd be wasting space and the processor would have to execute the NOP instructions, which would kill any performance benefit of aligning the instructions.

在实践中,对准$上DWORD C $ C(或其他)时的指令是分支指令的目标,以及编译器通常将调整函数的第一个指令,但不会对齐分支目标边界不仅有助于这也可以由通过下降到达。例如:

In practice, aligning code on dword (or whatever) boundaries only helps when the instruction is the target of a branching instruction, and compilers typically will align the first instruction of a function, but won't align branch targets that can also be reached by fall through. For example:

MyFunction:
    cmp ax, bx
    jnz NotEqual
    ; ... some code here
NotEqual:
    ; ... more stuff here

这是生成此code通常会调整编译器的MyFunction ,因为它是一个分支目标(由呼叫),但它不会对齐 NotEqual ,因为这样做会插入必须是 NOP 的说明通过掉落时执行。这增加code尺寸,使落空的情况下慢。

A compiler that generates this code will typically align MyFunction because it is a branch target (reached by call), but it won't align the NotEqual because doing so would insert NOP instructions that would have to be executed when falling through. That increases code size and makes the fall-through case slower.

我会建议,如果你刚开始学习汇编语言,你不用担心这样的事情会经常给你边际的性能提升。只写你的code,使事情的工作。他们的工作后,你可以分析他们,如果你觉得有必要看的资料数据之后,调整您的功能。

I would suggest that if you're just learning assembly language, that you don't worry about things like this that will most often give you marginal performance gains. Just write your code to make things work. After they work, you can profile them and, if you think it's necessary after looking at the profile data, align your functions.

汇编器通常不会自动为你做。

The assembler typically won't do it for you automatically.

这篇关于为什么要code对准在x86偶数地址边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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