LEA或ADD指令? [英] LEA or ADD instruction?

查看:470
本文介绍了LEA或ADD指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我手写组装,我一般选择的形式

When I'm handwriting assembly, I generally choose the form

lea eax, [eax+4]

在形式..

add eax, 4

我听说LEA是一个0时钟指令(如NOP),而添加不是。但是,当我看着编译器产生的大会我经常看到用来替代第一个后一种形式。我足够的信任编译器聪明,所以任何人都可以提供一些线索上哪一个更好?哪一个是更快?为什么编译器选择后一种形式了前者?

I have heard that lea is a "0-clock" instruction (like NOP), while 'add' isn't. However, when I look at compiler produced Assembly I often see the latter form used instead of the first. I'm smart enough to trust the compiler, so can anyone shed some light on which one is better? Which one is faster? Why is the compiler choosing the latter form over the former?

推荐答案

之间的一个显著差异 LEA 添加在x86 CPU的是实际执行指令的执行单元。现代的x86 CPU是超标量并具有并行操作,与管道有点喂养它们像循环赛(巴摊位)多个执行单元。事情是, LEA 被处理的寻址(这在早期阶段发生在管道)(一)单位(S)处理,而添加转到ALU(S)(算术/逻辑单元),并在管道已晚。这意味着,超标量x86处理器可以同时执行 LEA 和算术/逻辑指令。

One significant difference between LEA and ADD on x86 CPUs is the execution unit which actually performs the instruction. Modern x86 CPUs are superscalar and have multiple execution units that operate in parallel, with the pipeline feeding them somewhat like round-robin (bar stalls). Thing is, LEA is processed by (one of) the unit(s) dealing with addressing (which happens at an early stage in the pipeline), while ADD goes to the ALU(s) (arithmetic / logical unit), and late in the pipeline. That means a superscalar x86 CPU can concurrently execute a LEA and an arithmetic/logical instruction.

事实上, LEA 穿过地址生成逻辑,而不是运算单元也是为什么它曾经被称为零时钟的原因;它没有时间来执行,因为地址生成的已经发生了的由它会被执行/时间。

The fact that LEA goes through the address generation logic instead of the arithmetic units is also the reason why it used to be called "zero-clocks"; it takes no time to execute because address generation has already happened by the time it would be / is executed.

这不是的免费的,因为地址的产生是在执行流水线的一步,但它有没有执行开销。它不占据ALU流水线(S)插槽。

It's not free, since address generation is a step in the execution pipeline, but it's got no execution overhead. And it doesn't occupy a slot in the ALU pipeline(s).

编辑::要澄清, LEA 不是免费。即使在不通过它需要时间来执行运算单元实现它由于指令去code /调度/退休和/或其他管线的所有的说明经历阶段的CPU。所花费的时间做 LEA 刚刚发生的在它通过地址生成实现它的CPU不同阶段管道的的。

To clarify, LEA is not free. Even on CPUs that do not implement it via the arithmetic unit it takes time to execute due to instruction decode / dispatch / retire and/or other pipeline stages that all instructions go through. The time taken to do LEA just occurs in a different stage of the pipeline for CPUs that implement it via address generation.

这篇关于LEA或ADD指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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